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?
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.
The $url = $this->generateUrl() method is really what you need.
For the current situation I think there is a better approach. You can only set a flag when the current operation was successful:
// in your action
$this->getUser()->setFlash('success', 1);
Then in your view, you can check against that flag and use the UrlHelper to print out the link:
<?php if ($sf_user->getFlash('success')): ?><br />
<?php echo link_to(__('My message'), '@my_route') ?><br />
<?php endif ?>
This way you can easily localize your message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With