加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

zend-framework2 – ZF2如何监听特定控制器的调度事件

发布时间:2020-05-25 09:14:06 所属栏目:PHP 来源:互联网
导读:如何收听特定控制器的调度事件?目前我做了以下事情: Module.php public function onBootstrap(EventInterface $event) { $application = $event-getApplication(); $eventManager = $application-getEventManager(); $service

如何收听特定控制器的调度事件?目前我做了以下事情:

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

这将设置所有控制器调度的布局.现在我只想在应用程序调度特定控制器时设置布局.

像所有模块都有onBootstrap()方法一样,所有扩展AbstractController的控制器都有一个onDispatch()方法.

考虑到您要为单个特定控制器应用不同的布局,您只需执行以下操作:

<?php

namespace MyModuleController;

use ZendMvcControllerAbstractActionController; // Or AbstractRestfulController or your own
use ZendViewModelViewModel; // Or JsonModel or your own
use ZendMvcMvcEvent;

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
}

您可以为具有特殊布局的每个控制器执行此操作.对于那些不这样做的人,你不需要写任何东西.

如果您经常需要更改布局(例如,您不必处理单个控制器而是多个控制器),则可以在module.php中附加MvcEvent以将布局设置代码放在一个位置.

为了简单起见,我不是在这里使用自定义监听器,但您也可以使用自定义监听器.

<?php

namespace MyModule;

use ZendMvcMvcEvent;

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...
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读