Get started with 33% off your first certification using code: 33OFFNEW

How to add Statamic to an existing Laravel application

3 min read
Published on 7th November 2023

Blog Image

Laravel, known for its elegant syntax and robust capabilities, pairs beautifully with Statamic, a flat-file CMS that sits atop Laravel like a well-crafted cap. When you've got an existing Laravel app and want to infuse it with Statamic's content management prowess, the integration is not only possible but also quite straightforward. Here's how you can do it.

Prerequisites

Before we begin, ensure you have:

  • A Laravel application up and running.
  • Composer installed on your system.
  • Basic knowledge of Laravel and Command Line Interface (CLI) operations.

Step 1: Require Statamic via Composer

The first step is to pull in Statamic through Composer. Run the following command in your Laravel project directory:

composer require statamic/cms

Step 2: Publish Statamic Assets

After installation, you'll need to publish Statamic's assets and configuration files to your Laravel project:

php artisan vendor:publish --provider="Statamic\Providers\StatamicServiceProvider"

Step 3: Run Statamic Install Command

Next, you need to run the install command which scaffolds necessary directories, moves assets, publishes views, and sets up default user groups and roles:

php artisan statamic:install

Step 4: Migrate Users (If Necessary)

If you have existing users, you'll want to migrate them to Statamic's format. Statamic uses files for users, but you can opt to continue using your database by configuring the users repository in the config/statamic/users.php file.

Here's an example if you wish to keep users in the database:

'users' => [
    'repository' => 'database',
    'model' => App\Models\User::class,
],

If you decide to migrate your users to Statamic's file-based system, you can use the provided migration command after customizing the mapping in your config/statamic/migrator.php:

php artisan statamic:migrate:users

Step 5: Configure Your Filesystem

Statamic stores content, assets, and other data in the filesystem. Ensure your config/filesystems.php is set up correctly, typically involving the local and public disks.

Content - meaning the actual words on a page - is stored as YAML files on your filesystem within the content directory. This is configurable, and depending on your requirements you may wish to add it to your .gitignore file.

Step 6: Adjust Your Routes (If Necessary)

If your existing Laravel application has predefined routes, you might need to adjust them to accommodate Statamic's control panel and front-end routes. Statamic's routes can coexist with your existing ones; just make sure there are no conflicts.

Step 7: Theme Integration

To integrate your current Laravel app's theme with Statamic, place your blade templates within the resources/views directory. Statamic can use these directly, or you may convert them to use Statamic's Antlers templating language for more advanced CMS features.

Step 8: Review Configuration

Finally, go through the configuration files published to your config/statamic directory. Here, you can configure various aspects of Statamic, from caching to search, assets, static caching, and more.

Step 9: Test Everything

Once everything is set up, test your application thoroughly to ensure that both your Laravel components and Statamic sections are working harmoniously.

Profit

By adding Statamic to your existing Laravel app, you gain a powerful CMS that works seamlessly with Laravel's features. Whether you're looking to manage content more efficiently, build out a blog, or provide a backend for content editors, Statamic could be the silver bullet.

Integrating these two powerful tools can breathe new life into your Laravel application, providing an unbeatable combination of developer experience and content management.