【问题标题】:Can't initialize my plugin function in ZF2 constructor无法在 ZF2 构造函数中初始化我的插件函数
【发布时间】:2015-08-09 01:11:30
【问题描述】:

我是 ZF2 的新手,我正在准备一个带有简单登录和 CRUD 系统的演示应用程序。现在为了登录,我准备了一个插件,其中包含一些功能,这些功能将验证用户、返回登录的用户数据、返回登录状态等。但我面临的问题是我无法将任何变量初始化到构造函数中我的控制器将存储插件的任何返回值。它总是显示服务未找到异常。

请在下面找到我的插件代码:

AuthenticationPlugin.php

<?php
namespace Album\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Session\Container as SessionContainer;
use Zend\View\Model\ViewModel;
use Album\Entity\User;

class AuthenticationPlugin extends AbstractPlugin{

    protected $entityManager;
    protected $usersession;

    public function __construct(){

        $this->usersession = new SessionContainer('UserSession');   
    }

    public function dologin($email,$password)
    {
        $getData = $this->em()->getRepository('Album\Entity\User')->findOneBy(array('email' => $email, 'password' => $password));

        if(count($getData)){

            $this->usersession->offsetSet('userid', $getData->getId());

            return true;
        }
        else{

            return false;
        }   
    }

    public function isloggedin(){

        $userid = $this->usersession->offsetGet('userid');

        if(!empty($userid)){

            return true;    
        }   
        else{

            return false;   
        }
    }

    public function logindata(){

        $userid = $this->usersession->offsetGet('userid');
        $getData = $this->em()->getRepository('Album\Entity\User')->findOneBy(array('id' => $userid));

        return $getData;
    }

    public function logout(){

        $this->usersession->offsetUnset('userid');  
    }

    public function em(){

        return $this->entityManager = $this->getController()->getServiceLocator()->get('Doctrine\ORM\EntityManager');   
    }
}
?>

在我的 module.config.php

'controller_plugins' => array(
        'invokables' => array(
            'AuthPlugin' => 'Album\Controller\Plugin\AuthenticationPlugin',
        )
    ),

现在我正在我的控制器中执行此操作:

protected $entityManager;
protected $isloggedin;
protected $authentication;

public function __construct(){

    $this->authentication = $this->AuthPlugin();
    $this->isloggedin = $this->authentication->isloggedin();    
}

我得到的错误如下:

An error occurred 执行过程中发生错误;请再试一次 之后。附加信息: Zend\ServiceManager\Exception\ServiceNotFoundException

文件:

D:\xampp\htdocs\subhasis\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:555

消息:

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for AuthPlugin

但是,如果我在任何控制器操作中编写上述构造函数代码,一切都很好。在 ZF1 中,我可以在 init() 方法中初始化任何变量,并且可以在我的任何操作中使用该变量。我如何在 ZF2 中做到这一点?在这里,我想检测用户是否登录了构造函数本身。现在我必须在我不想要的每一个动作中调用插件。

我应该在这里做什么?

【问题讨论】:

    标签: php zend-framework frameworks zend-framework2 doctrine


    【解决方案1】:

    您收到的错误是因为您试图在控制器的__construct 方法中使用ServiceManager(通过Zend\Mvc\Controller\PluginManager)。

    当控制器注册为 invokable 类时,服务管理器 (ControllerManager) 负责创建控制器实例。 一旦创建,它将调用controllers various default 'initializers'also inlcudes the plugin manager。通过将您的代码放在__construct 中,它会在设置插件管理器之前尝试使用它。

    您可以通过使用控制器 factory 来解决此问题,而不是 module.config.php 中的可调用对象。

    'controllers' => [
        'factories' => [
            'MyModule\Controller\Foo' => 'MyModule\Controller\FooControllerFactory',
        ],
    ],
    

    然后是工厂

    namespace MyModule\Controller\FooControllerFactory;
    
    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    class FooControllerFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $controllerManager)
        {
            $serviceManager = $controllerManager->getServiceLocator();
            $controllerPluginManager = $serviceManager->get('ControllerPluginManager');
    
            $authPlugin = $controllerPluginManager->get('AuthPlugin');
    
            return new FooController($authPlugin);
        }
    }
    

    最后,更新控制器__construct 以添加新参数并删除$this-&gt;authPlugin()的调用

    class FooController extends AbstractActionController
    {
        public function __construct(AuthPlugin $authentication)
        {
            $this->authentication = $authentication;
            $this->isloggedin     = $authentication->isloggedin();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多