Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you configure routes when integrating Statamic with Laravel
    Laravel Livewire


How do you configure routes when integrating Statamic with Laravel


When integrating Statamic with an existing Laravel application, there are a few key steps to configure the routes:

1. Exclude Statamic Routes from Laravel's Route Handling: By default, Statamic will register its own routes, which can conflict with any existing routes in your Laravel application. To avoid this, you need to exclude the Statamic routes from Laravel's route handling.

In your `app/Providers/RouteServiceProvider.php` file, add the following code in the `boot()` method:

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 routes from Laravel's route handling
           Route::middleware('web')
                ->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;

   Route::statamic('/', 'home');
   Route::statamic('blog/{slug}', 'blog.show');
   Route::statamic('blog', 'blog.index');
   

In this example, we've defined three Statamic routes: a home page, a blog post page, and a blog index page.

3. Customize Statamic Routes as Needed: If you need to customize or extend Statamic's default routes, you can do so in the `routes/statamic.php` file. This allows you to integrate Statamic's routing with your existing Laravel application's routing structure.

4. Handle Statamic's Control Panel Routes: Statamic's control panel routes are handled separately from the front-end routes. You can configure these routes in the `config/statamic/routes.php` file.

By default, the control panel routes are prefixed with `/cp`. If you need to change this, you can update the `cp` key in the `config/statamic/routes.php` file.

By following these steps, you can seamlessly integrate Statamic's routing with your existing Laravel application, ensuring that there are no conflicts between the two and that your Statamic-specific routes are properly configured.

Citations:
[1] https://github.com/statamic/cms/issues/1764
[2] https://accreditly.io/articles/how-to-add-statamic-to-an-existing-laravel-application
[3] https://statamic.com/addons/jacksleight/bonus-routes
[4] https://github.com/jonassiewertsen/statamic-livewire
[5] https://statamic.dev/installing/laravel