Giter Site home page Giter Site logo

maurobonfietti / slim4-api-skeleton Goto Github PK

View Code? Open in Web Editor NEW
132.0 7.0 32.0 403 KB

Useful skeleton for RESTful API development with PHP and Slim 4.

Home Page: http://bit.ly/2nNNOZi

License: MIT License

Dockerfile 2.03% PHP 97.97%
php slim-micro-framework rest-api mysql api-skeleton slim

slim4-api-skeleton's Introduction

SLIM 4 - API SKELETON

Useful skeleton for RESTful API development, using Slim PHP micro-framework.

Software License Build Status Coverage Status Packagist Version

๐Ÿ’ป TECHNOLOGIES USED:

The main technologies used in this project are:

  • PHP 8
  • Slim 4
  • MySQL
  • PHPUnit
  • dotenv
  • Docker
  • Docker Compose

โš™๏ธ QUICK INSTALL:

Requirements:

  • Composer.
  • PHP >= 8.1
  • MySQL/MariaDB.
  • or Docker.

With Composer:

You can create a new project running the following commands:

composer create-project maurobonfietti/slim4-api-skeleton [my-api-name]
cd [my-api-name]
composer test
composer start

Configure your connection to MySQL Server:

By default, the API uses a MySQL database.

You should check and edit this configuration in your .env file:

DB_HOST='127.0.0.1'
DB_NAME='yourMySqlDatabase'
DB_USER='yourMySqlUsername'
DB_PASS='yourMySqlPassword'
DB_PORT='3306'

With Docker:

If you like Docker, you can use this project with Docker and Docker Compose.

Minimal Docker Version:

  • Engine: 18.03+
  • Compose: 1.21+

Docker Commands:

# Create and start containers for the API.
docker-compose up -d --build

# Checkout the API.
curl http://localhost:8081

# Stop and remove containers.
docker-compose 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.
  • slim/psr7: PSR-7 implementation for use with Slim 4.
  • pimple/pimple: A small PHP dependency injection container.
  • vlucas/phpdotenv: Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically.

LIST OF DEVELOPMENT DEPENDENCIES:

๐Ÿ”– ENDPOINTS:

BY DEFAULT:

  • Hello: GET /

  • Health Check: GET /status

๐Ÿ“น TUTORIAL:

Learn how to develop a RESTful API with PHP and Slim 4 by following this video [๐ŸŽฅ ๐Ÿ”ˆ ๐Ÿ‡ช๐Ÿ‡ธ ๐Ÿ‡ฆ๐Ÿ‡ท]

Desarrollar.una.API.RESTful.con.PHP.y.Slim.4.mp4

โค๏ธ SUPPORT THE PROJECT

If you would like to support this project, you can:

  • Invite the author a coffee โ˜• ๐Ÿ˜‹
  • Give a star to the repository โญ ๐Ÿ˜Š

ko-fi

๐Ÿ˜Ž AND THAT'S IT!

Now, go to build an excellent RESTful API.

slim4-api-skeleton's People

Contributors

dependabot[bot] avatar dlzer avatar gitvitox avatar goldrak avatar maurobonfietti avatar mrevjd avatar toniop99 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

slim4-api-skeleton's Issues

Role based authorization

Hello, I hope all is well with you.

What is the best way to implement a role based authentication and authorization system in slim4-api-skeleton

My app will have three different users - (admin, Author,Editor ...).
I want to configure allowed groups to a route ?

Thanks

Integrate Phinx or other migration database system

Hi,

I think is a good idea integrate phinx or other migration database system to management the database and change the unit test to sqlite memory for user CI/CD more easy.

I already have phinx set, if I can integrate the unit tests well I perform a pull request, but it is costing me more than I thought.

Good Skeleton

About running the docker compose

I'm a beginner in docker beginner and try to using docker compose start up the project and check the db

but when I add a view for check db in the docker-compose file,

but it will display error message when I login

What problem in the docker compose file?

Ps.

  1. localhost:8080/
  2. 127.0.0.1:8080/
    1 and 2 not work go to page then login still show the error

Thanks.

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve
2
1

3

slim4-api-skeleton as docker container without mysql in container

Hello,

i'm a beginner in docker.
This is my first test with slim4-api-skeleton.
I will use the slim4-api-skeleton in a container.
When i start docker-compose i see in the container is a maysql database.
Is there a way to use a external mysql database ?

Your sincerly

Stephan

How to handle with exceptions?

Hello do you have any example for exceptions?

im using this code buts its not catching the ReportsException its catching the pdo exception on the first $statement->execute(); when i force a SQLSTATE[23000]: Integrity constraint violation error

 public function create(object $boletins)
    {
        try {
            $this->getDb()->beginTransaction();

            $query = 'INSERT INTO `reports` (`id`) VALUES (:id)';
            $statement = $this->getDb()->prepare($query);
            $statement->bindParam('id', $reports->id);
   	
            $statement->execute(); //fails here SQLSTATE[23000]: Integrity constraint violation error

            $id_report = $this->getDb()->lastInsertId();

            foreach ($reports->votes as $vote) {
                $query = 'INSERT INTO `votes` (`id`, id_report) VALUES (:id, :id_report)';
                $statement = $this->getDb()->prepare($query);
                $statement->bindParam('id', $vote->id);
                $statement->bindParam('id', $id_report );
            	$statement->execute();
            }

            $this->getDb()->commit();
            return $this->checkAndGet((int) $id_report);

        } catch(PDOException $e) {
            $this->getDb()->rollBack();
            throw new ReportsException('Error custom',  401);
        }
    }

composer test not working

.F. 3 / 3 (100%)

Time: 00:00.045, Memory: 6.00 MB

There was 1 failure:

  1. Tests\integration\HomeControllerTest::testStatus
    Failed asserting that 500 matches expected 200.

D:\xampp\htdocs\PW_project\tests\integration\HomeControllerTest.php:28

FAILURES!
Tests: 3, Assertions: 10, Failures: 1.
PHP Warning: Version warning: Imagick was compiled against ImageMagick version 1799 but version 1802 is loaded. Imagick will run but may behave surprisingly in Unknown on line 0
Script phpunit handling the test event returned with error code 1

Add an option for setBasePath

In order to suit different use cases :

  • for people installing their API project in a sub-folder (either for production or even more for a dev/test environment)
  • for people deporting only content of "public" folder to the exposed htdocs folder of the vhost, while the rest of the application is located in a parent folder (not exposed).

it would be interesting to add $app->setBasePath(xxx) where the basepath could be defined in .env file. Note: just after after $app = AppFactory::create in src/App/App.php

Abandoned Packages Updated

Great package to get started with slim 4 and APIs.

While setting up this I noticed the following errors:

Package object-calisthenics/phpcs-calisthenics-rules is abandoned, you should avoid using it. Use symplify/phpstan-rules instead.
Package sebastian/finder-facade is abandoned, you should avoid using it. No replacement was suggested.

These were dependencies of nunomaduro/phpinsights v1.14.1
On researching I found that nunomaduro/phpinsights has made some changes and has now released v2.0.1

I have updated the composer.json as shown below:
"nunomaduro/phpinsights": "^2.0",

Basic tests seem to work.

require_once change in composer.json

Hi

And why use require_once - many times and everywhere? If you can specify a folder in composer, composer will apply all these files, including configuration files

automatic generation of a Swagger / OpenAPI JSON file

Hi there,

I have started to deal with this package. I want to create a rest of the API quickly.
Unfortunately I miss one feature. Is it possible with
slim4-api-skeleton to create a Swagger / OpenAPI JSON file?

Kind regards

Stephan

Issue in tests

curl http://localhost:8081/status
works as expected but on composer test ;

  1. Tests\integration\HomeTest::testGetStatus
    Failed asserting that 500 matches expected 200.

----- update

My bad.. I needed to run the test from the php container

Undefined property: stdClass::

Hi!

I tried the tutorial "Desarrollar una API RESTful con PHP y Slim 4" but a got a error when I use compose test after creating the player and teams endpoints.

There were 2 errors:                                                                                                                                                                          
                                                                                                                                                                                              
1) Tests\integration\PlayerTest::testCreate                                                                                                                                                   
Undefined property: stdClass::$id                                                                                                                                                             
                                                                                                                                                                                              
/var/www/site/tests/integration/PlayerTest.php:25                                                                                                                                             
                                                                                                                                                                                              
2) Tests\integration\TeamTest::testCreate                                                                                                                                                     
Undefined property: stdClass::$id                                                                                                                                                             
                                                                                                                                                                                              
/var/www/site/tests/integration/TeamTest.php:25

Any Ideas?

Thank so much!

composer test no arranca, Tests: 4/4

Buenas tengo este problema, tengo ya montado la base de datos en wamp en el localhost:82 y
he puesto tambien la db en .env:
DB_HOST='localhost:82'
DB_NAME='bdfutbol'
DB_USER='root'
DB_PASS=''

Pero sigue saliendo este problema

PHPUnit 9.4.2 by Sebastian Bergmann and contributors.

.F.. 4 / 4 (100%)

Time: 00:00.083, Memory: 6.00 MB

There was 1 failure:

  1. Tests\integration\HomeTest::testGetStatus
    Failed asserting that 500 matches expected 200.

C:\Users\assi\api\tests\integration\HomeTest.php:30

FAILURES!
Tests: 4, Assertions: 11, Failures: 1.
Script phpunit handling the test event returned with error code 1

Error , could not find driver

Hello, when i call
php console api:generate:endpoints test
i get the message

could not find driver

Who can help.

Your sincerly

Stephan

Adding monolog support?

Hello. I've been trying to add monolog to handle errors (rather than returning them in the response), and haven't had much luck -- If I add monolog to the container, how do I access the container in the error handler in order to log an error?
Any help is appreciated.

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.