A free and open-source book on Zend Framework for beginners


4.3. Controllers

A controller provides communication between the application, models and views: gets input from HTTP request and uses the model(s) and the corresponding view to produce the necessary HTTP response.

Controllers belonging to module typically reside in the Controller subdirectory of module's source directory (shown in figure 4.3).

Figure 4.3. Controller directory Figure 4.3. Controller directory

Zend Skeleton Application provides you with the default implementation of IndexController class. The IndexController is typically the main controller class of the website. Its code is presented below (some parts of code were omitted for simplicity):

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

From the example above, you can see that controllers usually define their own namespace (line 2). The Index controller, as all other controllers from the Application module, lives in Application\Controller namespace.

A controller is a usual PHP class derived from the AbstractActionController base class (line 7).

By default, the controller class contains the single action method called indexAction() (see lines 9-12). Typically, you will create other action methods in your controller classes.

ZF3 automatically recognizes the action methods by the Action suffix. If a controller method's name does not have that suffix, it is considered as a usual method, not an action.

As its name assumes, an action method performs some site action, which typically results in producing a single web page. Index controller usually contains action methods for site-wide web pages (table 4.1). For example, you would have "index" action for the Home page, "about" action for About page, "contactUs" action for the Contact Us page and possibly other actions.

Table 4.1. Index controller's typical actions
Action Method Description
IndexController::indexAction() The "index" action displays the Home page of your site.
IndexController::aboutAction() The "about" action displays the About page of the site. The About page contains contact and copyright information.
IndexController::contactUsAction() The "contactUs" action displays the Contact Us page of the site. The Contact Us page displays the form for contacting site authors.

4.3.1. Base Controller Class

Every controller in your website is inherited from the AbstractActionController base class. In figure 4.4, the class inheritance diagram is presented.

Figure 4.4. Controller inheritance diagram Figure 4.4. Controller inheritance diagram

The AbstractActionController provides you with several useful methods you can use in your controller classes. Table 4.2 provides you with a brief summary of the methods:

Table 4.2. AbstractActionController's useful methods
Method Name Description
getRequest() Retrieves the Zend\Http\Request object, which is the representation of HTTP request data.
getResponse() Retrieves the Zend\Http\PhpEnvironment\Response object allowing to set data of HTTP response.
getEventManager() Returns the Zend\EventManager\EventManager object, allowing to trigger events and listen to events.
getEvent() Returns the Zend\Mvc\MvcEvent object, which represents the event the controller responds to.
getPluginManager() Returns the Zend\Mvc\Controller\PluginManager object, which can be used for registering controller plugins.
plugin($name, $options) This method allows to access certain controller plugin with the given name.
__call($method, $params) Allows to call a plugin indirectly using the PHP __call magic method.

As you can see from the table above, the base controller class provides you with access to HTTP request and response data, and provides you with the access to the event manager. It also gives you an ability to register and call controller plugins (we will learn about controller plugins later in this chapter).


Top