api.php file:-
<?php
use AppHttpControllersUserController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| 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::post('login', [UserController::class,'login']);
Route::prefix('user')->middleware('auth:sanctum')->controller(UserController::class)->group(function () {
Route::get('/user', function (Request $request) {
return $request->user();
});
Route::post('/create','create');
});
Result of php artisan route:list :-
POST api/login .................................................. generated::ZbCPv6PYuv5EbuXE › UserController@login
POST api/user/create ........................................... generated::FVy5Q2BuwMm8p22P › UserController@create
GET|HEAD api/user/user .................................................... generated::6FyVEOXSKZ88144C › UserController
GET|HEAD sanctum/csrf-cookie ......................... sanctum.csrf-cookie › LaravelSanctum › CsrfCookieController@show
Kernel.php file:-
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
'api' => [
// LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,
'throttle:api',
IlluminateRoutingMiddlewareSubstituteBindings::class,
LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,
IlluminateRoutingMiddlewareThrottleRequests::class.':api',
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
];
The solution i tried :-
php artisan route:cache
4
Answers
Solved by adding
to the Postman header, reference
You have it already:
"api/login"
If you wish to be accessed without "api" prefix move it to web.php file.
This error occurs when you try to access a route by it’s name. You may need to name you route:
naming route will solve this issues , so name it
->name('login')
also checkget
orpost
.i create both route you may use according to your requirements.GET ROUTE:
Route::get('/login', [UserController::class,'login'])->name('login');
POST ROUTE:
Route::post('/login', [UserController::class,'login'])->name('login');