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.
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.
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:
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:
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:
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 %}
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