Was this page helpful?

Getting started with Contentful and Laravel

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

Requirements

The Contentful Laravel integration requires at least PHP 7.0.

Installation

The easiest way to install the Laravel integration is is to use Composer.

composer require contentful/laravel

Enable the Service Provider

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

return [
    'providers' => [
        // ...
        Contentful\Laravel\ContentfulServiceProvider::class,
        // ...
    ]
];

Configuration

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

php artisan vendor:publish  --provider="Contentful\Laravel\ContentfulServiceProvider"

This will add a file called contentful.php to your /config folder. Now open it, and add at least your space ID and API key.

return [
    /*
     * The ID of the space you want to access.
     */
    'delivery.space' => env('CONTENTFUL_SPACE_ID'),
    /*
     * The ID of the environment you want to access.
     */
    'delivery.environment' => env('CONTENTFUL_ENVIRONMENT_ID', 'master'),
    /*
     * An API key for the above specified space.
     */
    'delivery.token' => env('CONTENTFUL_DELIVERY_TOKEN'),
    /*
     * Controls whether Contentful's Delivery or Preview API is accessed.
     */
    'delivery.preview' => (bool) env('CONTENTFUL_USE_PREVIEW', \false),
    /*
     * The default locale to use when querying the API.
     */
    'delivery.defaultLocale' => env('CONTENTFUL_DEFAULT_LOCALE'),
    /*
     * A closure which manipulates a ClientOptions object.
     * See Contentful\Delivery\ClientOptions for more.
     */
    'delivery.options' => function (ClientOptions $options, Application $application) {
        // Update $options however you prefer
    },
];

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:

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.