I have encountered this error in this file : route/api.php
This error refers to line 16
<?php
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
16- Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request::user();
});
Route::post('login', 'Api\AuthController#login');
Problem solved by changing this line
from
use Illuminate\Routing\Route;
to
use Illuminate\Support\Facades\Route;
Since You've not defined in Your question what is error message I can only predict that You're using middleware incorrectly.
Middleware should be assigned to group.
Route::middleware('auth:api')->group(function() {
Router::get('/user', function (Request $request) {
return $request::user();
});
});
or You've to use middleware after defining the route handler:
Route::get('/user', function(Request $request) {
return $request::user();
})->middleware('auth:api');
Because the method middleware is not static method, according to the assigning-middleware-to-routes try it like this:
Route::get('/user', function (Request $request) {
return $request::user();
})->middleware('auth:api');
Related
i am new to laravel and i am trying to submit form into data base but i am getting error i dont know why
i have added the screen shot along with that controller
when i do dd($REQUEST->all()) i am getting the form data
<?php
namespace App\Http\Controllers;
use App\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $REQUEST){
// dd($REQUEST->all());
inventories::Create($REQUEST->all());
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/inventory', 'App\Http\Controllers\InventoryController#index')->name('map');
Route::get('/inventory/sales', 'App\Http\Controllers\InventoryController#sales');
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/inventory', 'App\Http\Controllers\InventoryController#create')->name('invetory.create');
according to your code the controller should look like
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $request){
Inventory::Create($request->all());
}
}
change the namesapce of inventory to use App\Models\Inventory;
and also , inside create function use :
Inventory::Create($request->all());
When I route:list all routes in the system, I find some of the routes have Closure middleware.
Although I did only assign api and auth a middlewares
api.php
Route::post('api/favorites/{post}', 'api/favorites/{post}')->middleware('auth');
Output
| Method | URI | Action | Middleware
-------------------------------------------------------------------------------------
| POST | api/favorites/{post} | \FavoritesController#favorite | api,auth,Closure
Middlewares can be set as a Closure as well. In case you need a middleware for a single usage only then you don't need to create a dedicated middleware for that. Instead, you can just add a Closure middleware in your Controller's __construct:
public function __construct(Request $request)
{
$this->middleware(function ($request, $next) {
//your logic
return $next($request);
});
}
When I log in Laravel, first thing that shows is 1 then it redirects me to my intended page. I have checked everywhere, there is no middleware which makes 1 to show up. I don't get what is the problem.
This is my login code:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
class LoginController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated()
{
return redirect('admin/dashboard');
}
public function showLoginForm()
{
return view('admin.login');
}
protected function validateLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required'
]);
}
public function logout()
{
session::flush();
auth::logout();
return back();
}
}
These are my routes
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
Auth::routes();
// Route::get('/home', 'HomeController#index')->name('home');
Route::post('/login', 'Auth\LoginController#attemptLogin')->name('login');
Route::middleware('auth')->prefix('/admin')->name('admin.')->group(function() {
Route::get('/dashboard', 'DashboardController#index')->name('dashboard');
Route::post('/logout', 'Auth\LoginController#logout')->name('logout');
});
There is not much in my routes as I just started to build my site and I am facing this error.
I have code on api.php thats using a Dingo function and I am having trouble calling the controller because it always state that Controller is not found even though in their documentation it clearly has specified that I have to put the full path of the controller in which what is I have done.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// use Dingo\Api\Contract\Http\Request;
// use Dingo\Api\Facade\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
// This Will Work
// $api->get('hello', function() {
// return "hi";
// });
// Will not work
$api->get('hello', 'App\\Api\Controllers\\TestController#index');
$api->get('hi','App\\Http\\Controllers\\TestController#index');
});
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
File locations:
Error Message: Error Message: message: "Target class [app\Http\Controllers\TestController] does not exist.", status_code: 500,
What do you think I am missing here?
Casing really does matter when referencing files. I just found this out recently.
Inside hello route
namespace app\Api\Controllers;
use Dingo\Api\Http\Request;
use app\Http\Controllers\Controller;
class TestController extends Controller
{
public function index(Request $request)
{
return "hi";
}
}
Have to change the app\Http\.. to App\Http\...
Also did the same on Api.php Controllers Path
Just a reminder so when you try to use "Copy relative path" on vscode try to watch out of the naming convention.
I've been trying for some time to figure this out, but no solution I found was conclusive. Basically I'm trying to retrieve a list of clients based on the user logged in, but I cannot retrieve the user object anywhere in the controller except the index() function where is returns the view, and also the blade template that displays the logged in user's name.
This is my ClientController.php:
<?php
namespace App\Http\Controllers;
use App\Http\Models\Client;
use App\Http\Requests\GetClientsRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class ClientController extends Controller
{
protected $user;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
/**
* Show the clients page.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
Log::info("client user: " . print_r($this->user, 1));
return view('page');
}
public function getClients()
{
$currentUser = Auth::user();
if ($currentUser) {
$clients = Client::with('account')
->where('user_id', $currentUser->account)
->get();
$collection = collect($clients);
return response($collection->toArray());
}
}
}
The Log in the index function prints out the user object no problem - but when it's called in the getClients() function, it's empty. I also tried using this in the __construct():
$this->middleware('auth');
As per the Laravel template I was using, but whenever I call the getClients API route I always get a 401 Unauthorized error.
Here is my api.php routes file (although only currently using the getClients call) :
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('welcome-message', 'DashboardController#getWelcomeMessage');
Route::prefix('clients')->group(function () {
Route::get('/', 'ClientController#getClients');
Route::get('/{clientId}', 'ClientController#getClient');
Route::post('/', 'ClientController#postCreateClient');
Route::put('/{clientId}', 'ClientController#putUpdateClient');
});
And my web.php routes file:
<?php
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::get('/', 'HomeController#index')->name('dashboard');
Route::get('/clients', 'ClientController#index')->name('clients');
The system knows I'm logged in, because I wouldn't even be able to visit the client dashboard at all, so I don't understand why I cannot get a result from Auth::user() in the function - unless I'm using the middleware incorrectly?
You will have to use the auth:api middleware to allow API authentication.
To do so, add this middleware to your API route, omit in that case to add middleware in the __constructor.
Route::group([
'prefix' => 'clients',
'middleware' => ['auth:api']
], function(){
Route::get('/', 'ClientController#getClients');
// ... etc
});
Make sure that you have a valid access token to access your API. When using your API with a JS front-end, read about it in the Laravel docs.
Note that 'web' authentication (middleware: auth) uses a different mechanism than API authentication (middleware: auth:api) to authenticate users.