PHP API Builder is a lightweight and powerful library that streamlines API development in PHP. It provides:
- Clean Architecture: Enforces a well-structured and maintainable codebase
- ORM Integration: Built-in MySQL database integration with a simple yet powerful ORM
- Authentication: Out-of-the-box JWT authentication support
- RESTful Services: Easy implementation of RESTful endpoints
- Error Handling: Comprehensive error handling and debugging capabilities
- Zero Configuration: Minimal setup required with sensible defaults
- PSR-4 Compliant: Follows PHP-FIG standards for maximum compatibility
Perfect for building robust and scalable APIs while maintaining clean and organized code.
Use composer to manage your dependencies and download PHP-API-BUILDER:
composer require coagus/php-api-builder
For the proper functioning of the API, it is necessary to modify the composer.json file and add the .htaccess and index.php files. With this, we can start the development.
|-- composer.json
|-- .htaccess
|-- index.php
|-- services
To maintain order in our API development, we define a name for our project from which all our services will branch out. For this example, my project will be called 'Services,' and I will specify that it will be developed in the 'services' folder.
{
"require": {
"coagus/php-api-builder": "v1.0.0"
}
"autoload": {
"psr-4": {
"Services\\": "services/"
}
}
}
This file defines the behavior of our server. Generally, the entry point is our index.php, and the URL does not define the folders within the server.
# Disable directory listing (prevents showing files in an empty directory)
Options All -Indexes
# Disable MultiViews option, serving only the exact requested file
Options -MultiViews
# Enable the URL rewriting engine
RewriteEngine On
# Redirect all requests that are not existing files to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
# Set cache headers, establishing Cache-Control as private
<IfModule mod_headers.c>
Header set Cache-Control "private"
</IfModule>
The index.php is the entry point to the API; it only defines the project specified in its namespace.
<?php
require_once 'vendor/autoload.php';
$api = new ApiBuilder\API('Services');
$api->run();
Create a Demo service file in services/Demo.php
<?php
namespace Services;
class Demo
{
public function get()
{
success('Hello World!');
}
public function postHello()
{
$input = getInput();
success('Hello ' . $input['name'] . '!');
}
}
Result:
{
"successful": true,
"result": "Hello World!"
}
Request:
{
"name": "Agustin"
}
Result:
{
"successful": true,
"result": "Hello Agustin!"
}
Create your entity in your database, for example User:
CREATE TABLE `roles` (
`id` int NOT NULL AUTO_INCREMENT,
`role` varchar(30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`role`)
);
INSERT INTO roles (role)
VALUES ('Administrator'), ('Operator');
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(150) NOT NULL,
`email` varchar(70) NOT NULL,
`active` tinyint DEFAULT 0,
`role_id` int NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_users_roles_idx` (`role_id`),
CONSTRAINT `fk_users_roles` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`),
UNIQUE (`username`)
);
COMMIT;
Create de environment file ".env"
# DataBase configuration
DB_HOST=yourHost
DB_NAME=yourDbName
DB_USERNAME=yourUsername
DB_PASSWORD=yourPassword
DB_CHARSET=UTF8
create the entity in your API services/entities/User.php
<?php
namespace DemoApi\Entities;
use ApiBuilder\ORM\Entity;
class User extends Entity
{
public $id;
public $name;
public $username;
public $password;
public $email;
public $active;
public $roleId;
}
Request:
{
"name": "Agustin",
"username": "agustin",
"password": "Pa$$word",
"email": "christian@agustin.gt",
"active": 1,
"roleId": 1
}
Result:
{
"successful": true,
"result": {
"id": 1,
"name": "Agustin",
"username": "agustin",
"password": "Pa$$word",
"email": "christian@agustin.gt",
"active": 1,
"roleId": 1
}
}
Result:
{
"successful": true,
"result": {
"pagination": {
"count": 1,
"page": "0",
"rowsPerPage": "10"
},
"data": [
{
"id": 1,
"name": "Agustin",
"username": "agustin",
"password": "Pa$$word",
"email": "christian@agustin.gt",
"active": 1,
"roleId": 1
}
]
}
}
Request:
{
"name": "Christian Agustin"
}
Result:
{
"successful": true,
"result": {
"id": 1,
"name": "Christian Agustin",
"username": "agustin",
"password": "Pa$$word",
"email": "christian@agustin.gt",
"active": 1,
"roleId": 1
}
}
Result:
{
"successful": true,
"result": "Deleted!"
}