shangab / slim-swagger
Automatic SWAGGER for slim projects, wroks as middleware.
Installs: 125
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/shangab/slim-swagger
README
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:
ShangabSlimSwaggerThe middleware for the swagger openapi and ui.ShangabJWTAuthThe middleware for authenticated routes or group of routes.ShangabJWTUtilA utility class that have four public functions, to handle authorization usingshangab/slim-swaggeryou should use this utility class:public function createToken($userData): stringGiven a user json object it will generates JWT token.public function verifyToken(): mixedThis will verify the headerAuthorizationbearer token and returns the user data or false if not authenticated.public function getTempPassword($length = 5): stringThis fuction will return a temporary password, default length is 5.public function getHash256($text): stringThis 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/.
composer require shangab/slim-swagger
Step 2: How to use ShangabJWTUtil
Use this class to:
- Create
JWTtokens 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:
use Shangab\Middleware\ShangabJWTUtil; $jwtUtil = new ShangabJWTUtil;
2.1 To crate a JWT token:
$user =[LOGIN THE USER WITH YOUR CONTROLLER CODE]; $jwtToken = $jwtUtil->createToken($user);
2.2 To verify a JWT token, though the middleware does it for you, but f you want to:
$verified = $jwtUtil->verifyToken(); // token will be read from the request headers.
2.3 To generate a temporary password:
$password = $jwtUtil->getTempPassword(8);
2.4 To hash any text:
$hashed = $jwtUtil->getHash256("Hello World!");
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.
use Shangab\Middleware\ShangabSlimSwagger; use Shangab\Middleware\ShangabJWTAuth;
$app = AppFactory::create(); $app->add(new ShangabSlimSwagger($app, 'Shangab Slim Swagger', '1.0.1', 'Api for Shangab Slim Swagger.')); // Protected endpoins same group: "users" $app->group('/users', function ($app) use ($container) { $app->post('/add', function (Request $request, Response $response, $args) use ($container) { $body = $request->getBody()->getContents(); $user = json_decode($body, true); $container['data']['users'][] = $user; $users = $container['data']['users']; $response->getBody()->write(json_encode(['status' => true, 'message' => 'User addded', 'users' => $users])); return $response->withHeader('Content-Type', 'application/json'); }); $app->put('/update', function (Request $request, Response $response, $args) use ($container) { $body = $request->getBody()->getContents(); $user = json_decode($body, true); $key = array_search($user['id'], array_column($container['data']['users'], 'id')); $container['data']['users'][$key] = $user; $users = $container['data']['users']; $response->getBody()->write(json_encode(['status' => true, 'message' => 'User updated', 'users' => $users])); return $response->withHeader('Content-Type', 'application/json'); }); $app->delete('/delete/{id}', function (Request $request, Response $response, $args) use ($container) { $id = $args['id']; $users = array_values(array_filter($container['data']['users'], function ($user) use ($id) { return $user['id'] != $id; })); $response->getBody()->write(json_encode(['status' => true, 'message' => 'User deleted', 'users' => $users])); return $response->withHeader('Content-Type', 'application/json'); }); })->add(new ShangabJWTAuth($app)); // All routes above this middleware will apply ShangabJWTAuth middleware protected routes. // Below routes will not be protected by ShangabJWTAuth middleware. // UnProtected endpoins same group: "users" $app->group('/users', function ($app) use ($container) { $app->get('/staff', function (Request $request, Response $response, $args) use ($container) { $users = array_values(array_filter($container['data']['users'], function ($user) { return $user['type'] == 'staff'; })); $response->getBody()->write(json_encode($users)); return $response->withHeader('Content-Type', 'application/json'); }); $app->get('/client', function (Request $request, Response $response, $args) use ($container) { $users = array_values(array_filter($container['data']['users'], function ($user) { return $user['type'] == 'client'; })); $response->getBody()->write(json_encode($users)); return $response->withHeader('Content-Type', 'application/json'); }); $app->get('/all', function (Request $request, Response $response, $args) use ($container) { $users = $container['data']['users']; $response->getBody()->write(json_encode($users)); return $response->withHeader('Content-Type', 'application/json'); }); }); $app->run();
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.
