2011-02-05 63 views
0

嘿。 我打算爲我的菜單組件創建一個「當前」類,它創建菜單。爲了找出當前的課程,建議使用$this>getContext()->getRouting()->getCurrentRouteName();。爲此,我必須爲所有將要存在的菜單元素設置路線。這條路線有什麼問題?

可以說我到目前爲止有兩個元素,一個用於frontpage,另一個用於客戶。每當我瀏覽首頁時,重要的是什麼操作和參數,我希望上面的代碼返回「frontpage」。顧客也一樣。

我現在的客戶規則如下: 的routing.yml

 
customer: 
    param: { module: customer } 

我認爲設立網址並不重要,我已經指定的模塊以客戶。無論使用何種操作,菜單元素都應該具有「當前」類。

我如何鏈接:

 
if ($current == $name){ 
    echo link_to($name, $link, array("class" => "selected")); 
}else { 
    echo link_to($name, $link); 
} 

鏈接($鏈接),現在去 「客戶/ some_action」 不工作,他們顯示爲只是 「/」。所以我想在$ link周圍使用url_for,這也不起作用,結果相同。

我的路由規則肯定有問題......因爲當我輸入讓手動說/客戶/新的,我可以看到頁面。如果我從模板中寫出$this>getContext()->getRouting()->getCurrentRouteName();,它表示默認。所以我的規則根本不起作用。我也嘗試了其他規則,但沒有任何人工作。訣竅是使一個規則適用於模塊客戶下的任何東西..

是的,我改變了我的每個規則後清除我的緩存。

非常感謝。

編輯: 我做了路由規則的工作,但不是爲客戶模塊下的所有行動。我必須爲每個行動制定規則。但是,鏈接仍然斷開,它們顯示「/」。

我的新規則:

 
customer_index: 
    url: /:module 
    param: { module: customer, action: index } 

customer_new: 
    url: /:module/:action 
    param: { module: customer, action: new } 

這些鏈接都沒有工作: echo link_to("Customers", "@customer_index", array("module" => "customer"));

echo link_to("New customer", "@customer_new", array("module" => "customer"));

回答

1

是的,這是多麼我已經做了幾次爲好。您需要單獨的路由規則來使用getCurrentRouteName()。

您對鏈接的問題是您無法以這種方式傳遞「模塊」參數。這不是Symfony link_to() helper的有效參數。取而代之的是,你可以通過這種方式:

link_to('Customers', '@customer_index?url_variable=something'); 

customer_index: 
    url: /:url_variable 
    param: { module: customer, action: index, url_variable: some_default_value } 

默認值是可選的,但的link_to助手將拋出一個錯誤,如果你不傳遞變量和路徑沒有爲它的默認值。

而且,只是指出了這一點,你已經得到它上面的代碼錯兩次:

$this>getContext()->getRouting()->getCurrentRouteName(); 
...should be: 
$this->getContext()->getRouting()->getCurrentRouteName();