Skip to content

rocker76/rest-api-slim-php

 
 

Repository files navigation

REST API IN SLIM PHP

Example of REST API with Slim PHP micro framework.

alt text

This simple RESTful API made in Slim version 3, allows CRUD operations to manage resources like: Users, Tasks and Notes.

Build Status Test Coverage Code Quality Quality Gate Status

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.

QUICK INSTALL:

Pre Requisite:

  • PHP 7.x
  • MySQL 5.x || MariaDB 10.x
  • Composer.
  • Git.

Run commands:

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

How to install

STEP BY STEP:

1- Clone project and install dependencies:

$ git clone https://github.com/maurobonfietti/rest-api-slim-php.git
$ cd rest-api-slim-php
$ cp .env.example .env
$ composer install

2- Create a new MySQL database. For example: "rest_api_slim_php".

From the command line run:

$ mysql -e 'CREATE DATABASE rest_api_slim_php;'

3- Create the structure and load test data into the database.

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

4- Configure the connection data with MySQL.

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 = ''

5- Configure optional environment variables.

For example:

DISPLAY_ERROR_DETAILS=true
APP_DOMAIN='https://www.yourdomain.com'
USE_REDIS_CACHE=false
REDIS_URL=''
SECRET_KEY='YourSuperSecret-KeY'

LOCAL SERVER:

You can start the PHP internal web server by running:

$ composer start

NOTE:

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

DEPENDENCIES:

LIST OF REQUIRE DEPENDENCIES:

  • 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 to getenv(), $_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.

LIST OF DEVELOPMENT DEPENDENCIES:

TESTS:

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)

DOCUMENTATION:

ENDPOINTS:

  • 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.

IMPORT WITH POSTMAN:

All the information of the API, prepared to download and use as postman collection: Import Collection.

Run in Postman

HELP AND DOCS:

For more information on how to use the REST API, see the following documentation available on Postman Documenter.

GIVE IT A TRY:

Deploy

Check it out to this live demo hosted on Heroku.

About

Example of REST API with Slim PHP Framework.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 90.6%
  • TSQL 5.2%
  • Shell 3.2%
  • Other 1.0%