Skip to content
Advertisement

Getting 404 in laravel 6.x

I have created ApiController in AppHttpControllersApiv1

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' => 'Apiv1'], function () {
  Route::post('register', 'ApiController@register');
});

And my API controller look like

namespace AppHttpControllersApiv1;

use AppHttpControllersController;
use IlluminateHttpRequest;

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 HttpMiddlewareVerifyCsrfToken

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

Advertisement

Answer

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' => 'Apiv1'], 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' => 'Apiv1'], 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/*'];
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement