How can I pass an attribute value between Middlewares?

I am using a middleware to get user’s IP (https://github.com/akrabat/rka-ip-address-middleware)

When I use $request->getAttribute(‘ip_address’); inside other custom middleware this returns NULL, but when I use it in a route handler it works perfectly.

What can i do?, I don’t wanna use globals or similar practices

In what order are you adding the middlewares?

Thank you Joe, your question was the solution…
I changed the order of the middleware and now it works.

Before

// don´t work $app->add(new RKA\Middleware\IpAddress(false, [])); $app->add(new Middlewares\ACL());

After

// works $app->add(new Middlewares\ACL()); $app->add(new RKA\Middleware\IpAddress(false, []));

This behavior works like FILO (First In Last Out), first to be defined and last to be evaluated.

1 Like

@diegoluisr I think the diagram on this page tries to describe how it works http://www.slimframework.com/docs/concepts/middleware.html but maybe at times its not clear enough.