Getting 404 in laravel 6.x

我的梦境 提交于 2021-01-29 10:18:16

问题


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

  1. How to except my entire api call from CSRF using $except..

  2. How to solve 404 for register method , i use postman with POST request and call URL http://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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!