The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim 3 pass variables from middleware

Sam's Avatar

Sam

05 Nov, 2015 11:17 PM

In Slim 3, how do I pass internal variables from middleware to the route it was added to? Or create an app registry where I can set variables in middleware and access them from the get/post functions.

  1. 1 Posted by Rob Allen on 06 Nov, 2015 07:09 AM

    Rob Allen's Avatar

    The easiest way is to use the request's attributes:

    In middleware:

    $request = $request->withAttribute('foo', 'bar');
    

    In route callback:

    $foo = $request->getAttribute('foo');
    
  2. 2 Posted by Sam on 06 Nov, 2015 04:53 PM

    Sam's Avatar

    Rob,
    That is very easy. Thank you. I couldn't find that method anywhere in the docs. This info might be very helpful to add to the new docs for new people like myself. Perhaps in the Request Helpers section (http://www.slimframework.com/docs/objects/request.html#request-helpers) or the Middleware section (http://www.slimframework.com/docs/concepts/middleware.html).

    Again, thank you,
    Sam

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

Generic

? 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