Runscope API Monitoring    Learn More →

Create API-First Web Apps with API Platform, a PHP Framework

By Neil Mansilla on .

Every programming language is subject to scrutiny, and PHP is no exception. While often pegged as an all too basic language forged some time during the Gilded Age, PHP has done more than just stand the test of time—the language and its ecosystem have continued to evolve. In fact, one article cites that PHP has over 25 web application frameworks (10 with recent version updates), eclipsed only by Java with 37.

Just last week, PHP added yet another web application framework to the fold, Dunglas’s API Platform (DAP). Developed by Symfony core developer Kévin Dunglas, DAP is an API-first web framework built on top of Symfony, one of PHP's most popular frameworks. DAP embraces hypermedia and open standards, like JSON-LD, Hydra and

Even though DAP is in its early stages (currently beta), it shows a lot of promise in becoming a very useful framework on its own, or at least one that influences developers to pursue an API-first development approach. Built on top of the full-stack Symfony framework, DAP gives you access to an enormous library of Symfony bundles (including bundles for OAuth and JWT).

API-first Development

About six years ago, the concept of mobile first was born, recognizing that we’d be consuming more content and using more apps on our mobile devices than on our PCs/desktops. In 2014, we crossed that inflection point. And in recent years, designers and developers have recognized that data and services are being consumed across a growing number of platforms and devices, bringing us to a new concept—API first development, which prioritizes building well-designed, architected and robust APIs before building the apps that consume them.

Getting Started with API Platform

Even if you're not a PHP developer, you'll be able to follow along with how to use DAP. Begin by creating a hypermedia API. Dunglas recommends defining your model using vocabulary to avoid re-inventing the wheel. Though you’re not required to use nor the generator, doing so will deliver fully generated PHP entities complete with type documentation, ORM mapping and validation annotations and much more. Before you begin customizing and optimizing, go through the process laid out in the README to better understand where your hand-crafted customizations are needed.

Generation Tools at your Fingertips

After defining your schema in YAML, run it through some of DAP’s bundled tools. First, you’ll use PHP Schema, a bundled project component to generate the PHP entities that represent your data model. At this point, you can start making code customizations to the entities.

Next, you’ll use Doctrine ORM (also bundled) to create the database and generate the tables for your API. Doctrine is optional and in fact, using a database is optional. However, while you're investigating DAP, use the bundled ORM as well as a database to observe all of DAP's functionality.

One Step to Creating Docs

Using another bundled tool, DAP generates nicely formatted Swagger UI-like documentation. The docs are available at the /doc endpoint as soon as you launch the server. Each parameter shows well-defined types of docs inherited from the vocabulary. In the example below, we use the schema configuration for a blogging app, with blog posts and people (authors) as the main resources.

Getting Down with REST

Now that the API is up and running, you can start executing calls. Some features that you get out of the box are a series of CRUD operations through RESTful methods, request validation against the schema, error handling and more. In the example below, we take an empty resource (/people), POST a new record, GET the collection and then DELETE the person we just added.

Now, Start Coding!

After you've defined, generated and stood up the API, it's time to build the client application. The author has included an AngularJS integration, but can easily work with any client technology such as Ionic, React or your favorite native mobile framework.

What's Good, What's Next

Dunglas's API Platform framework serves as a great introduction to the concept of API-first development. It's also a swell introduction to hypermedia APIs, with support for, JSON-LD and Hydra.

For more information about DAP check out the project repo.

Categories: apis, howto, community, api ecosystem, hypermedia

Everything is going to be 200 OK®