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 Schema.org.
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).
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 Schema.org vocabulary to avoid re-inventing the wheel. Though you’re not required to use Schema.org 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 Schema.org vocabulary. In the example below, we use the schema configuration for a blogging app, with blog posts and people (authors) as the main resources.