问题
I have created ApiController in App\Http\Controllers\Api\v1
Also created auth
using laravel/ui
Default created function for front end working perfectly.
But issue is when try to call the ApiController
My API Route file is as below
Route::group(['prefix' => 'api/v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController@register');
});
And my API controller look like
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function register(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
}
Before 404 it was csrf error and i have resolving it by
protected $except = [
'/register',
];
in Http\Middleware\VerifyCsrfToken
I cant figure out two question
How to
except
my entire api call from CSRF using $except..How to solve 404 for
register
method , i use postman with POST request and call URLhttp://localhost/larablog/api/v1/register
回答1:
Routes defined in the routes/api.php
file are nested within a route group by the RouteServiceProvider. Within this group, the /api
URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider
class.
1) 404 error :- Remove api
from prefix route.
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController@register');
});
http://localhost/larablog/api/v1/register
1. If you are using a route group:
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController@register');
});
Your $except
array looks like:
protected $except = ['v1/register'];
2. If you want to exclude all routes under v1
Your $except
array looks like:
protected $except = ['v1/*'];
来源:https://stackoverflow.com/questions/58111453/getting-404-in-laravel-6-x