2011-05-25 69 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

相關問題