20 May 2013

Zend framework 2 preDispatch

Category howto
Tags #zf2 #dev

Как сделать preDispatch и postDispatch actions в Zend Framework 2 ? код ниже

<?php
 
namespace Application\Controller;
 
use Zend\Mvc\Controller\ActionController
    Zend\Mvc\MvcEvent;
 
class IndexController extends ActionController
{
    public function indexAction()
    {
        return array();
    }
 
    protected function attachDefaultListeners()
    {
        parent::attachDefaultListeners();
 
        $events = $this->events();
        $events->attach('dispatch', array($this, 'preDispatch'), 100);
        $events->attach('dispatch', array($this, 'postDispatch'), -100);
    }
 
    public function preDispatch (MvcEvent $e)
    {
        // Called before self::indexAction()
    }
 
    public function postDispatch (MvcEvent $e)
    {
        // Called after self::indexAction()
    }
}

“100” и “-100” это порядок запуска действий.

Comments