2011-05-25 96 views
1

在symfony中,我可以使用哪种方法在我的路由上进行反向查找以确定URL指向的模块和操作?有没有一种方法来获取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.) 

谢谢!

+0

在freenode的#symfony中,我注意到symfony在满足请求时已经基本做到了这一点!看起来好像没有最好的实践方式来自己获取这些信息。也许像$ context-> getRouting() - >匹配('url')? – 2011-05-25 22:51:05

+0

我已就此问题打开以下票证:http://trac.symfony-project.org/ticket/9800 – 2011-05-25 23:06:30

回答

3

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

虽然这不是100%我的解决方案,但它非常接近,可能会更接近直到他们在symfony API中创建了一些东西。 – 2011-05-25 23:46:45

相关问题