【问题标题】:What is wrong with this route?这条路线有什么问题?
【发布时间】:2011-02-05 18:44:18
【问题描述】:

嘿。 我打算为我的菜单组件创建一个“当前”类,用于创建菜单。要查找当前课程,建议使用$this>getContext()->getRouting()->getCurrentRouteName();。为此,我必须为将要存在的所有菜单元素设置路线。

假设到目前为止我有两个元素,一个用于首页,一个用于客户。每当我浏览首页时,无论什么动作和参数,我都希望上面的代码返回“首页”。客户也是如此。

我现在对客户的规则如下: 路由.yml

顾客: 参数:{模块:客户}

我认为设置 url 并不重要,我已经指定模块为客户。无论使用何种操作,菜单元素都应具有“当前”类。

我如何链接:

if ($current == $name){ echo link_to($name, $link, array("class" => "selected")); }别的 { 回声链接到($名称,$链接); }

现在转到“customer/some_action”的链接 ($link) 不起作用,它们仅显示为“/”。所以我想在 $link 周围使用 url_for,这也不起作用,同样的结果。

我的路由规则一定有问题...因为当我手动输入 /customer/new 时,我可以看到该页面。如果我从模板中写出$this>getContext()->getRouting()->getCurrentRouteName();,它会显示默认值。所以我的规则绝对行不通。我也尝试过其他规则,但没有人工作。诀窍是制定一个适用于模块 customer 下任何事物的规则。

是的,我在更改每条规则后都会清除缓存。

非常感谢。

编辑: 我使路由规则起作用,但不适用于客户模块下的所有操作。我必须为每个动作制定规则。但是,链接仍然断开,它们显示“/”。

我的新规则:

客户索引: 网址:/:模块 参数:{模块:客户,操作:索引} 新客户: 网址:/:模块/:动作 参数:{模块:客户,操作:新}

这些链接都不起作用: echo link_to("Customers", "@customer_index", array("module" => "customer"));

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

【问题讨论】:

    标签: symfony1 url-routing


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 2012-10-20
      • 2011-04-15
      • 1970-01-01
      • 2020-02-14
      • 2014-10-07
      • 2014-11-26
      • 2016-05-08
      • 2011-01-25
      • 1970-01-01
      相关资源
      最近更新 更多