简体   繁体   中英

Disable Zend Developer Tools

How can I disable ZF2 developer tools in a particular controller?

I've already tried returning an terminal ViewModel, but it still renders.

You could create your own listener that fires before the zdt logic that detaches the events based on a specific controller.

<?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');
        }
    }

}

Then you would just need to attach this listener in the onBootstrap method of one fo your modules, and it should do what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM