ZF2如何监听特定控制器的调度事件 [英] ZF2 how to listen to the dispatch event of a specific controller
问题描述
如何监听特定控制器的调度事件?目前我执行以下操作:
How can I listen to the dispatch event of a specific controller? At the moment I do the following:
Module.php
public function onBootstrap(EventInterface $event) {
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$eventManager->attach($serviceManager->get('MyListener'));
}
MyListener.php
class MyListener extends AbstractListenerAggregate {
public function attach(EventManagerInterface $eventManager) {
$this->listeners[] = $eventManager->attach(
MvcEvent::EVENT_DISPATCH, function($event) {
$this->setLayout($event);
}, 100
);
}
public function setLayout(EventInterface $event) {
$event->getViewModel()->setTemplate('mylayout');
}
}
这设置了所有控制器调度的布局.现在我只想在应用程序调度特定控制器时设置布局.
This sets the layout for all controller dispatches. Now I want to set the layout only if the application dispatches a specific controller.
推荐答案
就像所有模块都有一个 onBootstrap() 方法一样,所有扩展 AbstractController 的控制器都有一个 onDispatch() 方法.
Like all Modules have an onBootstrap() method, all controllers extending AbstractController have an onDispatch() method.
考虑到您想为单个特定控制器应用不同的布局,您只需执行以下操作:
Considering you want to apply a different layout for a single specific controller, you can simply do the following:
<?php
namespace MyModule\Controller;
use Zend\Mvc\Controller\AbstractActionController; // Or AbstractRestfulController or your own
use Zend\View\Model\ViewModel; // Or JsonModel or your own
use Zend\Mvc\MvcEvent;
class MyController extends AbstractActionController
{
public function onDispatch(MvcEvent $e)
{
$this -> layout('my-layout'); // The layout name has been declared somewhere in your config
return parent::onDispatch($e); // Get back to the usual dispatch process
}
// ... Your actions
}
您可以为每个具有特殊布局的控制器执行此操作.对于那些不知道的人,您不必写任何东西.
You may do this for every controller that has a special layout. For those who don't, well, you don't have to write anything.
如果您经常需要更改布局(例如,您必须处理的不是单个控制器而是多个控制器),您可以在 module.php 中附加一个 MvcEvent 以将您的布局设置代码集中在一个地方.
If you often need to change your layout (e.g. you have to handle not a single controller but several), you can attach an MvcEvent in your module.php to get your layout setting code in one place.
为了简单起见,我在这里没有使用自定义侦听器,但您也可以使用一个.
To keep things simple, I'm not using a custom listener here, but you may use one as well.
<?php
namespace MyModule;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e -> getApplication() -> getEventManager();
$eventManager -> attach(
MvcEvent::EVENT_DISPATCH,
// Add dispatch error event only if you want to change your layout in your error views. A few lines more are required in that case.
// MvcEvent::EVENT_DISPATCH | MvcEvent::EVENT_DISPATCH_ERROR
array($this, 'onDispatch'), // Callback defined as onDispatch() method on $this object
100 // Note that you don't have to set this parameter if you're managing layouts only
);
}
public function onDispatch(MvcEvent $e)
{
$routeMatch = $e -> getRouteMatch();
$routeParams = $routeMatch -> getParams();
switch ($routeParams['__CONTROLLER__']) {
// You may use $routeParams['controller'] if you need to check the Fully Qualified Class Name of your controller
case 'MyController':
$e -> getViewModel() -> setTemplate('my-first-layout');
break;
case 'OtherController':
$e -> getViewModel() -> setTemplate('my-other-layout');
break;
default:
// Ignore
break;
}
}
// Your other module methods...
}
这篇关于ZF2如何监听特定控制器的调度事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!