Symfony link_to in action

I need to give a link to user feedback (via the setFlash method). So, in my processForm() function, I want to use link_to , but this will not work due to strict MVC Symfony policies. I tried manually writing <a href='#">somelink</a> , but then printed as is.

What could be this way?

+4
source share
4 answers

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

So in your action:

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

Great for symfony MVC :)

To use this in a flash, you can do the following:

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

Then render as follows:

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

This last part displays any HTML objects in the output, so always make sure the data is safe. If it contains any user input, you must make sure that you filter it.

+6
source

The $ url = $ this-> generateUrl () method is really what you need.

For the current situation, I believe that there is a better approach. You can set the flag only if the current operation is successful:

 // in your action $this->getUser()->setFlash('success', 1); 

Then, in your opinion, you can check this flag and use UrlHelper to print 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.

+2
source

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

+1
source

This can be done by changing the part in which you print the flash message.

for instance

You have this piece of code:

Controller:

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

In your opinion:

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

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

In your opinion:

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

You can find more information about screens in Symfony here: http://www.symfony-project.org/api/1_4/sfOutputEscaper

0
source

Source: https://habr.com/ru/post/1386081/


All Articles