Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware to route or group of routes #20

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from

Conversation

ybelenko
Copy link

It's great option for users who wants to add authentication to single route like:

/**
 * Restrict route example
 * Our token is "usertokensecret"
 */
$app->get('/restrict', function($request, $response){
    $output = ['message' => 'It\'s a restrict area. Token authentication works!'];
    return $response->withJson($output, 200, JSON_PRETTY_PRINT);
})->add(new TokenAuthentication([
    'authenticator' => $authenticator,
    'relaxed' => [
        'localhost',
        '127.0.0.1',
        'slim-token-authentication.local'
    ]
]));

This way you can change /restrict path and not modify middleware settings.
Only authenticator option required to restrict access to specified endpoint.

PHPUnit execution output:

$ composer test
> phpunit
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.7 with Xdebug 2.6.0
Configuration: C:\wamp64\www\slim-token-authentication\phpunit.xml
...........................................................       59 / 59 (100%)
Time: 490 ms, Memory: 6.00MB
OK (59 tests, 103 assertions)

When I add middleware directly to route or group of routes, then I
don't need to specify path and except params. Routing becomes more
clear and readable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant