Skip to content

Instantly share code, notes, and snippets.

@lagbox
Last active July 30, 2016 05:48
Show Gist options
  • Save lagbox/2bbfa3becae5c50d585dcabeb0201fa3 to your computer and use it in GitHub Desktop.
Save lagbox/2bbfa3becae5c50d585dcabeb0201fa3 to your computer and use it in GitHub Desktop.
Laravel >= 5.2.27 Registering API routes separately.

If you installed Laravel after 5.2.26, you will have the 'web' group applied to all your routes in routes.php. To add API routes that don't need the 'web' middleware group you can create another routes file to hold those routes and load them in your RouteServiceProvider separately.

  • Create a api-routes.php file in the same directory as your routes.php file.
  • Put your API routes in the api-routes.php file.
  • Adjust your RouteServiceProvider (app/Providers/RouterServiceProvider.php) to add what is in the example RouteServiceProvider provided.
    • Adjust namespace and middleware values of the array passed to group in mapApiRoutes to what you would like.
<?php
...
class RouteServiceProvider ...
{
...
public function map(Router $router)
{
$this->mapWebRoutes($router);
// add this
$this->mapApiRoutes($router);
}
// add this
protected function mapApiRoutes(Router $router)
{
$router->group([
'namespace' => 'App\Http\Controllers\Api', 'middleware' => 'api',
], function ($router) {
require app_path('Http/api-routes.php');
});
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment