Laravel API route requires Session

2020-05-27 php laravel

I used Laravel only for API requests. So i have deleted the Session classes from Kernel.php.

My routes are in web.php. Now i need to create web pages. So i moved current routes to api.php, and this occurs error.

Class session.store does not exist

Why api routes tries to use Session? Because api calls don't need Session. How can i switch off session for api routes? Because api calls accepting more than 100 requests per second.

Answers

Laravel's default guard is set to web. When you use only API endpoints, you need to change your defaults.guards in config/auth.php to api for example. You can see in app/Http/Kernel.php that $middlewareGroups for api does not includes session middlewares so they are not used.

If you use the default api driver token then you need to add a api_token column to your users table and include this parameter on the request like http://localhost/api/user?api_token={token}.

Take a look at https://laravel.com/docs/6.x/api-authentication#introduction. Seems that it changed in Laravel 7 but I don't know which version you are using.

Related