Web Programming TutorialsLearn about Controllers and its use in Zend Framework

Learn about Controllers and its use in Zend Framework

Learn about Controllers and its use in Zend FrameworkIn our first tutorial of Zend Framework we learnt how to install it in PHP, today we will learn about controller and its uses in Zend Framework.

Zend framework is an open source, object oriented web application framework for PHP 5. It uses the Model-View-Controller (MVC) implementation to provide a basic structure to the Zend framework applications. An application is built up of three important parts viz. presentation, business logic and data access. The MVC pattern allows the separation of these three parts in a very systematic way and thus helps in faster development.
In a MVC pattern, Model represents the data access part, View represents the presentation and Controller represents the business logic.
Controller is the heart of any MVC application. It binds the whole pattern together. It manipulates models, decide which view to display to which user request, pass data required by the view, etc.

  • The Zend Controller system is designed to be lightweight, modular and extensible. Then Zend Controller workflow is implemented by several components. The components are as follows:
    1. Zend_Controller_Front: It receives the request from the server, processes it and is responsible to deliver it to the Action Controllers. Action Controllers are the classes written to take actual action for the request.
    2. Zend_Controller_Request_Abstract: It represents the request environment and provides methods for setting and retrieving the controller and action names and request parameters if any. The default Request_Abstract is Zend_Controller_Request_Http is used for it, which provides access to the entire HTTP request environment.
    3. Zend_Controller_Router_Interface: It is used for defining routers. Routing is the process of examining the request environment to determine, which controller and action of that controller should receive the request. The controller, action and optional parameters are then set in the request object to be processed by Zend_Controller_Dispatcher_Standard. The default Router_Interface is Zend_Controller_Router_Rewrite. It takes the URI from the Zend_Controller_Request_Http and decomposes it into a controller, an action and parameters if any. For example, if the URI is http://localhost/colours/apply, it would be decoded to use colours as controller and apply as action of the controller.
    4. Zend_Controller_Dispatcher_Interface: It is used to define dispatchers. Dispatcher process consists of instantiating the controller class and calling the action method in that class. The default dispatcher is Zend_Controller_Dispatcher_Standard. It instantiates the controller class and calls the action method in it, which is extracted by the Zend_Controller_Router_Rewrite from the URI.
    5. Zend_Controller_Action: It is the base action controller component. Each action controller is a single class that extends the Zend_Controller_Action class. These classes should contain one or more action methods.
    6. Zend_Controller_Response_Abstract: It defines a base response class used to collect and return responses from the action controllers. It collects both headers and body content. The default response class is Zend_Controller_Response_Http, which is suitable for use in an HTTP environment.
  • We saw the components used in the workflow of Zend Controller; now let us see the step by step working of Zend controller in the processing of a web application. The workflow of zend controller is very simple.
    1. A request is received by Zend_Controller_Front.
    2. The Zend_Controller_Front then calls the Zend_Controller_Router_Rewrite to determine which controller and action in that controller to dispatch.
    3. The Zend_Controller_Router_Rewrite decomposes the URI in order to locate the controller and action names in the request.
    4. The Zend_Controller_Front then enters a dispatch loop. It calls the Zend_Controller_Dispatcher_Standard, passing it the request, to dispatch to the controller and action specified in the request or use defaults. After completing the process, control returns to the Zend_Controller_Front.
    5. If the controller has indicated that another controller should be dispatched by resetting the dispatched status of the request, the loop continues and another dispatch is performed. Otherwise, the process ends.

Thus we successfully understood the working and use of controller in this Controllers and their use in Zend Framework.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -