symfony link_to in action

prongs picture prongs · Dec 13, 2011 · Viewed 7.7k times · Source

I need to give a link in user feedback(via setFlash method). So In my processForm() function, I want to use link_to but that won't work due to symfony's strict MVC policies. I tried manually writing <a href='#">somelink</a> but then that printed as it is.

What can be a way around that?

Answer

Grad van Horck picture Grad van Horck · Dec 13, 2011

You can access the "routing" in your controller. In fact, it has a shortcut method:

So in your action:

$url = $this->generateUrl('your_route_name', array(/* parameters */));

Perfectly valid for Symfony MVC :)

To use this in your flash, you could do the following:

$this->getUser()->setFlash('success_raw', 'Click <a href="'.$url.'">here</a>');

Then render in your view like this:

echo $sf_user->getFlash('success_raw', ESC_RAW);

This last part renders any HTML entities in the output, so always make sure the data is safe. If it contains any user input, you should make sure you filtered it.