Contentful +ย PHP

Introduction

KB3

Itโ€™s no secret that managing an old-school, monolithic content management systems (CMS) comes with its fair share of burdens. Whether itโ€™s worrying about scalability issues or babysitting dense database architecture, one thing is clear: Nobody has time for that. Thatโ€™s why weโ€™re proposing a new solution: an API-first, technology agnostic content infrastructure.In this article, weโ€™ll highlight the benefits of using Contentful and content infrastructure for your next PHP project.

What isย Contentful?

Before we go any further, itโ€™s important to clarify what youโ€™ll be getting with Contentful. Through ourย content infrastructure, youโ€™ll receive a platform that enables developers to fetch content with API calls, while offering editors a familiar-looking web app for creating and managing content.So whether youโ€™re developing for wearables, smartphones, or web apps, your content is just anย API callย away. And because it runs as a hosted solution in the cloud, it requires far less maintenance than a traditional PHP content management systemโ€”leaving you more time to focus on building great applications.

Differences between Contentful and otherย CMSs

When talking about PHP CMSs, developers are often limited toย WordPress or Drupal. The problem with these legacy CMSs is that they are created around one type of content theyโ€™re optimized to display, usually websites, and most become simple web publishing tools.Contentful, on the other hand, is completely adapted to your needs. You define your ownย content model, which is independent from any presentation layer. This model is what defines the kinds of content you want to manage. Then, you can take that content and create engaging experiences around it, while our CDN takes care of delivering content to your users 24/7.

Our PHPย SDKs

We provide SDKs for popular programming languages like JavaScript, Ruby, and, of course, PHP. These SDKs are designed to make your life as a developer easier when using Contentful and give you almost instant access toย our APIsย and their features.

Content Delivery API (CDA)

Contentfulโ€™s Content Delivery API (CDA) is aย read-only API for delivering content from Contentfulย to apps, websites, and other mediaโ€”arriving as JSON data and media files. To get started with ourย PHP CDA SDK, make sure you haveย Composerย installed on your machine. You can then use Composer to install the Content Delivery API (CDA) SDK:

Contentful + PHP / CS1

Then add the Composer autoloader to the top of your project:

Contentful + PHP / CS2

All interactions with the SDK go through 'Contentful\Delivery\Client'. To create a new client, you have to pass an access token and a space ID to the constructor. From there, you can adjust your query to receive the appropriate data.

Hereโ€™s an example that fetches content from Contentful using ourย Images API:

Contentful + PHP / CS3

And thereโ€™s so much more where that came from. If you want to dive deeper, weโ€™ve explained this entire process in ourย getting started tutorial.

Content Management API (CMA)

We also recently released theย beta version of the PHP SDK for Contentfulโ€™s Content Management API (CMA). This is aย read-write API for managing content. You can use the CMA for several use cases, including automatic imports from CMSes like WordPress or Drupal and building custom editing experiences.

Once you have Composer installed and you include the autoloader (as outlined above), you can then work with the following code snippet to manage content from Contentful:

Contentful + PHP / CS4

Integrations with Symfony and Laravelย frameworks

Youโ€™re probably using a framework for your PHP applicationsโ€”and for a good reason. Frameworks enable you to scale quickly, code maintenance is more manageable than with plain PHP, and they allow you to invest your time in the actual task instead of the supporting technology.Donโ€™t worry, weโ€™ve got you covered. Contentful has integrations for two of the most popular PHP frameworks:ย Symfonyย andย Laravel.

Symfony

One of our PHP evangelists once described the Symfony Framework as a bit like a beautiful soup base. Itโ€™s made of excellent ingredients (well, components) and from that, you can make any kind of soup (application) you want. People love it, including Contentful clientย Spotify.Hereโ€™s how you can configure your Contentful client the Symfony way:

Contentful + PHP / CS5

Along with this integration, weโ€™ve also launchedย ContentfulBundle, an additional ingredient that you can add to your Symfony-based soup. The 'ContentfulBundle' integrates with Symfony Web Profiler, allowing you to fix bugs and performance issues on the integration point between your application and Contentful.

So you can see our 'ContentfulBundle' in action, we created a generic product catalog app connected to an example space using Symfony. You can find the full code onย GitHubย or follow ourย step-by-step guideย to build it yourself.

Laravel

Probably the most popular framework right now, Laravel is self-proclaimed as the โ€œPHP Framework for Web Artisans.โ€ With out-of-the-box REST API compatibility, unit-testing support, and a focus on speed of development, the framework is a natural fit with the Contentful platform.

Configuring your Contentful client using Laravel looks like this:

Contentful + PHP /. CS6

To facilitate this collaboration, we produced theย Contentful Laravel Package. This package empowers PHP developers to integrate the Contentful API into their applications built with the Laravel framework. Additionally, we developed aย sample blogging appย using the Content Delivery API with a supplementaryย tutorial.

Whatโ€™sย next

Weโ€™re constantly developing new features and framework integrations for PHP. In fact, we have an entire team dedicated to it. But in the meantime, you can also check out theย Contentful for PHPย page for tutorials, example apps, and more information on other ways of using PHP with Contentful. Or if youโ€™re ready to jump right in,ย create a free accountย today andย try it for yourself.