简体   繁体   English

ZF2-模块之间的共享模型

[英]ZF2 - shared models between modules

In current state I've got two modules - main module, and admin panel module. 在当前状态下,我有两个模块-主模块和管理面板模块。 Main module is called "Kreator", admin -> "KreatorAdmin". 主模块称为“ Kreator”,管理->“ KreatorAdmin”。 All the models are located inside the Kreator module (Kreator/Model/UserTable.php etc.). 所有模型都位于Kreator模块内部(Kreator / Model / UserTable.php等)。

"KreatorAdmin" is almost empty, there is a configuration for it: “ KreatorAdmin”几乎为空,有一个配置:

KreatorAdmin/config/module.config.php KreatorAdmin / config / module.config.php

<?php
return array(
  'controllers' => array(
    'invokables' => array(
      'KreatorAdmin\Controller\Admin' => 'KreatorAdmin\Controller\AdminController',
    ),
  ),


  'router' => array(
    'routes' => array(
      'zfcadmin' => array(
        'options' => array(
          'defaults' => array(
            'controller' => 'KreatorAdmin\Controller\Admin',
            'action'     => 'index',
          ),
        ),
      ),
    ),
  ),

  'view_manager' => array(
    'template_path_stack' => array(
      __DIR__ . '/../view'
    ),
  ),
);

KreatorAdmin/src/KreatorAdmin/AdminController.php KreatorAdmin / src / KreatorAdmin / AdminController.php

<?php

namespace KreatorAdmin\Controller;

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


class AdminController extends AbstractActionController
{


  public function indexAction()
  {

    //$this->getServiceLocator()->get('Kreator\Model\UserTable');

    return new ViewModel();
  }
}

KreatorAdmin/Module.php KreatorAdmin / Module.php

<?php
namespace KreatorAdmin;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

Simply adding "use" statements in controller and navigating by namespaces results in error 在控制器中简单地添加“ use”语句并按名称空间导航会导致错误

Argument 1 passed to KreatorAdmin\\Controller\\AdminController::__construct() must be an instance of Kreator\\Model\\UserTable, none given, 传递给KreatorAdmin \\ Controller \\ AdminController :: __ construct()的参数1必须是Kreator \\ Model \\ UserTable的实例,未给出任何实例,

I also tried to play a bit with service manager as described here: ZF2 Models shared between Modules but no luck so far. 我还尝试按如下所述与服务管理器一起玩: ZF2模型在模块之间共享,但到目前为止还没有运气。

How am I supposed to access UserTable from KreatorAdmin/src/KreatorAdmin/AdminController.php ? 我应该如何从KreatorAdmin / src / KreatorAdmin / AdminController.php访问UserTable?

Cheers! 干杯!

update 1 I've added getServiceConfig to Module.php 更新1我已经将getServiceConfig添加到Module.php

public function getServiceConfig()
{
  return [
    'factories' => [
      // 'Kreator\Model\UserTable' => function($sm) {
      //   $tableGateway = $sm->get('UserTableGateway');
      //   $table = new UserTable($tableGateway);
      //   return $table;
      // },
      // 'UserTableGateway' => function($sm) {
      //   $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
      //   $resultSetPrototype = new ResultSet();
      //   $resultSetPrototype->setArrayObjectPrototype(new User());
      //   return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
      // },

      'DbAdapter' => function (ServiceManager $sm) {
        $config = $sm->get('Config');
        return new Adapter($config['db']);
      },
      'UserTable' => function (ServiceManager $sm) {
        return new UserTable($sm->get('UserTableGateway'));
      },
      'UserTableGateway' => function (ServiceManager $sm) {
        $dbAdapter = $sm->get('DbAdapter');
        $resultSetPrototype = new ResultSet();

        $resultSetPrototype->setArrayObjectPrototype(new User());
        return new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
      },
    ],
  ];
}

And updated controller 并更新了控制器

class AdminController extends AbstractActionController
{

  protected $userTable;

  public function indexAction()
  {

    $userTable = $this->getServiceLocator()->get('Kreator\Model\UserTable');

    return new ViewModel();
  }
}

First error - using commented version: 第一个错误-使用注释版本:

Zend\\ServiceManager\\Exception\\ServiceNotFoundException: Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for Zend\\Db\\Adapter\\Adapter Zend \\ ServiceManager \\ Exception \\ ServiceNotFoundException:Zend \\ ServiceManager \\ ServiceManager :: get无法获取或创建Zend \\ Db \\ Adapter \\ Adapter的实例

Second - using uncommented part: 第二-使用未注释部分:

Zend\\ServiceManager\\Exception\\ServiceNotFoundException: Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for Kreator\\Model\\UserTable Zend \\ ServiceManager \\ Exception \\ ServiceNotFoundException:Zend \\ ServiceManager \\ ServiceManager :: get无法获取或创建Kreator \\ Model \\ UserTable的实例

Solution

If anyone wonder. 如果有人想知道。 Using above configuration there is a correct solution in jobaer answer. 使用以上配置,可以在工作答案中找到正确的解决方案。 Using commented version, you have to remember to add 使用带注释的版本,您必须记住要添加

 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',

somewhere in config to service_manager. 在service_manager的配置中。

May be you messed up with ZF2 and ZF3 configuration. 可能是您搞砸了ZF2和ZF3配置。 I am not sure but somewhere may be, you tried to create a factory of AdminController by passing an instance of UserTable to make it available inside AdminController 's action methods. 我不确定,但是可能在某个地方,您试图通过传递UserTable的实例以使其在AdminController的操作方法中可用来创建AdminController的工厂。 And later you are not passing that instance of UserTable into the AdminController 's constructor while working with it further. 以后, 您将不会在进一步处理UserTable实例时将其传递到AdminController的构造函数中 The highlighted part from the previous line results in that error. 前一行中突出显示的部分将导致该错误。

In ZF2 you do not need to pass that UserTable instance in the controller's constructor for its availability. ZF2中,您无需在控制器的构造函数中传递该UserTable实例即可获得其可用性。 Just use the following one in any controller's action methods. 只需在任何控制器的操作方法中使用以下方法即可。

$userTable = $this->getServiceLocator()->get('UserTable');

If want to know how this process is done, please, refer to this part of the tutorial . 如果想知道此过程是如何完成的,请参阅本教程的这一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM