Skip to content

adriano-pinaffo/php_rest_api

Repository files navigation

PHP REST API

Purpose

This work is the implementation of a CRUD system with REST API created in PHP without a framework. The basis for this is the Okta article Build a Simple REST API in PHP but without authentication.

Architecture

It has a database and a server-side PHP application to provide a REST API functionality to clients written in PHP, Bash, Python, Node, HTML forms and HTML/JavaScript.
For a version of a similar implementation with authentication, please refer to this project.
This system simulates the registering of persons on a database.

The following diagram shows all the clients on top and the server-side application on the bottom part.

php rest api

REST API Architecture diagram

Components

The server is composed of four parts:
  • FrontController: implemented in the index.php, it validates the request. Then, the FrontController processes it by calling the PersonController's processRequest method.
  • DatabaseConnector: implemented in DatabaseConnector.php and called by bootstrap.php, which will be imported (required) by index.php, it opens the connection to the database. The database parameters are located in the .env local file, and are loaded by phpdotenv.
  • PersonController: implemented in PersonController.php, it instantiates the PersonGateway to handle the database processes. It also implements the method verification, data validation/sanitization. Depending on the HTTP method a different method from PersonGateway is invoked.
  • PersonGateway: implemented in PersonGateway.php, it is the "model" that handles database activities. It implements the methods with PDO prepare to protect the database from SQL injection.

How to use it

  • Be sure to have PHP installed.
  • Clone this repository, enter in the root folder and run php -S 127.0.0.1:8000 -t public (it can be run as well with any Web server, or in the background by forking with & or nohup).

Clients

Six types of clients are available to work as an API client. As for the clients, please go to the client section to see examples and how to use them.

About

Rest API in PHP (without authentication)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published