PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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 controller classes (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:

<?php
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

Default Behaviour

By default, the front controller enables the ViewRenderer action helper. This helper takes care of injecting the view object into the controller, as well as automatically rendering views. You may disable it within your action controller via one of the following methods:

<?php
class FooController extends Zend_Controller_Action
{
    public function 
init()
    {
        
// Local to this controller only; affects all actions,
        // as loaded in init:
        
$this->_helper->viewRenderer->setNoRender(true);

        
// Globally:
        
$this->_helper->removeHelper('viewRenderer');

        
// Also globally, but would need to be in conjunction with the
        // local version in order to propagate for this controller:
        
Zend_Controller_Front::getInstance()
            ->
setParam('noViewRenderer'true);
    }
}

initView(), getViewScript(), render(), and renderScript() each proxy to the ViewRenderer unless the helper is not in the helper broker or the noViewRenderer flag has been set.

You can also simply disable rendering for an individual view by setting the ViewRenderer's noRender flag:

<?php
class FooController extends Zend_Controller_Action
{
    public function 
barAction()
    {
        
// disable autorendering for this action only:
        
$this->_helper->viewRenderer->setNoRender();
    }
}

The primary reasons to disable the ViewRenderer are if you simply do not need a view object or if you are not rendering via view scripts (for instance, when using an action controller to serve web service protocols such as SOAP, XML-RPC, or REST). In most cases, you will never need to globally disable the ViewRenderer, only selectively within individual controllers or actions.

Zend Framework