有没有办法获取 URL 引用的模块/操作?

发布于 2024-11-09 11:32:04 字数 387 浏览 5 评论 0原文

在 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.)

谢谢!

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 returns is entirely dependent on the configured routes.)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-11-16 11:32:04

使用 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
  }
}

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
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文