Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony: pass parameter between actions (with a redirect)

I am redirecting from one action (executeProcess) to another (executeIndex). I want to be able to pass a parameter/variable along, without using GET (e.g. $this->redirect('index', array('example'=>'true')))

Is there a way I can directly pass parameters without having it directly displayed in the URL? (e.g. POST). thanks.

like image 634
whamsicore Avatar asked Mar 08 '11 07:03

whamsicore


People also ask

How to redirect to a route instead of path in Symfony?

Because you are redirecting to a route instead of a path, the required option is called route in the redirect () action, instead of path in the urlRedirect () action. The feature to keep the request method when redirecting was introduced in Symfony 4.1. The redirections performed in the previous examples use the 301 and 302 HTTP status codes.

How to read query parameters in Symfony controller?

What if you need to read query parameters, grab a request header or get access to an uploaded file? That information is stored in Symfony's Request object. To access it in your controller, add it as an argument and type-hint it with the Request class:

How do I pass a service to a controller in Symfony?

If you need a service in a controller, type-hint an argument with its class (or interface) name. Symfony will automatically pass you the service you need:

What is the permanent switch in Symfony?

The permanent switch tells the action to issue a 301 HTTP status code instead of the default 302 HTTP status code. Assume you are migrating your website from WordPress to Symfony, you want to redirect /wp-admin to the route sonata_admin_dashboard. You don't know the path, only the route name. This can be achieved using the redirectAction () action:


1 Answers

The best way of passing a variable between two Actions is by using FlashBag

public function fooAction() {
    $this->get('session')->getFlashBag()->add('baz', 'Some variable');
    return $this->redirect(/*Your Redirect Code to barAction*/);
}

public function barAction() {
    $baz = $this->get('session')->getFlashBag()->get('baz');
}

To use the variable in Twig template use this --

{% for flashVar in app.session.flashbag.get('baz') %}
    {{ flashVar }}
{% endfor %}
like image 168
Pratyush Avatar answered Sep 26 '22 07:09

Pratyush