Symfony's link_to helper method ignores the third argument
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 i开发者_开发知识库n 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.
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
.
Try something like this:
echo link_to('Sample', 'sample', array(), array('class' => 'actualSection'));
The first array - options for route, and second - html's attributes
精彩评论