Example of REST API with Slim PHP micro framework.
This simple RESTful API, allows CRUD operations to manage resources like: Users, Tasks and Notes.
You can also read this README IN SPANISH.
Main technologies used: PHP, Slim 3, MySQL, Redis, PHPUnit and JSON Web Tokens.
Also, I use other aditional tools like: Docker & Docker Compose, Travis CI, Swagger, Code Climate, Scrutinizer, Sonar Cloud, PHPStan, Heroku, CORS, environment variables, composer and git.
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.
- PHP.
- Composer.
- MySQL/MariaDB.
- Redis (Optional).
You can create a new project running the following commands:
$ composer create-project maurobonfietti/rest-api-slim-php [my-api-name]
$ cd [my-api-name]
$ composer restart-db
$ composer test
$ composer start
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 restart-db
$ composer test
$ composer start
You can use this project using docker and docker-compose.
Minimal Docker Version:
- Engine: 18.03+
- Compose: 1.21+
Commands:
# Start the API (this is my alias for: docker-compose up -d --build).
$ make up
# To create the database and import test data from scratch.
$ make db
# Checkout the API.
$ curl http://localhost:8081
# Stop and remove containers (it's like: docker-compose down).
$ make down
If you get stuck, you can try this guide step by step.
Example of how to use this API with JWT for Authentication (Spanish 🇪🇸):
How to use Redis in this API with Slim and PHP (Spanish 🇪🇸):
- 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: Flexible and feature-complete Redis client for PHP and HHVM.
- 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.
- nunomaduro/phpinsights: Instant PHP quality checks from your console.
Run all PHPUnit tests with composer test
.
$ composer test
> phpunit
PHPUnit 9.1.1 by Sebastian Bergmann and contributors.
............................................................... 63 / 63 (100%)
Time: 221 ms, Memory: 16.00 MB
OK (63 tests, 367 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}
-
Create Task:
POST /api/v1/tasks
-
Update Task:
PUT /api/v1/tasks/{id}
-
Delete Task:
DELETE /api/v1/tasks/{id}
-
Get All Notes:
GET /api/v1/notes
-
Get One Note:
GET /api/v1/notes/{id}
-
Create Note:
POST /api/v1/notes
-
Update Note:
PUT /api/v1/notes/{id}
-
Delete Note:
DELETE /api/v1/notes/{id}
Also, you can see the API documentation with the full list of endpoints.
For more information on how to use the REST API, see the following documentation available on Postman Documenter.
All the information of the API, prepared to download and use as postman collection: Import Collection.
Also, you can view the OpenAPI Specification, using Swagger UI.
You can deploy this API with Heroku Free.
Check it out to this LIVE DEMO.
Would you like to support this project?
The MIT License (MIT). Please see License File for more information.