在當前狀態下,我有兩個模塊 - 主模塊和管理面板模塊。 主要模塊被稱爲「Kreator」,管理 - >「KreatorAdmin」。所有模型都位於Kreator模塊(Kreator/Model/UserTable.php等)中。ZF2 - 模塊之間的共享模型
「KreatorAdmin」 幾乎是空的,存在一種用於它的結構包括:
KreatorAdmin /配置/ 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__,
),
),
);
}
}
簡單地增加在控制器 「使用」 語句和在錯誤由命名空間的結果導航
參數1傳遞給KreatorAdmin \控制器\ AdminController :: __構建體()必須KREATOR的一個實例\型號\用戶表,但沒有給出
我也試着玩了一下與服務經理如下所述: 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:Z最終\的ServiceManager \的ServiceManager :: GET無法獲取或創建的Zend \ DB \適配器\適配器
二實例 - 使用未加註釋部分:
的Zend \的ServiceManager \異常\ ServiceNotFoundException的: Zend的\的ServiceManager \的ServiceManager :: GET無法獲取或創建KREATOR \型號實例\用戶表
解決方案
如果有人想知道。使用上述配置有jobaer答案正確的解決方案。 使用註釋版本,你要記得在設定中新增
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
某處service_manager。
嗨,謝謝你的回答。我用完整的代碼更新了這個問題。 仍然無法讓它工作壽。有任何想法嗎? 是的,有使用說明。 – ficus
剛剛更新了我的答案。如果你想和**一起使用這個'$ this-> getServiceLocator() - > get('UserTable')'而不是'$ this-> getServiceLocator() - > get('Kreator \ Model \ UserTable')' uncommented **配置。讓我知道這是否有效。 – unclexo
是的,我得到了它在兩種方式工作。 謝謝! – ficus