Download the PHP package shangab/slim-swagger without Composer
On this page you can find all versions of the php package shangab/slim-swagger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shangab/slim-swagger
More information about shangab/slim-swagger
Files in shangab/slim-swagger
Package slim-swagger
Short Description Automatic SWAGGER for slim projects, wroks as middleware.
License MIT
Informations about the package slim-swagger
Slim Swagger Middleware
This is a Slim PHP middleware that automatically generates and serves Swagger (OpenAPI) documentation for your Slim PHP API routes. It supports dynamic route scanning, including GET, POST, PUT, PATCH, and DELETE methods, and generates detailed documentation without requiring external annotation libraries. This project was created for a project I am working on in php
. I used to do backend with Python FatAPI
or .NET
, which both have swagger UI embeded in them, out of the box. However in php
swagger it is tricky and time consuming.
I wanted the same automatic swagger in php
in my new project but the easiest way I found is using external packages such as zircote/swagger-php
which requires a lot of annotations if I got it right. I loved the fact that slim-php
is light, powerful and scalable, Therefore, I started to build this middleware. The first version, tag
, was built in a hurry in a single day, but the future roadmap will increase the middleware power and flexibility.
All the best wishes, use, recommend, star, fork, contribute, spread and enjoy it.
Features
- Automatic Swagger generation: Scans all Slim routes and generates OpenAPI documentation on the fly.
- Supports multiple HTTP methods: Handles GET, POST, PUT, PATCH, DELETE, and more.
- No external annotations: Does not rely on any library or any third-party annotation libraries like
zircote/swagger-php
. - Customizable: Easily extendable to add custom parameters, request bodies, and responses.
- MIT License: Open-source and free to use under the MIT License.
Requirements
- php 8.2
- "slim/slim": "^4.9"
- "slim/psr7": "^1.5"
The classes to be used
We now have three classes:
ShangabSlimSwagger
The middleware for the swagger openapi and ui.ShangabJWTAuth
The middleware for authenticated routes or group of routes.ShangabJWTUtil
A utility class that have four public functions, to handle authorization usingshangab/slim-swagger
you should use this utility class:public function createToken($userData): string
Given a user json object it will generates JWT token.public function verifyToken(): mixed
This will verify the headerAuthorization
bearer token and returns the user data or false if not authenticated.public function getTempPassword($length = 5): string
This fuction will return a temporary password, default length is 5.public function getHash256($text): string
This function takes any text and gives a 256 hash code.
Step 1: Installation
You can install this middleware in your Slim project via Composer from https://packagist.org/
.
Step 2: How to use ShangabJWTUtil
Use this class to:
- Create
JWT
tokens for logged-in users. - Verify that the user has valid tokens.
- Generate temporary passwords.
- Hash text as you see fit.
To use the ShangabJWTUtil
class in your code, see below example:
2.1 To crate a JWT
token:
2.2 To verify a JWT
token, though the middleware does it for you, but f you want to:
2.3 To generate a temporary password:
2.4 To hash any text:
Step 3: How to use The Middleware
To use the middleware follow the code below, declare the ShangabSlimSwagger
middleware and add it to your app in index.php
:
First list all the protected groups of endpoints or endpoints, then list the un-protected ones.
Please avoid using route names openapi
and docs
I use these two routes and serve them before the $app
routes, openapi
returns the OpenAPI Specs,
while docs
route returns the swagger UI shown above.