中间件中的Slim PHP路由 [英] Slim PHP Route in Middleware

查看:95
本文介绍了中间件中的Slim PHP路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Slim中,是否可以在中间件中获取当前路由?

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?
  }
}

我知道您可以在调用slim.before.dispatch挂钩之后调用$app->router()->getCurrentRoute(),但是当您从中间件中调用它时,它将返回一个非对象.任何帮助将不胜感激.

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.

推荐答案

是,不是.如果查看Slim的源代码,则会看到在调用Slim::run方法时,按LIFO顺序调用已注册的中间件,然后Slim运行它自己的调用"方法,在该方法中开始处理请求. Slim通过这种方法解析并处理路由.在这种情况下,您将无法在Middleware::call方法中访问$app->router()->getCurrentRoute(),因为尚未对其进行解析和定义.

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 the Middleware::call method because it won't have been parsed and defined yet.

唯一的方法是在中间件内的slim.before.dispatch上注册一个侦听器,并实现您想在该方法中执行的任何操作.

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:

class AuthMiddleware extends \Slim\Middleware
{
    public function call()
    {
        $this->app->hook('slim.before.dispatch', array($this, 'onBeforeDispatch'));

        $this->next->call();
    }

    public function onBeforeDispatch()
    {
        $route = $this->app->router()->getCurrentRoute();

        //Here I check if the route is "protected" some how, and if it is, check the
        //user has permission, if not, throw either 404 or redirect.

        if (is_route_protected() && !user_has_permission())
        {
            $this->app->redirect('/login?return=' . urlencode(filter_input(INPUT_SERVER, 'REQUEST_URI')));
        }
    }
}

在此示例中,将在调用路由处理程序之前运行onBeforeDispatch方法.如果您查看源代码,则可以看到事件在try/catch块内触发,该块正在侦听$app->redirect()$app->pass()等引发的异常.这意味着我们可以在此处实现检查/重定向逻辑就像这是一个路由处理程序函数一样.

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 a try/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.

is_route_protecteduser_has_permission仅仅是用来说明我的身份验证中间件如何工作的伪代码.我构造了该类,以便您可以在Middleware构造函数中为受保护的路由指定路由或正则表达式的列表,并传递实现用户权限检查的服务对象,等等.希望对您有所帮助.

Above is_route_protected and user_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.

这篇关于中间件中的Slim PHP路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆