Or, more generally, is there an idiomatic way of setting the active page when creating site navigation in Symfony?
Don't use sfContext::getInstance(), route names or internal uris for this. There are several ways to achieve navigation menu highlighting with Symfony, personnaly I like setting a request attribute (eg. in a controller), like this:
<?php
// ...
public function executeFoo(sfWebRequest $request)
{
$request->setAttribute('section', 'blah');
}
Then in your template:
<ul>
<li class="<?php echo 'blah' === $sf_request->getAttribute('section') ? 'active' : '' ?>">
<a href="<php echo url_for('@my_route') ?>">Blah</a>
</li>
</ul>
You can even add the section
parameter from your routes in the routing.yml
file:
my_route:
url: /foo
param: { module: foo, action: bar, section: blah }
Then in your template if you do so, be careful to check for a request parameter nstead of an attribute:
<ul>
<li class="<?php echo 'blah' === $sf_request->getParameter('section') ? 'active' : '' ?>">
<a href="<php echo url_for('@my_route') ?>">Blah</a>
</li>
</ul>
Simple yet efficient, right? But f you need more complex navigatioin (especially nested menus), you should be thinking using some more full-featured plugins or symfony-based CMSes like Sympal, Diem or ApostropheCMS.