coagus / php-api-builder
PHP API Builder is a library designed to simplify the construction of APIs in PHP, ensuring clean, well-structured code, and providing out-of-the-box support for connecting a MySQL database as a resource.
Requires
- php: ^8.1
- firebase/php-jwt: ^6.10
- monolog/monolog: ^3.8
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- phpunit/phpunit: ^11.4
README
PHP API BUILDER
A library designed to simplify the construction of APIs in PHP, ensuring clean, well-structured code, and providing out-of-the-box support for connecting a MySQL database as a resource.
Installation
Use composer to manage your dependencies and download PHP-API-BUILDER:
composer require coagus/php-api-builder
Get Started
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
composer.json
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": "v0.6.0" } "autoload": { "psr-4": { "Services\\": "services/" } } }
.htaccess
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>
index.php
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();
Examples Demo Service
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'] . '!'); } }
GET http://localhost/api/v1/demo
Result:
{ "successful": true, "result": "Hello World!" }
POST http://localhost/api/v1/demo/hello
Request:
{ "name": "Agustin" }
Result:
{ "successful": true, "result": "Hello Agustin!" }
Example ORM
Database
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;
Environment File
Create de environment file ".env"
# DataBase configuration
DB_HOST=yourHost
DB_NAME=yourDbName
DB_USERNAME=yourUsername
DB_PASSWORD=yourPassword
DB_CHARSET=UTF8
Entity
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; }
POST http://localhost/api/v1/users
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 } }
GET http://localhost/api/v1/users
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 } ] } }
PUT http://localhost/api/v1/users/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 } }
DELETE http://localhost/api/v1/users/1
Result:
{ "successful": true, "result": "Deleted!" }