PHP code example of crtl / slim-auth-middleware

1. Go to this page and download the library: Download crtl/slim-auth-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crtl / slim-auth-middleware example snippets



use Crtl\AuthorizationMiddleware\BasicAuthorization;

$app = new \Slim\App();

$app->add(
    new BasicAuthorization([
        BasicAuthorization::CONFIG_ENABLE => true,
        BasicAuthorization::CONFIG_USER => "secret",
        BasicAuthorization::CONFIG_SECRET => "password"
    ])
);

$app->get("/", function($request, $response) use ($app) {
    $body = $response->getBody();
    $body->write("Authorized");
    
    return $response->withBody($body),
});


$app->run();



class CustomAuthorization extends \Crtl\AuthorizationMiddleware\AbstractAuthorization{
    
    protected function isAuthorized(): bool {
        
        /* @var \Psr\Http\Message\ResponseInterface $response */
        $response = $this->response;
        
        /* @var \Psr\Http\Message\RequestInterface $request */
        $request = $this->request;
        
        /* check if authorized */
        
        return true;
        
    }
    
}