4

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?

prongs
  • 9,422
  • 21
  • 67
  • 105

4 Answers4

6

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.

Grad van Horck
  • 4,488
  • 1
  • 21
  • 22
  • generateUrl is fine but How do I achieve an actual link?? as in `...` – prongs Dec 13 '11 at 11:15
  • That depends on how you are using it. Maybe you could add a bit of relevant code? You could just write it in the flash message. But when you do, you have to display it's raw value in the view (so use `ESC_RAW`), and have to make sure all text in the notice is "safe". – Grad van Horck Dec 13 '11 at 11:57
  • where do i use this `ESC_RAW` ? and what do you mean by `"safe"`? – prongs Dec 13 '11 at 12:22
  • See my updated answer. But again: if you add a bit of relevant code, we can help you more easily. – Grad van Horck Dec 13 '11 at 14:04
2

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.

Stef
  • 203
  • 3
  • 4
1

In your actions you can use $this->generateUrl(), which works much like link_to.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
0

This can be accomplished by modifying the part where you print out the flash message.

For example

You have this piece of code:

Controller part:

// Form save success.
$this->getUser()->setFlash('success', 'This is a ' . link_to('@myRouteName', 'link') . ' for testing.');

In your view:

<?php
if ($sf_user->hasFlash('success'):
  echo $sf_user->getFlash('success');
endif;
?>

As you can see the identifier of the flash message is success. This will print out the exact text you assigned to the flash variable. You can print out a link by using the getRawValue() function as such:

In your view:

<?php
if ($sf_user->hasFlash('success'):
  echo $sf_user->getRawValue()->getFlash('success');
endif;
?>

More information about output escaping in Symfony can be found here: http://www.symfony-project.org/api/1_4/sfOutputEscaper

Marco Bax
  • 383
  • 2
  • 6