ZF2 - 模块之间的共享模型
ZF2 - shared models between modules
在当前状态下,我有两个模块 - 主模块和管理面板模块。
主模块名为 "Kreator",admin -> "KreatorAdmin"。所有模型都位于 Kreator 模块内(Kreator/Model/UserTable.php 等)。
"KreatorAdmin"几乎是空的,有一个配置:
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__,
),
),
);
}
}
在控制器中简单地添加 "use" 语句并按命名空间导航会导致错误
Argument 1 passed to KreatorAdmin\Controller\AdminController::__construct() must be an instance of Kreator\Model\UserTable, none given,
我还尝试按照此处所述尝试使用服务管理器:
ZF2 Models shared between Modules 但到目前为止运气不好。
我应该如何从 KreatorAdmin/src/KreatorAdmin/AdminController.php 访问 UserTable?
干杯!
更新 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);
},
],
];
}
并更新了控制器
class AdminController extends AbstractActionController
{
protected $userTable;
public function indexAction()
{
$userTable = $this->getServiceLocator()->get('Kreator\Model\UserTable');
return new ViewModel();
}
}
第一个错误 - 使用注释版本:
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 was unable to fetch or create an instance for Kreator\Model\UserTable
解决方案
如果有人想知道。使用以上配置,jobaer 答案中有一个正确的解决方案。
使用注释版本,你必须记得添加
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
配置中的某处 service_manager。
可能是您搞砸了 ZF2 和 ZF3 配置。我不确定,但可能在某个地方,您试图通过传递 UserTable
的实例来创建 AdminController
的工厂,以使其在 AdminController
的操作方法中可用。后来 在进一步使用它时,您没有将 UserTable
的实例传递给 AdminController
的构造函数 。上一行中突出显示的部分导致该错误。
在 ZF2 中,您不需要在控制器的构造函数中传递那个 UserTable
实例来获得它的可用性。只需在任何控制器的操作方法中使用以下一个即可。
$userTable = $this->getServiceLocator()->get('UserTable');
如果想知道这个过程是如何完成的,请参考this part of the tutorial。
在当前状态下,我有两个模块 - 主模块和管理面板模块。 主模块名为 "Kreator",admin -> "KreatorAdmin"。所有模型都位于 Kreator 模块内(Kreator/Model/UserTable.php 等)。
"KreatorAdmin"几乎是空的,有一个配置:
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__,
),
),
);
}
}
在控制器中简单地添加 "use" 语句并按命名空间导航会导致错误
Argument 1 passed to KreatorAdmin\Controller\AdminController::__construct() must be an instance of Kreator\Model\UserTable, none given,
我还尝试按照此处所述尝试使用服务管理器: ZF2 Models shared between Modules 但到目前为止运气不好。
我应该如何从 KreatorAdmin/src/KreatorAdmin/AdminController.php 访问 UserTable?
干杯!
更新 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);
},
],
];
}
并更新了控制器
class AdminController extends AbstractActionController
{
protected $userTable;
public function indexAction()
{
$userTable = $this->getServiceLocator()->get('Kreator\Model\UserTable');
return new ViewModel();
}
}
第一个错误 - 使用注释版本:
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 was unable to fetch or create an instance for Kreator\Model\UserTable
解决方案
如果有人想知道。使用以上配置,jobaer 答案中有一个正确的解决方案。 使用注释版本,你必须记得添加
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
配置中的某处 service_manager。
可能是您搞砸了 ZF2 和 ZF3 配置。我不确定,但可能在某个地方,您试图通过传递 UserTable
的实例来创建 AdminController
的工厂,以使其在 AdminController
的操作方法中可用。后来 在进一步使用它时,您没有将 UserTable
的实例传递给 AdminController
的构造函数 。上一行中突出显示的部分导致该错误。
在 ZF2 中,您不需要在控制器的构造函数中传递那个 UserTable
实例来获得它的可用性。只需在任何控制器的操作方法中使用以下一个即可。
$userTable = $this->getServiceLocator()->get('UserTable');
如果想知道这个过程是如何完成的,请参考this part of the tutorial。