7.5. Action Controllers
Prev | Chapter 7. Zend_Controller | Next

## 7.5. Action Controllers

### 7.5.1. Introduction

`Zend_Controller_Action` is an abstract class you may use for implementing Action Controllers for use with the Front Controller when building a website based on the Model-View-Controller (MVC) pattern.

To use Zend_Controller_Action, you will need to subclass it in your actual action controllers (or subclass it to create your own base class for action controllers). The most basic operation is to subclass it, and create action methods that correspond to the various actions you wish the controller to handle for your site. Zend_Controller's routing and dispatch handling will autodiscover any methods ending in 'Action' in your class as potential controller actions.

For example, let's say your class is defined as follows:

    class FooController extends Zend_Controller_Action
    {
        public function barAction()
        {
            // do something
        }

        public function bazAction()
        {
            // do something
        }
    }

The above FooController class (controller 'foo') defines two actions, 'bar' and 'baz'.

There's much more that can be accomplished than this, such as custom initialization actions, default actions to call should no action (or an invalid action) be specified, pre- and post-dispatch hooks, and a variety of helper methods. This chapter serves as an overview of the action controller functionality

### 7.5.2. Object initialization

While you can always override the action controller's constructor, we do not recommend this. Zend_Controller_Action::__construct() performs some important tasks, such as registring the request and response objects, as well as any custom invocation arguments passed in from the front controller. If you must override the constructor, be sure to call parent::__construct($request, $response, $invokeArgs).

The more appropriate way to customize instantiation is to use the init() method, which is called as the last task of __construct(). For example, if you want to connect to a database at instantiation:

    class FooController extends Zend_Controller_Action
    {
        public function init()
        {
            $this->db = Zend_Db::factory('Pdo_Mysql', array(
                'host'     => 'myhost',
                'username' => 'user',
                'password' => 'XXXXXXX',
                'dbname'   => 'website'
            ));
        }
    }

### 7.5.3. Pre- and Post-Dispatch Hooks

Zend_Controller_Action specifies two methods that may be called to bookend a requested action, preDispatch() and postDispatch(). These can be useful in a variety of ways: verifying authentication and ACLs prior to running an action (by calling _forward() in preDispatch(), the action will be skipped), for instance, or placing generated content in a sitewide template (postDispatch()).

### 7.5.4. Accessors

A number of objects and variables are registered with the object, and each has accessor methods.

- Request Object: getRequest() may be used to retrieve the request object used to call the action.

- Response Object: getResponse() may be used to retrieve the response object aggregating the final response. Some typical calls might look like:

      $this->getResponse()->setHeader('Content-Type', 'text/xml');
      $this->getResponse()->appendBody($content);

- Invocation Arguments: the front controller may push parameters into the router, dispatcher, and action controller. To retrieve these, use getInvokeArg($key); alternatively, fetch the entire list using getInvokeArgs().

- Request parameters: The request object aggregates request parameters, such as any _GET or _POST parameters, or user parameters specified in the URL's path information. To retrieve these, use _getParam($key) or _getAllParams(). You may also set request parameters using _setParam(); this is useful when forwarding to additional actions.

  To test whether or not a parameter exists (useful for logical branching), use _hasParam($key).

### 7.5.5. Utility Methods

Besides the accessors, Zend_Controller_Action has several utility methods for performing common tasks from within your action methods (or from pre-/post-dispatch).

- _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.

- _redirect($url, $code = 302): redirect to another location. This method takes a URL and an optional HTTP status code. The redirect is performed immediately and program execution is halted.

  If a status code is provided, it will be passed to the PHP header() command when the redirect is performed.

Prev | Up | Next
7.4. Provided Subclasses | Home | 7.6. Plugins