如何在特定控制器中禁用ZF2开发人员工具?
我已经尝试过返回终端ViewModel,但它仍然呈现。
答案 0 :(得分:3)
您可以创建自己的侦听器,该侦听器在zdt逻辑之前触发,该逻辑根据特定控制器分离事件。
<?php
namespace Application\Listener;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceLocatorInterface;
class DetachZdtListener extends AbstractListenerAggregate
{
protected $listeners = array();
protected $serviceLocator;
public function __construct(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
public function attach(\Zend\EventManager\EventManagerInterface $events)
{
// Attach a listener to the finish event that has a priority sooner
// than the ZDT listener(s)
$this->listeners[] = $events->attach(MvcEvent::EVENT_FINISH,
array($this, 'onFinish'), -9499
);
}
/**
* The method called when event is fired
*
* @param \Zend\Mvc\MvcEvent $e
*/
public function onFinish(MvcEvent $e) {
$controller = $e->getController();
if ($controller === 'Application\Controller\SomeController') {
$sm = $this->serviceLocator;
$eventManager = $e->getApplication()->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
$eventManager->detachAggregate($sm->get('ZendDeveloperTools\FlushListener'));
$eventManager->detachAggregate($sm->get('ZendDeveloperTools\ProfilerListener'));
$sharedEventManager->clearListeners('profiler');
}
}
}
然后你只需要在你的模块的onBootstrap方法中附加这个监听器,它应该做你正在寻找的东西。