有没有一种方法可以获取URL引用的模块/操作


Is there a method to get the module/action a URL refers to?

在symfony中,有没有一种方法可以用来对路由进行反向查找,以确定URL指向的模块和操作?

比如:

get_module("http://host/cars/list"); // ("cars")
get_action("http://host/frontend_dev.php/cars/list"); // ("list")

请记住,我不想执行简单的字符串破解来做到这一点,因为可能存在不太明显的映射:

get_module("/");  // (What this returns is entirely dependent on the configured routes.)

谢谢!

使用sfRouting类匹配URL。这是sfContext对象的一部分。你的代码(在操作中)看起来是这样的:

public function executeSomeAction(sfWebRequest $request)
{
  if($params = $this->getContext()->getRouting()->parse('/blog'))
  {
     $module = $params['module']; // blog
     $action = $params['action']; // index
     $route  = $params['_sf_route'] // routing instance (for fun)
  }
  else
  {
     // your URL did not match
  }
}