aalfiann/slim-jwt-skeleton

This is a skeleton to built rest api with slim framework 3 and JWT Auth.

1.1.0 2019-06-11 22:58 UTC

This package is auto-updated.

Last update: 2024-12-12 11:12:35 UTC


README

Version Downloads License

This is a skeleton to built rest api with slim framework 3 and JWT Auth.

Dependencies

  • 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

Installation

Install this package via Composer.

composer create-project aalfiann/slim-jwt-skeleton [my-app-name]

Getting Started

How to generate Token

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
}

How to test

Send request to https://yourdomain.com/api/

Method:
    GET / POST
Header:
    Content-Type: application/json
    X-Token: thisisyourjwttoken generated

How to create new application

  • Go to modules directory
  • Create new folder my_app
  • To create routes, you should follow this pattern >> *.router.php
  • Done

Example

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: