Symfony 的 link_to 辅助方法忽略第三个参数
我正在尝试使用 Symfony 路由系统在导航菜单中生成与环境无关的链接。我在routing.yml中有以下路由:
# Navigation menu rules
sample:
class: sfDoctrineRouteCollection
options: { model: Sample }
# Default rules (catch all)
homepage:
url: /
param: { module: sample, action: index }
default_index:
url: /:module
param: { action: index }
default:
url: /:module/:action/*
在导航菜单中,我对link_to
帮助器方法进行了以下调用,根据实际部分进行了自定义:
<?php
...
if ( $actualSection === 'sample' )
echo link_to('Sample', 'sample', array('class' => 'actualSection'));
else
echo link_to('Sample', 'sample');
...
?>
问题是link_to
正在生成类似以下的链接:
<a href="/sample?class=actualSection">Samples</a>
而不是
<a href="/sample" class="actualSection">Samples</a>
What is the right way to use the link_to
helper method with the Routing System to generated the back link?
根据 link_to 文档 内部 URI 应该写在 'module/action' 中格式,所以我猜最初的 /
可以省略。
I'm trying to generate environment-independent links in a navigation menu with Symfony routing system. I have the following routes in routing.yml
:
# Navigation menu rules
sample:
class: sfDoctrineRouteCollection
options: { model: Sample }
# Default rules (catch all)
homepage:
url: /
param: { module: sample, action: index }
default_index:
url: /:module
param: { action: index }
default:
url: /:module/:action/*
And in the navigation menu, I have the following calls to the link_to
helper method, customized according to the actual section:
<?php
...
if ( $actualSection === 'sample' )
echo link_to('Sample', 'sample', array('class' => 'actualSection'));
else
echo link_to('Sample', 'sample');
...
?>
Problem is that link_to
is generating links like:
<a href="/sample?class=actualSection">Samples</a>
instead of
<a href="/sample" class="actualSection">Samples</a>
What is the correct way to use the link_to
helper method with the routing system in order to generate the latter link?
According to link_to documentation internal URIs should be written in 'module/action' format, so I guess the initial /
can be omitted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的第二个论点似乎不正确。要么输入以“@”为前缀的路由名称(例如
@sample_index
或 sfDoctrineCollection 生成的任何路由),要么使用“模块/操作格式”=>样本/索引
。Your second argument seems to be incorrect. Either you put the name of a route, prefixed by '@' (like
@sample_index
or whatever route sfDoctrineCollection generates), either you use the "module/action format" =>sample/index
.尝试这样的操作:
第一个数组 - 路由选项,第二个数组 - html 的属性
Try something like this:
The first array - options for route, and second - html's attributes