2011-02-05 113 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();