Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I prioritize Statamic routes over Laravel routes


How do I prioritize Statamic routes over Laravel routes


To prioritize Statamic's routes over Laravel's routes when integrating Statamic with an existing Laravel application, you can follow these steps:

1. Exclude Statamic's Routes from Laravel's Route Handling:
In your `app/Providers/RouteServiceProvider.php` file, in the `boot()` method, add the following code to exclude Statamic's routes from Laravel's route handling:

php
   public function boot()
   {
       $this->configureRateLimiting();

       $this->routes(function () {
           Route::middleware('web')
                ->group(base_path('routes/web.php'));

           Route::prefix('api')
                ->middleware('api')
                ->group(base_path('routes/api.php'));

           // Exclude Statamic's routes from Laravel's route handling
           Route::middleware(['web', 'statamic.cp'])
                ->prefix(config('statamic.routes.cp'))
                ->group(base_path('routes/statamic.php'));
       });
   }
   

This will ensure that Statamic's routes are handled separately from your Laravel application's routes.

2. Create a `routes/statamic.php` File:
Create a new file called `routes/statamic.php` and add your Statamic-specific routes in this file. This will keep your Statamic routes separate from your Laravel application's routes.

Example `routes/statamic.php` file:

php
   <?php

   use Statamic\Routing\RouteGroup;

   // Statamic control panel routes
   Route::statamic('cp', 'statamic.cp.dashboard');
   Route::statamic('cp/{segments?}', 'statamic.cp.index')
        ->where('segments', '.*');

   // Statamic front-end routes
   Route::statamic('/', 'home');
   Route::statamic('blog/{slug}', 'blog.show');
   Route::statamic('blog', 'blog.index');
   

In this example, we've defined the control panel routes as well as some front-end routes.

3. Ensure Statamic's Middleware is Applied:
Make sure that the `statamic.cp` middleware is applied to the control panel routes in your `routes/statamic.php` file. This middleware is responsible for handling the authentication and authorization for the control panel.

By following these steps, you can ensure that Statamic's routes take priority over your Laravel application's routes. Statamic's routes will be handled separately, and your custom Laravel routes will not conflict with Statamic's routes.

Remember that you can still define custom Laravel routes in your `routes/web.php` file, and they will take precedence over Statamic's routes. However, it's generally recommended to use Statamic's routing wherever possible to take advantage of its content management features and integrations.

Citations:
[1] https://statamic.com/addons/jacksleight/bonus-routes
[2] https://statamic.dev/controllers
[3] https://laracasts.com/discuss/channels/laravel/routes-priority
[4] https://laracasts.com/discuss/channels/laravel/is-it-possible-to-prioritize-a-packages-routes
[5] https://github.com/statamic/cms/issues/1764