In Slim is it possible to get the current route within middleware?
class Auth extends \Slim\Middleware{
public function call(){
$currentRoute = $this->app->getRoute(); // Something like this?
}
}
I know you can call $app->router()->getCurrentRoute()
after the slim.before.dispatch
hook is called, but when you call this from middleware it returns a non-object. Any help would be greatly appreciated.
You should use app->request()->getPathInfo() instead of app->getRoute().
There is an alternative method of doing this, as I've been in the same situation. What I wanted to avoid was matching anything by route and wanted to use route names instead, so you could try the following:
You could even have an array of routes you DON'T want the middleware to run on and then just check if it's in_array() etc and if not, do what you need to.
Yes and no. If you look at the source code for Slim, you will see that registered Middlewares are called in LIFO order when the
Slim::run
method is called, and then Slim runs it's own "call" method where the processing of the request begins. It is in this method that Slim parses and processes the route. In which case, you cannot access$app->router()->getCurrentRoute()
in theMiddleware::call
method because it won't have been parsed and defined yet.The only way to do this is to register a listener on
slim.before.dispatch
inside your Middleware, and implement whatever you want to do in that method.From the name of your class I assume you are trying to create a basic authentication module? I've done something similar to this before, and it went something like this:
In this example, the
onBeforeDispatch
method will be run before of the route handlers are invoked. If you look at the source code, you can see the events are fired inside atry/catch
block that is listening for the exceptions thrown by$app->redirect()
and$app->pass()
, etc. This means we can implement our check/redirect logic here just as if this was a route handler function.Above
is_route_protected
anduser_has_permission
are just pseudo-code to illustrate how my auth middleware worked. I structured the class so that you could specify a list of routes or regex for routes in the Middleware constructor that were protected, as well as passing a service object that implemented the user permission checking, etc. Hope this helps.