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.
- PHP 7.x
- MySQL 5.x || MariaDB 10.x
- Composer.
- Git.
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
$ 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
- 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.1.5 by Sebastian Bergmann and contributors.
....................................................... 55 / 55 (100%)
Time: 171 ms, Memory: 12.00 MB
OK (55 tests, 290 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.