Slim: Get variable from middleware in slim 3

Created on 18 Nov 2015  路  4Comments  路  Source: slimphp/Slim

Hello. I m totally confused about how i should pass some user data from middleware to route...

I have made my first app and add the authorization middleware to some routes, that validate user cookies, get user data from database. But now, I want to use $user variable in route. How can I solve my problem?

here the code od middleware:

$mw = function ($request, $response, $next) use ($app, $db) {
        $user_id = Dflydev\FigCookies\FigRequestCookies::get($request, 'user_id')->getValue();
        $some_value = Dflydev\FigCookies\FigRequestCookies::get($request, 'some_value')->getValue();
        $user = call_user_func($app->getContainer()->get('get_user_by_id'), $user_id);

        if($user && $some_value == md5($user['user_id'].$user['mail'].$app->getContainer()->get('salt'))) {

            $response->write("user: ".json_encode($user)."<br>");
        } else {
            return $response->withRedirect($app->router->pathFor('login'));
        }
        $response = $next($request, $response);
        return $response;
    };
Slim 3 question

Most helpful comment

You can use something like:

$newRequest = $request->withAttribute('user', $user);
$response = $next($newRequest, $response);
return $response;

And you can get this with:

$request->getAttribute('user');

All 4 comments

There are probably many ways to do it. In some of my middlewares I include an callback function which can be used to store information outside of middleware scope. For example with slim-jwt-auth you can store the decoded token outside of middleware with something like:

$app->add(new \Slim\Middleware\JwtAuthentication([
    "secret" => "supersecretkeyyoushouldnotcommittogithub",
    "callback" => function ($request, $response, $arguments) use ($app) {
        $app->jwt = $arguments["decoded"];
    }
]));

Note that this is in not Slim 3 feature. Just my way of solving similar problem. I prefer generic callbacks. They are kind of swiss knife because different users have different needs. You might be interested in the code for handling callbacks.

You can use something like:

$newRequest = $request->withAttribute('user', $user);
$response = $next($newRequest, $response);
return $response;

And you can get this with:

$request->getAttribute('user');

You may also set variables into DIC, given that DIC is passed into middleware's constructor.

This is how I determine current locale inside the middleware, set it into settings array and then use in controllers/ actions to load appropriate items.

I use a combination of both, Request attribute and Container registration depending on the kind of data I want to pass:

  • If it's meant for another middleware I use request attribute and remove it when used
  • If it's meant for the route handler (callback or class) I tend to use the container

Mainly because this way route classes dependencies can be clearly declare rather than just use something coming 'magically' from the request

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Zyles picture Zyles  路  4Comments

lwiwala picture lwiwala  路  5Comments

enygma picture enygma  路  3Comments

basuke picture basuke  路  3Comments

xymz picture xymz  路  5Comments