The Redirector action helper
Redirector does what it says on the tin and redirects the user to another page. I mostly use this when coming back from filling a form in, so that the user is then redirected to another page. In admin systems, this is usually a list page. On front end websites, this is usually a thank you page. Though for log-in forms, I tend to try and return the user to where they were going!
It’s used in a controller action method like this:
$urlOptions = array('controller'=>'index', 'action'=>'index');
gotoRoute() takes the same set of parameters are the url() view helper which is not a surprise as they both proxy through to the Front Controller’s router object. It’s handy though as one you know one, you know the other :)
If you are using the default route, then you can use gotoSimple(). For example to redirect to the news controller’s list action, you would do:
The gotoSimple() method signature is:
gotoSimple($action, $controller = null, $module = null, array $params = array());
As you can see, it provides defaults for the controller and module and params parameters so you only need to set them if you need to. This works well for admin system as I tend to be redirecting within the same controller (from the edit or delete action to index, usually).
You can also use the Redirector with an absolute URL, by using the gotoUrl() method:
$url = 'http://www.akrabat.com';
I tend to use this one much less frequently – so infrequently, that I can’t think of a use-case off the top of my head :)
By default, Redirector sets a 302 status code, however you can also set a 301 if you want to:
There are a few other options that can be set like setExit() and setUseAbsoluteUri(), but to be honest, I don’t think I’ve ever used them!
I find that I use Redirector fairly frequently as its gotoRoute() uses the same parameters as url() which makes it easy to remember how to use it. Like url(), it also benefits from remembering which route was used to get you to the current page and reuses that when creating the next one which is handy.