- Zend Framework - Working Example
- Zend Framework - Error Handling
- Zend Framework - Unit Testing
- Email Management
- Zend Framework - Authentication
- Session Management
- Cookie Management
- Zend Framework - Ajax
- Zend Framework - File Uploading
- Forms & Validation
- Different Databases
- Models & Database
- Zend Framework - Layout
- Zend Framework - View Layer
- Zend Framework - Routing
- Zend Framework - Controllers
- Zend Framework - Creating Module
- Application Structure
- Zend Framework - Module System
- Zend Framework - Event Manager
- Zend Framework - Service Manager
- Zend Framework - Concepts
- Zend Framework - MVC Architecture
- Skeleton Application
- Zend Framework - Installation
- Zend Framework - Introduction
- Zend Framework - Home
Zend Framework Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Zend Framework - Controllers
As discussed earper, the controller plays an important role in the Zend MVC Framework. All the webpages in an apppcation needs to be handled by a controller.
In the Zend MVC Framework, controllers are objects implementing the – Zend/Stdpb/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 ZendStdpbDispatchableInterface; use ZendStdpbRequestInterface as Request; use ZendStdpbResponseInterface as Response; class HelloWorld implements DispatchableInterface { pubpc 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 −
InjectApppcationEventInterface − 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.
AbstractActionController
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 TutorialController 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 psting is as follows −
?php namespace TutorialController; use ZendMvcControllerAbstractActionController; use ZendViewModelViewModel; class TutorialController extends AbstractActionController { pubpc function indexAction() { return new ViewModel(); } }
We have successfully added the new TutorialController.
AbstractRestfulController
The AbstractRestfulController (ZendMvcControllerAbstractRestfulController) 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.
AbstractConsoleController
The AbstractConsoleController (ZendMvcControllerAbstractConsoleController) is pke the AbstractActionController except that it only runs in the console environment instead of a browser.
Advertisements