doudang4857 2017-06-21 16:36
浏览 47
已采纳

ZF2 - 模块之间的共享模型

In current state I've got two modules - main module, and admin panel module. Main module is called "Kreator", admin -> "KreatorAdmin". All the models are located inside the Kreator module (Kreator/Model/UserTable.php etc.).

"KreatorAdmin" is almost empty, there is a configuration for it:

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

<?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

<?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

Argument 1 passed to KreatorAdmin\Controller\AdminController::__construct() must be an instance of Kreator\Model\UserTable, none given,

I also tried to play a bit with service manager as described here: ZF2 Models shared between Modules but no luck so far.

How am I supposed to access UserTable from KreatorAdmin/src/KreatorAdmin/AdminController.php ?

Cheers!

update 1 I've added getServiceConfig to 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

Second - using uncommented part:

Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for 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.

  • 写回答

1条回答 默认 最新

  • dongmei2351 2017-06-21 19:06
    关注

    May be you messed up with ZF2 and ZF3 configuration. 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. And later you are not passing that instance of UserTable into the AdminController's constructor while working with it further. 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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证