This is a skeleton to built rest api with slim framework 3 and JWT Auth.
- Logger >> monolog/monolog
- HTTP Cache >> slim/http-cache
- Slim JWT Auth >> tuupola/slim-jwt-auth
- Cors Middleware >> tuupola/cors-middleware
- ETag Middleware >> aalfiann/slim-etag-middleware
Install this package via Composer.
composer create-project aalfiann/slim-jwt-skeleton [my-app-name]
Send request to https://yourdomain.com/api/generate
Method:
GET / POST
Header:
Content-Type: application/json
Body:
{
"userid":"",
"scope":["get","post","delete","put"]
}
Output Response:
{
"token":"This is jwt token",
"expire" 1557908861
}
Send request to https://yourdomain.com/api/
Method:
GET / POST
Header:
Content-Type: application/json
X-Token: thisisyourjwttoken generated
- Go to modules directory
- Create new folder
my_app
- To create routes, you should follow this pattern >>
*.router.php
- Done
This is just the part code of my_app.router.php
file,
please take look at modules/my_app/my_app.router.php
for more detail.
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// Route for /my_app
$app->group('/my_app', function($app) {
// Show index page
// Try to open browser to http://yourdomain.com/my_app/
$app->get('/', function (Request $request, Response $response) {
$data = [
'welcome' => 'Hello World, this is my_app index page.',
'message' => 'This is my first app rest api with slim-jwt-skeleton.'
];
return $response->withJson($data,200,JSON_PRETTY_PRINT);
})->setName("/my_app/");
});
Note:
- Documentation about
Slim
is available on slimframework.com. - This is a forked version from the original slimphp/Slim-Skeleton.