zf3 - 从Module.php

时间:2017-02-23 12:35:12

标签: php zend-framework zend-framework2 zend-framework3

我是Zend Framework 3的新手。并将我的Zend Framework 2应用程序迁移到Zend Framework 3。

目前我正在尝试从应用程序模块的userPlugin文件中调用Module.php

这是我的代码,

public function onRoute(MvcEvent $e)
{
    $sm = $e->getApplication()->getServiceManager();
    $userPlugin = $sm->get('ControllerPluginManager')->get('userPlugin');
    $userPlugin->setServiceManager($e->getApplication()->getServiceManager());
    $checkLogin = $userPlugin->isLoggedIn();
}

在我的module.config.php

'controller_plugins' => array(
    //This is also not working
    //'invokables' => array(
    //    'userPlugin' => 'User\Controller\Plugin\UserPlugin',
    //),
    'factories' => [
        Controller\Plugin\UserPlugin::class => InvokableFactory::class,
    ],
    'aliases' => [
        'userPlugin' => Controller\Plugin\UserPlugin::class,
    ]
)

但是收到此错误

  

未捕获的Zend \ ServiceManager \ Exception \ ServiceNotFoundException:在插件管理器Zend \ Mvc \ Controller \ PluginManager中找不到名为“UserPlugin”的插件

我哪里错了?

1 个答案:

答案 0 :(得分:2)

使用以下代码

 $sm = $e->getApplication()->getServiceManager();
 $checkLogin = $sm->get('ControllerPluginManager')
                  ->get(Controller\Plugin\UserPlugin::class)
                  ->isLoggedIn();
相关问题