PluginManager :: get无法为Controlador获取或创建实例

时间:2013-11-08 18:34:19

标签: php zend-framework controller zend-framework2

我正在尝试创建一个插件控制器,如下所示:

Application\src\Application\Controller\Plugin\Controlador.php

namespace Application\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPluginManager;
use Biblioteca\Mvc\Db\TableGateway;

class Controlador extends AbstractPluginManager
{

   protected function getTable($table){

        $sm = $this->getServiceLocator();
        $dbAdapter = $sm->get('DbAdapter');
        $tableGateway = new TableGateway($dbAdapter, $table, new $table);
        $tableGateway->initialize();

        return $tableGateway;
    }

    protected function getService($service)
    {
        return $this->getServiceLocator()->get($service);
    }
}

在我的module.config.php中我把它:

'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',

            'Controlador' => 'Application\Controller\Plugin\Controlador'
        ),
    ),

在我的indexController.php中这样:

namespace Application\Controller;</br>


use Zend\Mvc\Controller\AbstractActionController;

use Zend\View\Model\ViewModel;

use Biblioteca\ActionController;

class IndexController extends AbstractActionController{


    public function indexAction()
    {

        $controlador = $this->Controlador();

        return new ViewModel(array(
            'posts' => $controlador->getTable('Application\Model\Post')->fetchAll()->toArray()
        ));
    }
}

当我执行代码时,我收到此消息:“Zend \ Mvc \ Controller \ PluginManager :: get无法为Controlador获取或创建实例”

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您正在controllerManager注册您的插件,该管理器负责创建控制器实例。您需要在模块配置中使用“controller_plugins”键来定义它。

return array(
    'controller_plugins' => array(
        'invokables' => array(
            'Controlador' => 'Application\Controller\Plugin\Controlador'
        )
    )
);

您还需要继承AbstractPlugin。现在您继承AbstractPluginManager,您将用它来创建自己的插件管理器。

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class Controlador extends AbstractPlugin
{