Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you ensure that Statamic's backend routes are accessible after integration
    Laravel Service Provider


How do you ensure that Statamic's backend routes are accessible after integration


To ensure that Statamic's backend (control panel) routes are accessible after integrating Statamic with an existing Laravel application, you need to take the following steps:

1. Exclude Statamic's Control Panel 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 control panel 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 control panel 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 control panel 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, including the control panel 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 backend (control panel) routes are accessible and properly integrated with your existing Laravel application. This separation of Statamic's routes from your Laravel application's routes will help avoid any potential conflicts or issues.

Citations:
[1] https://statamic.com/addons/jacksleight/bonus-routes
[2] https://statamic.com/addons/skynettechnologies/statamic-all-in-one-accessibility
[3] https://github.com/statamic/cms/issues/1764
[4] https://statamic.dev/routing
[5] https://accreditly.io/articles/how-to-add-statamic-to-an-existing-laravel-application