Remove web middleware for API in Laravel

The web middleware is by default loaded for ebery route in the app/Http/routes.php file. If you create an API, you mostly don't need all the things this web middleware is loading, like cookie sessions and CRSF protection. To create routes for the API:

  1. Create a new app/Http/routes-api.php file and add the routes for the API.
  2. Copy the mapWebRoutes method to mapApiRoutes in the app/Providers/RouteServiceProvider.php file and adjust it, so it loads the api middleware and requires the routes-api.php file.
  3. Make sure it calls this new method in the same place it now only calls mapWebRoutes.
Tag: | Category: