Add a headless CMS to Laravel in 5 minutes
Storyblok is the first headless CMS that works for developers & marketers alike.
This tutorial uses Laravel 10, which is currently a stable release. You can access the source code on our GitHub open source repository
Environment Setup
Requirements
To follow this tutorial make sure to meet these requirements:
- Basic understanding of PHP, Laravel, and Blade components
- PHP 8.1 or PHP 8.2
- Composer tool installed
- Homebrew tool (if you are using macOS)
- Node and NPM
- An account in the Storyblok App
The composer create-project
command covers also the PHP package installations and the key generation needed by Laravel ( php artisan key:generate
). So, once you enter the new directory, you can launch php artisan serve
to launch the internal webserver.
Now you’ll see this screen when you open http://127.0.0.1:8000 in your browser:
Shortly afterwards, a Storyblok space with sample content has been created for you. Let’s open the Home story by first clicking on Content {1} and then on Home {2}:
The Visual Editor
Now you’ll see the default screen and the Visual Editor:
Directly on the default home page, you can retrieve the access token {1} (useful later when we will set the environment variables in Laravel), and you can set the preview URL {2}, where the Laravel Web Application is exposed locally (by default with php artisan serve
the address is http://127.0.0.1:8000). If you change the preview URL, click on Save and Show button {3}. Probably you will see an error page, just because we still have to configure HTTPS.
Because the Visual Editor is exposed via HTTPS protocol, also your Laravel Application has to be available via HTTPS. The internal Web server (artisan serve) doesn't support the HTTPS, but we have more than one way to add HTTPS: via Valet (with Valet secure), via Laravel Sail (the Docker way), or using an HTTPS Proxy.
We are going to use the third method, adding an HTTPS Proxy. For that fill the Preview URL with: https://127.0.0.1:8010
At the end:
- the internal web server will expose the Web Application via HTTP on port 8000
- the HTTPS proxy will create a tunnel between the HTTP port 8000 and the exposed HTTPS port 8010.
- The browser will access via HTTPS to port 8010.
You can change the preview URL, and retrieve the access token later in the Settings section.
To change later the preview URL (or add a new one) go to Settings > Visual Editor {1} and set the Location field {2}:
Storyblok v2 requires that your website is served via HTTPS. You can follow the instructions in our FAQ entries: Setting up Dev Server with HTTPS Proxy On macOS or Setting up Dev Server with HTTPS Proxy On Windows.
Now, if you go back to the Home story, you won’t see your Laravel app in there just yet. Just one more quick step to take: Open the Entry configuration {1} and set the Real path to /
{2}. After having saved, you should now be seeing your Laravel app in the Visual Editor:
Connecting Laravel to Storyblok
First of all, let’s install our official SDK for PHP:
This SDK allows you to interact with the Storyblok API. Let’s start configuring it.
If you didn't copy the API token from the initial default home page, you can grab it by going to Settings > Access Tokens {1} and copying the Preview Token {2}.
Now we can use this token by adding the STORYBLOK_API_KEY
and STORYBLOK_DRAFT
to the .env
file:
Once you define the parameter in .env
file you have to allow Laravel to load them correctly. Create a storyblok.php
file in the config
directory. And load them via env()
function. Your parameter will be available to Laravel code with config('storyblok.api_key')
and config('storyblok.draft')
:
Rendering Dynamic Components in the Laravel App
The core idea of using Storyblok for this particular use case is the following:
- Content managers (even if it’s only yourself) can create pages (or stories) composed of different components (or bloks)
- Developers receive the page in the JSON format by using the Storyblok API and can render components accordingly (this is what we want to accomplish in our Laravel app)
When you create a new space from scratch, Storyblok automatically creates four default components for you:
- page (content type)
- grid (nestable component)
- feature (nestable component)
- teaser (nestable component)
You can find find all of these in the Components section of your space.
Understand the difference between the nestable components and content type in our Structures of Content tutorial.
You may be wondering why we added those components to a components
subfolder. By doing that, they’re found and loaded automatically by the Laravel x-dynamic-component
component. No need to register your components – it doesn’t get any easier!
Finally, we’ll have to edit the following two files:
Now you can build your CSS executing npm run build
.
And that’s it! Let’s move on.
Once installed and configured the npm packages, you can build the assets via npm run build
command. The command will generate all the assets and the manifest file.
All we have to do is to create the file resources/views/index.blade.php
with the following code:
At this point, we have a couple of things to do: the first one is to force Vitejs configuration and the Laravel assets helpers to provide via HTTPS all the assets included in the HTML page (because in our local environment we are using an HTTPS proxy that communicates with the builtin web server via HTTP), and the second one is to set the route in routes/web.php
file for loading the right view index.blade.php
created just now.
HTTPS configuration fine tuning
To adjust the Vitejs configuration add these lines
in the vite.config.js
fil. So the whole file will look like:
The last thing for fixing the behavior of our HTTPS proxy (not needed in a scenario where the Webserver is able to provide directly the HTTPS traffic), is to add this line:
in the app/Providers/AppServiceProvider.php
file, in the register()
method.
Declaring the catch-all route
In the routes/web.php
file we have to declare a catch-all route and be sure that the index.blade.php
is loaded with the data that comes from Storyblok API:
If you are using a space created in US region, you should set the region (apiRegion parameter) in the Storyblok Client initialization, using the PHP named arguments:
Visual editing with Storyblok Bridge
The power of Storyblok relies on its fantastic visual editing experience. Play with changing the teaser headline or re-arranging the features and see the magic happen!
In order to make editable the frontend generated from Blade templates you have to include the Storyblok Javascript Bridge in the index.blade.php
file before closing body
tag:
The first part loads storyblok-v2-latest.js
javascript file.
The second part creates a StoryblokBridge
instance and reloads the page when a change
event occurs. The change
event occurs when the content editor click on Save
button.
Resource | Link |
---|---|
Storyblok PHP SDK Client | https://github.com/storyblok/php-client |
Storyblok APIs | https://www.storyblok.com/docs/api/content-delivery/v2 |
Laravel | https://laravel.com/ |