Example of REST API with Slim PHP micro framework.
This simple RESTful API made in Slim version 3, allows CRUD operations to manage resources like: Users, Tasks and Notes.
You can also read this README IN SPANISH.
More info about this project in my post: How to create a REST API using Slim PHP.
I implemented this API in this project. It's a todo list web app developed in Angular.
- Git.
- Composer.
- PHP.
- MySQL/MariaDB.
In your terminal execute this commands:
$ git clone https://github.com/maurobonfietti/rest-api-slim-php.git && cd rest-api-slim-php
$ cp .env.example .env
$ composer install
$ composer database
$ composer start
$ git clone https://github.com/maurobonfietti/rest-api-slim-php.git
$ cd rest-api-slim-php
$ cp .env.example .env
$ composer install
From the command line run:
$ mysql -e 'CREATE DATABASE rest_api_slim_php;'
The database can be updated manually using the following file: database.sql.
It can also be run from the command line:
$ mysql rest_api_slim_php < database/database.sql
Edit and complete configuration file: .env
. For example:
DB_HOSTNAME = '127.0.0.1'
DB_DATABASE = 'rest_api_slim_php'
DB_USERNAME = 'root'
DB_PASSWORD = ''
For example:
DISPLAY_ERROR_DETAILS=true
APP_DOMAIN='https://www.yourdomain.com'
USE_REDIS_CACHE=false
REDIS_URL=''
SECRET_KEY='YourSuperSecret-KeY'
You can start the PHP internal web server by running:
$ composer start
If everything went well 😎, you can access the project locally by entering: Help, Status and Notes.
The composer start
command would be the equivalent to execute:
$ php -S 0.0.0.0:8080 -t public public/index.php
You can use this project using docker and docker-compose.
- Engine: 18.03+
- Compose: 1.21+
Work In Progress...
Execute for example:
# To create the database and import test data from scratch.
$ make db
# Start the API (this is my alias for: docker-compose up -d --build).
$ make up
# Checkout the API.
$ curl http://localhost:8053
# Stop and remove containers (or docker-compose down).
$ make down
- slim/slim: Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
- respect/validation: The most awesome validation engine ever created for PHP.
- palanik/corsslim: Cross-origin resource sharing (CORS) middleware for PHP Slim.
- vlucas/phpdotenv: Loads environment variables from
.env
togetenv()
,$_ENV
and$_SERVER
automagically. - predis/predis: A PHP extension for Redis.
- firebase/php-jwt: A simple library to encode and decode JSON Web Tokens (JWT) in PHP.
- phpunit/phpunit: The PHP Unit Testing framework.
- phpstan/phpstan: PHPStan - PHP Static Analysis Tool.
Access the root of the project and run all tests PHPUnit with composer test
.
PHPUnit 8.2.3 by Sebastian Bergmann and contributors.
........................................................... 59 / 59 (100%)
Time: 243 ms, Memory: 14.00 MB
OK (59 tests, 320 assertions)
-
Help:
GET /
-
Status:
GET /status
-
Login User:
POST /login
-
Create User:
POST /api/v1/users
-
Update User:
PUT /api/v1/users/{id}
-
Delete User:
DELETE /api/v1/users/{id}
-
Get All Tasks:
GET /api/v1/tasks
-
Get One Task:
GET /api/v1/tasks/{id}
-
Search Tasks:
GET /api/v1/tasks/search/{string}
-
Create Task:
POST /api/v1/tasks
-
Update Task:
PUT /api/v1/tasks/{id}
-
Delete Task:
DELETE /api/v1/tasks/{id}
Also, you can see the API documentation with the full list of endpoints.
All the information of the API, prepared to download and use as postman collection: Import Collection.
For more information on how to use the REST API, see the following documentation available on Postman Documenter.
Check it out to this live demo hosted on Heroku.