ZF2控制器插件

时间:2013-06-20 03:48:08

标签: zend-framework2

我是ZF2的新手,并且在应该是一个基本想法时遇到一些麻烦。

我正在使用ZfCommons User模块进行身份验证,它已经安装并且运行正常。

现在我想验证用户实际上是从我的控制器登录的(How to check if the user is logged in但是我无法弄清楚如何注册控制器插件,我现在收到此错误:< / p>

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

我的控制器看起来像这样:

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController as AbstractActionController;
use Zend\View\Model\ViewModel as ViewModel;

class IndexController extends AbstractActionController
{

    public function __construct()
    {
        $plugin = $this->plugin('ZfcUserAuthentication');

    }

    public function indexAction()
    {
        return new ViewModel();
    }

    public function testAction()
    {
        return new ViewModel();
    }
}

2 个答案:

答案 0 :(得分:9)

抛出异常是因为您在构造函数中请求插件。由于插件如何与控制器绑定,因此根本无法在控制器的构造函数中使用插件。

<强>背景

创建对象时首先调用构造函数。在__construct()之前没有其他方法调用。如果您通过setter方法在此新实例中注入了某些内容,则因此无法在构造函数中访问此实例。

代码示例:

$obj = new MyClass();
$obj->setFoo('foo');

class MyClass()
{
  protected $foo;

  public function __construct()
  {
    var_dump($this->foo); // null!
  }

  public function setFoo($foo)
  {
    $this->foo = $foo;
  }
}

这看起来很明显,但是正好在Zend Framework 2中使用控制器和控制器插件管理器进行了什么。在创建对象后注入管理器:

$controller = new MySomethingController;
$controller->setPluginManager($plugins);

另请参阅Github上的code which injects the plugin manager in the controller。因此:遗憾的是,您无法访问构造函数中的插件。

<强>替代

您可以通过以下任何操作访问插件:

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        // This works!
        $plugin = $this->plugin('ZfcUserAuthentication');

        return new ViewModel();
    }

    public function testAction()
    {
        // This works too!
        $plugin = $this->plugin('ZfcUserAuthentication');

        return new ViewModel();
    }
}

您还可以将侦听器附加到控制器的调度,因此将对每个操作进行函数调用,而不是您指定的操作:

use Zend\Mvc\MvcEvent;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
       return new ViewModel();
    }

    public function testAction()
    {
        return new ViewModel();
    }

    protected function attachDefaultListeners()
    {
        parent::attachDefaultListeners();

        $events = $this->getEventManager();
        $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'checkUserIdentity'));
    }

    public function checkUserIdentity()
    {
        // This works!
        // It is called for both indexAction() and testAction()
        $plugin = $this->plugin('ZfcUserAuthentication');
    }
}

您甚至可以从控制器中删除此逻辑,因此您可以将其重复用于多个控制器:

class Module
{
    public function onBootstrap($e)
    {
        $app = $e->getApplication();
        $em  = $app->getEventManager()->getSharedManager();

        $em->attach('MyModule\Controller\MyController', MvcEvent::EVENT_DISPATCH, function($e) {
            $controller = $e->getController();

            // This works!
            $plugin = $controller->plugin('ZfcUserAuthentication');
        });
    }
}

所以,根据你想要的DRY,有很多可能在构造函数外部访问插件。

答案 1 :(得分:0)

根据文档,您可以使用以下

$this->zfcUserAuthentication()->hasIdentity()

$this->zfcUserAuthentication()->getIdentity()