开发者

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

In symfony, is there a method that I can use which does a reverse lookup on my routes to determine the module and action a URL points to?

Say, something like:

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

Bear in mind, I don't want to perform simple string-hacking to do this as there may be mappings that are not quite so obvious:

get_module("/");  // (What this retu开发者_运维问答rns is entirely dependent on the configured routes.)

Thanks!


Use the sfRouting class to match URLs. This is part of the sfContext object. Your code (in an action) would look like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜