The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Pass params from middleware to next route

Mike Orland's Avatar

Mike Orland

16 Jan, 2016 03:35 AM

Is there a way in slim 3.x to pass arguments from my middleware to the up following route?
Something like:

next($request, $response, $customParams);

  1. 1 Posted by Rob Allen on 16 Jan, 2016 04:29 AM

    Rob Allen's Avatar

    You're looking for $request->setAttribute()

  2. 2 Posted by Mike Orland on 16 Jan, 2016 02:33 PM

    Mike Orland's Avatar

    Thanks, did you guys hide that in the docu, can't find it on

  3. 3 Posted by Rob Allen on 16 Jan, 2016 03:06 PM

    Rob Allen's Avatar

    There's a good chance that it's not documented. We'd love a PR against fixing this :)

  4. 4 Posted by Mike Orland on 17 Jan, 2016 12:03 AM

    Mike Orland's Avatar

    Took a brief look into the request.php. There is no such function, setAttribute. Only getAttribute is present!

  5. 5 Posted by Rob Allen on 17 Jan, 2016 12:45 AM

    Rob Allen's Avatar

    It's PSR-7, so it'll be withAttribute() as the Request is immutable.

  6. 6 Posted by Buddy Yaussy on 21 Jan, 2016 05:31 PM

    Buddy Yaussy's Avatar

    I used writeAttribute to pass user information through to the app layer after authenticating via middleware:

    public function __invoke($request, $response, $next)
        $authKeys = $request->getHeader('Authorization');
        $user = $db->getUserFromAuthKey($authKeys[0]);
        if (/*user is authorized, yada yada yada*/) {
            $response = $next($request->withAttribute("User_id",$user['id']), $response);
        } else {
            // not authorized stuff goes here
            return $response->withStatus(400);
        return $response;

  7. 7 Posted by Buddy Yaussy on 21 Jan, 2016 06:50 PM

    Buddy Yaussy's Avatar

    Sorry, there is a typo there, and I can't edit.

    I used withAttribute() ...

    And then of course, within the app, the attribute can be retrieved with:

        $user_id = $request->getAttribute("User_id");

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:


Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac