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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler