There are many PHP Frameworks, but not all are easy to learn. Also, not all PHP Frameworks enable easy REST API development. In this article, we will review the most popular PHP API Frameworks that are actively developed. Also, they are optimized for REST APIs or Microservices. Microservices and APIs work together to support complex systems. Frameworks that are optimized for Microservices usually do well for APIs. But first, a little background is in order.
What is a REST API?
REST stands for REpresentational State Transfer. Dr. Roy Fielding coined the term at the turn of the century. He outlined REST in his 2000 Doctoral dissertation. Dr. Fielding included REST in his dissertation on the design of Network Architectures. REST, as applied to APIs, is modeled after the web itself. There are other types of APIs like SOAP, but REST is easier to integrate within my experience. Also, REST APIs are more popular than SOAP APIs.
Design Constraints
There are 6 key constraints to consider when building a REST API:
Uniform interface
A REST API must use conventions for its interfaces like HTTP does with the standard methods of GET and POST. Each resource should map to a single endpoint (you can think of this as a webpage). This also means that a developer’s learning curve only applies to the first endpoint. They should be able to follow a similar approach for all other endpoints.
Client-server
There should be no coupling or dependency between the client and server applications in a REST API integration. Each side should be able to evolve independently of the other. This preserves a constant interface between them.
Stateless
This constraint means that each new API request needs to be independent. It should not rely on any other requests. This is in contrast to the OAuth Dance. The OAuth Dance refers to a series of interactions to come up with a token. An otherwise RESTful API can then use the token for authentication. Even though OAuth itself is not RESTful, many popular APIs labeled REST use it. Microsoft, Google, Twitter, and Oracle all use OAuth to authenticate their REST APIs.
Cacheable
Caching can happen in the client, not the server. Within the response from the API can be caching instructions. These tell whether the client can cache the supplied data and for how long.
Layered system
This constraint restricts knowledge of the server to the interface only. That means you can expand the underlying logic and data structure over time. This can happen without affecting the API interface so that remains constant.
Code on demand (optional)
Dr. Fielding referred to downloading Java applets when describing this constraint. But for a PHP REST API, it could mean that an API response includes executable code. This code can be in any client-side language (like Javascript). The code can activate a widget or perform some action within the client.
Main Criteria for inclusion in this list
The main criteria used for selecting the best PHP Frameworks for developing REST APIs are as follows:
- API Focus: The design and focus of the framework include REST APIs or Microservices. The tagline of the framework’s GitHub home or the framework about page indicates this.
- Gentle learning curve: The framework is easy to use and learn. One sign of that is the use of common software design patterns. These common patterns include Model View Controller (MVC) and the Factory Method Pattern
- Active community: There is an active development community for the framework. Github and Stackoverflow will be the primary indicators for vibrant development communities.
Based on those three criteria, this is what I’ve found as a list of the top contenders for PHP API Frameworks:
What are the best PHP API Frameworks for REST APIs?
Lumen
The stunningly fast micro-framework by Laravel.
- Lumen is born out of Laravel but is focused on APIs specifically. On its home page is the following description. “Lightning fast micro-services and APIs delivered with the elegance you expect.”
- If you know Laravel, Lumen should be familiar to you already. Here is an example of creating a REST API using Lumen. The goal is to manage authors from a local database. After creating the database, the router looks like this:
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It is a breeze. Simply tell Lumen the URIs it should respond to | and give it the Closure to call when that URI is requested. | */ $router->get('/', function () use ($router) { return $router->app->version(); }); $router->group(['prefix' => 'api'], function () use ($router) { $router->get('authors', ['uses' => 'AuthorController@showAllAuthors']); $router->get('authors/{id}', ['uses' => 'AuthorController@showOneAuthor']); $router->post('authors', ['uses' => 'AuthorController@create']); $router->delete('authors/{id}', ['uses' => 'AuthorController@delete']); $router->put('authors/{id}', ['uses' => 'AuthorController@update']); });
All the routes go to one controller called AuthorController.php. It has a function for each of the basic Create, Read, Update, Delete (CRUD) operations.
- On the Lumen GitHub page, there have been 93 releases, there are 181 contributors, and the last commit was 4 hours ago. There are over 2k active questions on StackOverflow tagged Lumen. There are 111 questions tagged both lumen and API.
Guzzle
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.
- According to the Guzzle homepage, it is great for consuming APIs. Guzzle can be a middleware solution to interact with many 3rd party APIs. It can also interact well with local Microservices.
- Guzzle is easier than CURL to work with, simplifying the code. Here are a couple of examples:
$client = new GuzzleHttpClient(); // Send a synchronous request. $response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle'); echo $response->getStatusCode(); // 200 echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8' echo $response->getBody(); // '{"id": 1420053, "name": "guzzle", ...}' // Send an asynchronous request. $request = new GuzzleHttpPsr7Request('GET', 'http://httpbin.org'); $promise = $client->sendAsync($request)->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); $promise->wait();
- Guzzle powers the official AWS SDK for PHP. According to Github, there are 124 releases, 313 contributors, and the last commit was 10 days ago. There are over 1900 questions tagged guzzle on StackOverflow. Also, 313 questions include both the guzzle and API tags.
Slim
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
- The Slim Framework has API creation in it’s tagline on the framework homepage.
- Slim is a framework developed using the Factory Method Pattern. Here is a hello world type example which creates a very simple API –
<?php use PsrHttpMessageResponseInterface as Response; use PsrHttpMessageServerRequestInterface as Request; use SlimFactoryAppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/hello/{name}', function (Request $request, Response $response, array $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); $app->run();
- There are 141 StackOverflow questions tagged both API and slim, and over 2600 tagged slim. Github for the slim framework shows 96 releases and 198 contributors. The latest commit was 8 days ago.
Other Notable PHP API Frameworks
Leaf PHP
Leaf is a PHP micro framework that helps you create clean, simple but powerful web apps and APIs quickly
The GitHub home for Leaf PHP shows only 11 releases and 2 contributors. But the latest commit was last month so it seems to be alive and well. Leaf PHP is a Microservice with great reviews on Product Hunt. The Getting Started page describes Leaf PHP like this:
PHP starter kit that helps you quickly write simple yet powerful web applications and APIs without any framework
On the GitHub home we see the beginning of a simple application:
<?php require __DIR__ . 'vendor/autoload.php'; // Instantiate Leaf $leaf = new LeafApp; // In v2.0, the request and response objects are directly tied to the Leaf Object, // so you don't have to instanciate them if you don't want to // Add routes $leaf->get('/', function () use($leaf) { // since the response object is directly tied to the leaf instance $leaf->response->renderMarkup('<h5>My first Leaf app</h5>'); }); $leaf->post('/users/add', function () use($leaf) { $name = $leaf->request->get('name'); $leaf->response->respond(["message" => $name." has been added"]); }); // Don't forget to call leaf run $leaf->run();
Also, there is a LeafAPI package created specifically for API development. It replaces the View in “Model-View-Controller” design with a “Request-Response” layer.
Swoft
PHP Microservice Full Coroutine Framework
This Framework came out of Beta in May of 2019. It uses a concurrency design pattern that works well for developing APIs. There is an active Swoft community on Github with 24 contributors and 26 releases. The latest commit is 3 days old. But the Swoft developer team is all in China so there is more activity in Chinese than English. For example, Stackoverflow thinks I’m looking for Swift when I search for Swoft. Also, the coroutine concept involves more coupling than a true REST API. This means Dr. Fielding would not consider APIs created with SWOFT to be RESTful. 🙂
Ubiquity
PHP rapid development framework
This framework came on the scene in 2017 and there is no Stackoverflow tag for it yet. The Github page for Ubiquity shows 10 contributors, 28 releases, and the latest commit was 2 days ago. It is not only focused on APIs. But I’m including it here because of its focus on speed, which is very important for APIs. It is the fastest framework tested for REST API calls at PHPBenchmarks. There is also a REST module for developing REST APIs.
Fat Free Framework
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust web applications – fast!
This is another micro framework focused on fast development with a gentle learning curve. There is an active community in a google group as well as 403 questions on StackOverflow tagged fat-free-framework. There are 13 contributors and 35 releases in Github but the latest commit was over 5 months ago so it may not be actively developed anymore.
Conclusion
See below for a table of the PHP Frameworks we’ve discussed:
Summary: Best PHP Frameworks for REST APIs
PHP Framework | Focus of Framework | Learning Curve/Design Pattern | Github Statistics | StackOverflow Statistics |
---|---|---|---|---|
Lumen | Fast micro-services and APIs | Repository Pattern commonly used | 93 releases, 181 contributors, and last commit was 4 hours ago | Over 2000 questions tagged lumen |
Guzzle | HTTP requests and Microservice integration | Easier than CURL, no dictated design pattern | 124 releases, 313 contributors, and the last commit was 10 days ago. | Over 1900 questions tagged guzzle |
Slim | Simple yet powerful web applications and APIs. | Factory Method Pattern | 96 releases and 198 contributors in github. Latest commit is 8 days old. | 141 StackOverflow questions tagged both API and slim, and over 2600 tagged slim. |
Leaf PHP | Clean, simple, and powerful web apps and APIs. | Similar to MVC but modified for APIs. | 11 releases and 2 contributors. Latest commit one month old. | None yet |
Swoft | Coroutines | Concurrency design pattern | 24 contributors and 26 releases on Github. More active in Chinese than English. | None yet |
Ubiquity | Speed, not just for APIs | MVC design pattern | 10 contributors and 28 releases on Github. The latest commit was 2 days ago. | 25 questions tagged Ubiquity-Framework. |
Fat Free Framework | Fast learning curve | No specific pattern dictated | 13 contributors and 35 releases. But the latest commit was 5 months ago. | 403 questions tagged fat-free-framework |
There are many PHP Frameworks that can be used to create REST APIs. Besides what I’ve mentioned above, there are several other popular PHP frameworks. Laravel, Symfony, CodeIgniter, Yii, CakePHP, and others can all create REST APIs. Depending on your needs, anyone of those frameworks may be best for you. Or you might not even use a framework. Yet, using one of the Frameworks reviewed in this article can give you an advantage. They help you develop REST APIs which perform well. They are also supported by the developer community.
Other Related Frameworks:
Related Resources:
Laravel Framework says
Yii- It is a fast and out-of-the-way PHP framework that allows you to build any web app. This is a universal web-programming framework and is appropriate for all kinds of web apps. Yii development services are faster and easier because of the robust security features and simplified installation process.
David Plaskan says
I recently found a new open source API named DressApi (https://dressapi.com), I think it’s the best one because you can do everything with just a few lines of code. It was recently released but it works and looks very promising.