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.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀