Thứ Năm, 24 tháng 4, 2014

Zend\EventManager [Chat and E-mail]

Zend Framework 2 is an event-driven framework; the event manager allowyou tattach 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 thareacts 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 MVevents 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\ApplicationBootstrap
2.       Zend\Mvc\ApplicationRoute
3.       Zend\Mvc\ApplicationDispatch
4.       Zend\Mvc\Controller\ActionControllerDispatch (if controller extends this
class)
5.       Zend\Mvc\ApplicationRender
6.       Zend\View\ViewRenderer
7.       Zend\View\ViewResponse
8.       Zend\Mvc\ApplicationFinish

In case of errors during dispatch (or) route, the flow of events will be as follows:

1.       Zend\Mvc\ApplicationDispatch.error
2.       Zend\Mvc\ApplicationRender
3.       Zend\View\ViewRenderer
4.       Zend\View\ViewResponse
5.       Zend\Mvc\ApplicationFinish

In our next activity, we will trto set a new layout for multiple controllers using the shared event manager in Zend Framework.


Perform the following stepfor 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 tMvcEvent:
use Zend\Mvc\MvcEvent;

4.       Overwrite the onBootStrap() method with the following code:
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 evenof 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 havthe default layout, then the myaccount layout is applied to these controllers.

Q1. Which of the following helpercan 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 arvalid mail transports supported bZend Framework 2?

1.       Zend\Mail\Transport\Pop
2.       Zend\Mail\Transport\Smtp
3.       Zend\Mail\Transport\Imap
4.       Zend\Mail\Transport\File

Summary


We havcovered a wide range of topics in this chapter; first we learned about making use of external JavaScripts. Nexwe created a simple group chat application and then we learned about Zend\Mail and implemented a simple mailinform. 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 bworking with various media-sharing APIs.

Không có nhận xét nào:

Đăng nhận xét