0

我使用自定义路由,其中​​包括除了controlleraction之外的命名空间。因此,出于 ACL 的目的,我使用 MVC 路由名称作为 ACL 资源名称。现在我需要获取当前的DISPATCHING路由名称。我想出的唯一解决方案是从 Dispatcher 获取命名空间/控制器/操作并遍历所有路由以找到合适的路由。

有没有最简单的方法来获取当前调度(不仅仅是匹配)的路线名称?

4

2 回答 2

5

相当容易

\Phalcon\DI::getDefault()->get('router')->getMatchedRoute()->getName();
于 2014-06-05T17:47:02.527 回答
0

You can use your router, dispatcher and base controller to get what you need. Consider this:

$router = new \Phalcon\Mvc\Router(false);

$routes  = array(
    '/{namespace:"[a-zA-Z]+}/:controller' => array(
        'controller' => 2,
    ),
    '/{namespace:"[a-zA-Z]+}/:controller/:action/:params'       => array(
        'controller' => 2,
        'action'     => 3,
        'params'     => 4,
    ),
);

foreach($routes as $route => $params) {
    $router->add($route, $params);
}

Now in your base controller you can do this:

public function getNamespace()
{
    return $this->dispatcher->getParam('namespace');
}

This way you can have the namespace currently being served in your controllers (so long as they extend your base controller).

If you need to get the namespace in a model you can always use the DI like so (base model):

public function getNamespace()
{
    $di = \Phalcon\DI::getDefault();

    return $di->dispatcher->getParam('namespace');
}
于 2013-07-02T17:14:45.560 回答