Getting started with Contentful and Symfony

This tutorial will show you how to setup the ContentfulBundle in your Symfony application and how to access you content inside the framework.

Requirements

The ContentfulBundle requires at least PHP 5.5.9 and Symfony 2.7. PHP 7 and Symfony 3 are supported.

Installation

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

php composer.phar require contentful/contentful-bundle

Adding ContentfulBundle to the application kernel

Next you need to enable the Bundle by adding it to app/Kernel.php:

<?php
public function registerBundles()
{
    return [
        // ...
        new Contentful\ContentfulBundle\ContentfulBundle(),
        // ...
    ];
}

Configuration

To configure the Bundle add the following section to your application's config.yml:

contentful:
  delivery:
    space: cfexampleapi
    token: b4c0n73n7fu1

This is the minimum configuration necessary to use ContentfulBundle. To use the Preview API instead of the Content Delivery API, simply add preview: true:

contentful:
  delivery:
    space: cfexampleapi
    token: e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50.
    preview: true

If you need access to multiple spaces or to both the delivery API and the Preview API you can configure multiple clients:

contentful:
  delivery:
    clients:
      default:
        space: cfexampleapi
        token: b4c0n73n7fu1
      preview:
        space: cfexampleapi
        token: e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50.
        preview: true

The name default is special and used for the default service. You can override which the default service is by specifying the default_client. For example:

contentful:
  delivery:
    default_client: example
    clients:
      example:
        space: cfexampleapi
        token: b4c0n73n7fu1
      example_preview:
        space: cfexampleapi
        token: e5e8d4c5c122cf28fc1af3ff77d28bef78a3952957f15067bbc29f2f0dde0b50.
        preview: true

To confirm that everything is configured as you wish, execute bin/console contentful:info in your shell. The output should look like this:

+-----------------+--------------------------------------------+----------+--------------+
| Name            | Service                                    | API      | Space        |
+-----------------+--------------------------------------------+----------+--------------+
| example         | contentful.delivery.example_client         | DELIVERY | cfexampleapi |
| example_preview | contentful.delivery.example_preview_client | PREVIEW  | cfexampleapi |
+-----------------+--------------------------------------------+----------+--------------+

Using Contentful

You now have the services contentful.delivery and contentful.delivery.default_client available. Both pointing to the default client. If you have more than one client configured, or have specified a name, clients will be available in services following this naming scheme: contentful.delivery.{name}_client. A small controller displaying an entry based on an ID in the URL could look like this:

<?php

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/entry/{id}", name="entry.item")
     */
    public function entryAction($id)
    {
        $client = $this->get('contentful.delivery');
        $entry = $client->getEntry($id);

        if (!$entry) {
            throw new NotFoundHttpException;
        }

        return $this->render('default/entry.html.twig', [
            'entry' => $entry
        ]);
    }
}

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

Using the Web Debug Toolbar

The ContentfulBundle integrates with Symfony's Web Debug Toolbar. If the toolbar is shown and there were requests to the Contentful API, there will be a section showing how many requests haven been made against the API.

Clicking on that section will open the Contentful panel in the web profiler.

This view shows you a all requests that were made against one of Contentful's API including how long they took. Clicking on the the "Details" in the last column gives you an overview of the request and response and exceptions thrown by the Contentful SDK.

Conclusion

Now you should be familiar with the basics of how to use Contentful in a Symfony application. You can find the Bundle 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 Symfony, please open an issue on GitHub.