Giter Site home page Giter Site logo

maurobonfietti / rest-api-slim-php Goto Github PK

View Code? Open in Web Editor NEW
290.0 13.0 94.0 23.46 MB

Example of REST API with Slim PHP micro Framework.

License: MIT License

PHP 95.32% Dockerfile 0.39% Makefile 0.81% HTML 3.45% Procfile 0.03%
php slim-micro-framework rest-api mysql slim-3 slim rest api slim-php-framework

rest-api-slim-php's Introduction

REST API IN SLIM PHP

This is an example of a RESTful API built using the Slim PHP micro-framework.

The API allows you to manage resources such as users, tasks, and notes.

You can also read this README IN SPANISH.

Software License Build Status Code Quality Test Coverage Packagist Version

alt text

πŸ–₯️ TECHNOLOGIES USED:

The leading technologies used in this project are:

  • PHP 8
  • Slim 3
  • MySQL
  • Redis
  • dotenv
  • PHPUnit
  • JSON Web Tokens (JWT)

Additional tools:

Also, I use other additional tools like:

  • Docker & Docker Compose
  • Travis CI
  • Swagger
  • Code Climate
  • Scrutinizer
  • Sonar Cloud
  • PHPStan
  • PHP Insights
  • Heroku
  • CORS

βš™οΈ QUICK INSTALL:

Requirements:

  • Git
  • Composer
  • PHP >= 8.0
  • MySQL/MariaDB
  • Redis (Optional)
  • or Docker

With Composer:

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

How to install

With Git:

In your terminal, execute these 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

With Docker:

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

πŸ› οΈ TROUBLESHOOTING:

If you get stuck, you can try this guide step by step.

🎦 TUTORIALS:

Watch this mini-series of videos about Slim PHP (Spanish Audio πŸ”‰ πŸ‡ͺπŸ‡Έ πŸ‡¦πŸ‡·).

πŸ“Ή VIDEO #1

How to install and configure this API.

πŸŽ₯ VIDEO #2

How to use JWT for Authentication.

πŸ“Ή VIDEO #3

Using Redis Cache.

πŸŽ₯ VIDEO #4

Deploy Slim PHP on Heroku.

πŸ“¦ 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: 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.

LIST OF DEVELOPMENT DEPENDENCIES:

πŸš₯ TESTING:

Run all PHPUnit tests with composer test.

$ composer test
> phpunit
PHPUnit 9.5.28 by Sebastian Bergmann and contributors.

........................................................          56 / 56 (100%)

Time: 00:00.697, Memory: 18.00 MB

OK (56 tests, 343 assertions)

SCREENSHOOTS:

Screen Shot API using Browser


Screen Shot API using Postman


πŸ“š DOCUMENTATION:

ENDPOINTS:

INFO:

  • Help: GET /

  • Status: GET /status

USERS:

  • Login User: POST /login

  • Create User: POST /api/v1/users

  • Update User: PUT /api/v1/users/{id}

  • Delete User: DELETE /api/v1/users/{id}

TASKS:

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

NOTES:

  • 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 complete list of endpoints.

IMPORT WITH POSTMAN:

All the API information is prepared for download and use as Postman collection: Import Collection.

Run in Postman

πŸš€ DEPLOY:

You can deploy this API with Heroku.

Deploy

ℹ️ MORE INFORMATION:

For more information about this project, check out my blog post: How to create a REST API using Slim PHP.

You can also look at the to-do list web app I developed using this API in Angular.

πŸ’¬ CONTRIBUTING:

If you would like to contribute to the project, please open an issue or submit a pull request. Contributions are always welcome!

❀️ DO YOU LIKE THE PROJECT?

You can support this project by inviting me a coffee β˜• πŸ˜‹ or giving a star to this repo ⭐ 😎.

Buy Me a Coffee at ko-fi.com

πŸ“„ LICENSE

The MIT License (MIT). Please see License File for more information.

rest-api-slim-php's People

Contributors

dependabot[bot] avatar dinsen avatar maurobonfietti avatar scrutinizer-auto-fixer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rest-api-slim-php's Issues

"migration" tool.

Hi,

Someone just asked for help with using this project, and looking quickly I found that the 'database migration' script actually is more of a 'nuke from orbit and recreate from a backup' script.

This is just an opinion but:

If you're going to call it a DB migration tool, then it should probably behave like phinx or Doctrine migrations.

If it's intended to drop the existing database and restore from a backup, then it shouldn't be called a migration tool.

Having the capability for a 'migration' to destroy data is highly suprising.

cheers
Dan
Ack

Invalid HTTP status code

I am getting following error eventhough passing JWT token to getalltask API.

Fatal error: Uncaught InvalidArgumentException: Invalid HTTP status code in F:\wamp64\www\testapi\vendor\slim\slim\Slim\Http\Response.php on line

Adding Port Setting

Hi,
This is an awesome job, thank you so much. In my case the mysql server isn't running on the default port, I had to tweak the code a bit to allow for an additional setting.

in .env:
DB_PORT='3308'

in extras/bin/restart-db.php:
$dsn = "mysql:host=${hostname};port=${port};charset=utf8";
$pdo = new PDO($dsn, $username, $password);

in src/app/dependencies.php:
$port = $db['port'];
$dsn = sprintf('mysql:host=%s;dbname=%s;port=%s;charset=utf8', $db['hostname'], $db['database'], $db['port']);

Thanks again for this great project.

Error en restart-db.php

No coge variables de entorno de la DB.
Fichero: extras/bin/restart-db.php
La funciΓ³n $container->get('settings')['db']; no estΓ‘ accesible.

Las funciones getenv del fichero restart-db.php no devuelven las variables de entorno.

Issue on the database > Dependencies.php file

Hola, Buenas tardes.

Intentando probar tu api veo que me da un error al ejecutar el comando > "composer database" en la linea 18 del archivo Dependencies.php.

He comentado la linea y ha funcionado a la primera podrias tomar nota o decirme si ha sido causa algo mal condigurado?

Adjunto imagenes.

error api

Error al ejecutar "composer database"
2020-06-15_01-05

Funciona OK luego d e comentar la linea marcada en la primera imagen.
app

Install warning

Hello

I see this:

Package webmozart/path-util is abandoned, you should avoid using it. Use symfony/filesystem instead.

image

Gettin 500 error on every route with /[{id}]

Thanks for work, is is very easy to deploy with docker.
I am getting the following issue and as a beginner I need your help

Fatal error: Uncaught InvalidArgumentException: ReasonPhrase must be supplied for this code in /var/www/html/vendor/slim/slim/Slim/Http/Response.php on line 204

The strange thing is that there is a message key value for each.
It seems that it does this only for routes that contain [{id}] (GET, POST, PUT, DELETE).

I have just cloned and run the make up command.

Any idea?

slim3 -> slim4 Error on php 8.2

PHP Deprecated: Return type of Slim\Collection::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

slimphp/Slim#3234

Not work

Auto documentation

Hi,

It is possible with your API to run Swagger documentation automatically ?

Thanks !

Very good job

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.