Skip to content

Nayananga/project_3s_server

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.

I implemented this API in this project. It's a todo list web app developed in Angular.

QUICK INSTALL:

Pre Requisite:

  • Git.
  • Composer.
  • PHP.
  • MySQL/MariaDB.

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 database
$ 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

USING DOCKER:

You can use this project using docker and docker-compose.

MINIMAL DOCKER VERSION:

  • 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

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.2.3 by Sebastian Bergmann and contributors.

...........................................................                                                                                                            59 / 59 (100%)

Time: 243 ms, Memory: 14.00 MB

OK (59 tests, 320 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

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published