Zend_Controller_Action: Redirecting

This post will focus on redirecting to a new action or URL within the Zend_Controller. Zend_Controller_Action has several utility methods for performing common tasks from within your action methods, we will discus _forward and _redirect. Zend_Controller also has a several Action Helpers to perform redirecting. We will discus the Redirector helper. You can determine the current redirect URL at any time using getRedirectUrl().

_forward($action, $controller = null, $module = null, array $params = null): perform another action. If called in preDispatch(), the currently requested action will be skipped in favor of the new one. Otherwise, after the current action is processed, the action requested in _forward() will be executed. For example:

public function indexAction() {
 /* Do something */
 // Execute loginAction() within this class
public function loginAction() {
  /* Do something */
  // Execute getuserAction() in the indexController of the module users
  $this->_forward(getuser, index, users)

_redirect($url, array $options = array()): redirect to another location. This method takes a URL and an optional set of options. By default, it performs an HTTP 302 redirect. The options may include one or more of the following:

  • exit: whether or not to exit immediately. If requested, it will cleanly close any open sessions and perform the redirect
  • prependBase: whether or not to prepend the base URL registered with the request object to the URL provided
  • code: what HTTP code to utilize in the redirect. Any code between 301 and 306 may be used

The Redirector helper allows you to use a redirector object to fulfill your application’s needs for redirecting to a new URL. It provides numerous benefits over the _redirect() method, such as being able to preconfigure sitewide behavior into the redirector object or using the built in gotoSimple interface similar to that of Zend_Controller_Action::_forward(). The Redirector has a number of methods that can be used to affect the behaviour at redirect:

  • setCode() can be used to set the HTTP response code to use during the redirect
  • setExit() can be used to force an exit() following a redirect. By default this is true
  • setGotoSimple() can be used to set a default URL to use if none is passed to gotoSimple(). Uses the API of _forward()
  • setGotoRoute() can be used to set a URL based on a registered route. Pass in an array of key/value pairs and a route name, and it will assemble the URL according to the route type and definition
  • setGotoUrl() can be used to set a default URL to use if none is passed to gotoUrl(). Accepts a single URL string
  • setPrependBase() can be used to prepend the request object’s base URL to a URL specified with setGotoUrl(), gotoUrl(), or gotoUrlAndExit()
  • setUseAbsoluteUri() can be used to force the Redirector to use absolute URIs when redirecting. When this option is set, it uses the value of $_SERVER[‘HTTP_HOST’], $_SERVER[‘SERVER_PORT’], and $_SERVER[‘HTTPS’] to form a full URI to the URL specified by one of the redirect methods. This option is off by default, but may be enabled by default in later releases.

Additionally, there are a variety of methods in the redirector for performing the actual redirects:

  • gotoSimple() uses setGotoSimple() to build a URL and perform a redirect. It builds a URL from the parameters passed, and using the default :module/:controller/:action/* format of the default router. It then redirects instead of chaining the action (like _forward())
  • gotoRoute() uses setGotoRoute() (route-assembly) to build a URL and perform a redirect
  • gotoUrl() uses setGotoUrl() (URL string) to build a URL and perform a redirect

For example:

$this->_helper->redirector('index', null, null, array("message" => "We are done"));

Leave a Reply