Magento Redirect in Controller with Parameters

Magento controllers come built in with a _redirect() function that gives you the option to add parameters. This includes GET parameters and Magento’s path parameters.

This is the basic syntax:

$this->_redirect($url,$paramsArray);

The $url parameter is the magento url where you would like to redirect to. The $paramsArray is an array that contains the parameters you want in the URL.

Here’s an example:

$this->_redirect('module/controller/action', array('param1'=>'1','param2'=>'2'));

This would redirect to http://mysite.com/module/controller/action/param1/1/param2/2/ with param1 and param2 being Magento’s path parameters.

To change the parameters to be GET parameters, you would put the $paramsArray inside another array with '_query' being it’s key.

It would look like this:

$this->_redirect('module/controller/action', array('_query'=>array('param1'=>'1','param2'=>'2')));

This code would redirect to http://mysite.com/module/controller/action?param1=1&param2=2.

Magento reads these different types of parameters the same way so functionally it makes no difference which one you use. However, you want to only use Magento’s when the parameter is a unique ID that will be used to create a unique page for that ID only. Every other situation should use GET parameters.

Have fun playing with Magento redirects!

Leave a Reply