Zend Framework 2 is an event-driven framework; the event manager allows you to attach events to almost any functionality. There are three main terms used in Zend Framework's event management, which are as follows:
◆ Event manager: The EventManager object is the object that holds a collection of listeners and their relative events
◆ Listener: The listener is the callback that reacts to the triggered event
◆ Event: The event is the action that is triggered by the event manager
The event manager provides attach() and trigger() to create and trigger events respectively. Mostly we will be depending on MVC events for various operation, and the sequence of execution of MVC applicationevents is described in the following diagram:
Flow of events for successful execution is as follows:
1. Zend\Mvc\Application: Bootstrap
2. Zend\Mvc\Application: Route
3. Zend\Mvc\Application: Dispatch
4. Zend\Mvc\Controller\ActionController: Dispatch (if controller extends this
class)
5. Zend\Mvc\Application: Render
6. Zend\View\View: Renderer
7. Zend\View\View: Response
8. Zend\Mvc\Application: Finish
In case of errors during dispatch (or) route, the flow of events will be as follows:
1. Zend\Mvc\Application: Dispatch.error
2. Zend\Mvc\Application: Render
3. Zend\View\View: Renderer
4. Zend\View\View: Response
5. Zend\Mvc\Application: Finish
In our next activity, we will try to set a new layout for multiple controllers using the shared event manager in Zend Framework.
Perform the following steps for setting the module layout using ZF events:
1. Create a new layout for the My Account page and save it under CommunicationApp/ module/Users/view/layout/myaccount-layout.phtml.
2. Add the layout to the CommunicationApp/module/Users/config/module.config.php
file under view_manager -> template_map: 'layout/myaccount' => DIR phtml',
. '/../view/layout/myaccount-layout.
3. Open the CommunicationApp/module/Users/module.php file and add references to MvcEvent:
use Zend\Mvc\MvcEvent;
public function onBootstrap($e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$sharedEventManager = $eventManager->getSharedManager(); // The shared event manager
$sharedEventManager->attach( NAMESPACE , MvcEvent::EVENT_ DISPATCH, function($e) {
$controller = $e->getTarget(); // The controller which is dispatched
$controllerName = $controller->getEvent()
->getRouteMatch()->getParam('controller'); if (!in_array($controllerName,
array('Users\Controller\Index', 'Users\Controller\ Register', 'Users\Controller\Login'))) {
$controller->layout('layout/myaccount');
}
});
}
5. Open the Communication Application page in any web browser; make a note of the layout:
6. Log in to the application and see the new layout being applied:
What just happened?
We have used the Zend Framework event manager to attach a listener to the Dispatch event of the module. So every time the controller is dispatched, this event is triggered. The callback checks if the controller is valid and if thecontroller is not among the list of controllers that have the default layout, then the myaccount layout is applied to these controllers.
Q1. Which of the following helpers can be used to define/attach CSS styles inside the HTML
head section?
1. HeadLink
2. HeadScript
3. HeadCss
4. HeadStyle
Q2. Which of the following are valid mail transports supported by Zend Framework 2?
1. Zend\Mail\Transport\Pop
2. Zend\Mail\Transport\Smtp
3. Zend\Mail\Transport\Imap
4. Zend\Mail\Transport\File
Summary
We have covered a wide range of topics in this chapter; first we learned about making use of external JavaScripts. Next we created a simple group chat application and then we learned about Zend\Mail and implemented a simple mailing form. Towards the end, we learned about events and how to make use of these events in Zend Framework. In the next chapter we will be working on media sharing using Zend Framework by working with various media-sharing APIs.
Không có nhận xét nào:
Đăng nhận xét