Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony link_to in action

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?

like image 651
prongs Avatar asked Jan 19 '23 01:01

prongs


2 Answers

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.

like image 110
Grad van Horck Avatar answered Jan 31 '23 09:01

Grad van Horck


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.

like image 38
Stef Avatar answered Jan 31 '23 09:01

Stef