Zend Framework - Controllers


As discussed earlier, the controller plays an important role in the Zend MVC Framework. All the webpages in an application needs to be handled by a controller.

In the Zend MVC Framework, controllers are objects implementing the – Zend/Stdlib/DispatchableInterface. The DispatchableInterface has a single method, dispatch, which gets the Request object as input, do some logic and returns Response an object as the output.

dispatch(Request $request, Response $response = null) 

A simple example of a Controller object to return “Hello World” is as follows −

use Zend\Stdlib\DispatchableInterface; 
use Zend\Stdlib\RequestInterface as Request; 
use Zend\Stdlib\ResponseInterface as Response;  
class HelloWorld implements DispatchableInterface { 
   public function dispatch(Request $request, Response $response = null) { 
      $response->setContent("Hello World!"); 

The DispatchableInterface is basic and it needs lot of other interfaces to write high level controllers. Some of such interfaces are as follows −

  • InjectApplicationEventInterface − Used to inject events (Zend EventManager)

  • ServiceLocatorAwareInterface − Used to locate Services (Zend ServiceManager)

  • EventManagerAwareInterface − Used to manage events (Zend EventManager)

Keeping these things in mind, the Zend Framework provides lot of readymade controllers implementing these interfaces. The most important controllers are as explained below.


The AbstractActionController (Zend/Mvc/Controller/AbstractActionController) is the most used controller in the Zend MVC Framework. It has all the necessary features to write a typical web page. It allows routes (Routing is matching request url to a controller and one of its methods) to match an action. When matched, a method named after the action will be called by the controller.

For example, if a route test is matched and the route, test returns hello for action, then the helloAction method will be invoked.

Let us write our TutorialController using the AbstractActionController.

  • Create a new PHP class called TutorialController by extending the AbstractActionController and place it in the module/Tutorial/src/Controller/ directory.

  • Set the Tutorial\Controller as the namespace.

  • Write an indexAction method.

  • Return the ViewModel object from indexAction method. The ViewModel object is used to send data from the controller to view engine, which we will see in the subsequent chapters.

The complete code listing is as follows −

namespace Tutorial\Controller;  
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel;  
class TutorialController extends AbstractActionController { 
   public function indexAction() { 
      return new ViewModel(); 

We have successfully added the new TutorialController.


The AbstractRestfulController (Zend\Mvc\Controller\AbstractRestfulController) inspects the HTTP method of the incoming request and matches the action (method) by considering the HTTP methods

For example, the request with GET HTTP method either matches the getList() method or the get() method, if the id parameter is found in the request.


The AbstractConsoleController (Zend\Mvc\Controller\AbstractConsoleController) is like the AbstractActionController except that it only runs in the console environment instead of a browser.