Getting started with Contentful and Laravel

This tutorial will show you how to setup the Content Delivery API in your Laravel application and how to access your content inside the framework.

The ContentfulBundle and the PHP SDK are currently in beta and their APIs may change before a stable release.

Requirements

The Contentful Laravel integrations requires at least PHP 5.5.9 and Laravel 5. PHP 7 is supported.

Installation

The easiest way to install the Laravel integration is is to use Composer. If you've downloaded composer.phar, the integration can be installed by executing:

1
2
php composer.phar require contentful/contentful:@beta
php composer.phar require contentful/laravel:@beta

Currently the SDK and the integration both need to be added to your composer.json because both only have had beta quality releases.

Enable the Service Provider

Next you need to enable the Service Provider by adding it to config/app.php:

1
2
3
4
5
6
7
8
<?php
return [
    'providers' => [
        // ...
        new Contentful\ContentfulBundle\ContentfulBundle(),
        // ...
    ]
];

Configuration

Before the Contentful SDK can be configured, the necessary config files have to be published. To do so execute the following command:

1
php artisan vendor:publish

This will add a file called contentful.php to your /config folder.

Now open that file and fill in your space ID and API key.

1
2
3
4
5
<?php
return [
  'delivery.space' => 'cfexampleapi',
  'delivery.token' => 'b4c0n73n7fu1'
];

To use the Preview API instead of the Content Delivery API, simply add 'delivery.preview' => true:

1
2
3
4
5
6
<?php
return [
  'delivery.space' => 'cfexampleapi',
  'delivery.token' => 'e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50',
  'delivery.preview' => true
];

Using Contentful

You now have a service for the class Contentful\Delivery\Client available. A small controller displaying an entry based on an ID in the URL could look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

use Illuminate\Routing\Controller as BaseController;
use Contentful\Delivery\Client as DeliveryClient;

class DefaultController extends Controller
{
    /**
     * @var DeliveryClient
     */
    private $client;

    public function __construct(DeliveryClient $client)
    {
        $this->client = $client;
    }

    public function entryAction($id)
    {
        $entry = $this->client->getEntry($id);

        if (!$entry) {
            abort(404);
        }

        return view('entry', [
            'entry' => $entry
        ]);
    }
}

To discover how to use the Contentful client, check out the getting started with Contentful and PHP tutorial.

Conclusion

Now you should be familiar with the basics of how to use Contentful in a Laravel application. You can find the integration on GitHub and Packagist. To get a deeper understanding, read some of our other PHP tutorials. If you find a bug, or have an idea how to further integrate with Laravel, please open an issue on GitHub.