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

Allow access to $app from within authenticator, error #25

Closed
sivann opened this issue Jul 23, 2016 · 4 comments
Closed

Allow access to $app from within authenticator, error #25

sivann opened this issue Jul 23, 2016 · 4 comments

Comments

@sivann
Copy link

sivann commented Jul 23, 2016

Useful in many cases to have access to $app and container. E.g. In error, it would be useful to have access to the renderer ($app->getContainer()->get('view') in order to render an error page.

In authenticator, also useful to access app's Model methods, settings, etc.

@tuupola
Copy link
Owner

tuupola commented Jul 23, 2016

Sorry I do not understand. Can you show some example code what you are trying to achieve.

@sivann
Copy link
Author

sivann commented Jul 24, 2016

I may be wrong about it, but here's the error part definition. How could I access $app without using "global"?

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "error" => function ($request, $response, $arguments) { //on 401
        global $app; //ugly, but how else without modifying the module..

        $container = $app->getContainer();
        $renderer=$container->get('view');

        $renderer->render($response,'gotohome.twig',array('uri'=>$request->getUri(),'session'=>$_SESSION,'message'=>$arguments['message']) );

        return $response;
    }
...

Probably the __construct of HttpBasicAuthentication could take $app as argument, and also pass it as arguments to its callbacks.

@tuupola
Copy link
Owner

tuupola commented Jul 25, 2016

PHP has the use keyword which can be used for passing variables to closures.

$app = new \Slim\App;

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "error" => function ($request, $response, $arguments) use ($app) {
        /* do something with $app */
    }
]));

@sivann
Copy link
Author

sivann commented Jul 26, 2016

I've completely forgotten about that. Thanks a lot for your time and advice !

@sivann sivann closed this as completed Jul 26, 2016
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

No branches or pull requests

2 participants