Using Redirection Inside a Plugin In Zend Framework

2nd February 2009

I had a situation the other day where I had an application in Zend Framework and I wanted to redirect a user to another page. This is fine if you are inside a controller as you can use the _redirect() controller helper, but in this instance I was running the code from inside a plugin and so therefore didn't have direct access to the controller.

The solution is to use the getResponse() method, which is accessible to plugins, and which will retrieve the response object. The response object has a function called setRedirect() that is used to redirect. Any headers that have been issued will be overwritten by this function. The following code can be run inside your zend framework plugins to redirect the user to a different page.

$this->getResponse()->setRedirect('http://another/page/', 301);

The setRedirect() function takes two parameters. The first is the URL to be redirected to and the second is the HTTP response code.

Add new comment

The content of this field is kept private and will not be shown publicly.