Zend Framework 2 - 在 Module.php 中用工厂替换闭包
Zend Framework 2 - replace closures by factory in Module.php
我正在使用 Zend Framework 2 开发一个系统并在 application.config.php
中打开密钥 config_cache_enabled
闭包收到错误:
Fatal error: Call to undefined method set_state Closure::__()in /home/user/www/myProject.com/data/cache/module-config-cache.app_config.php online 185.
更好地搜索我发现不建议在 Module.php
中使用闭包,因为那是导致配置缓存中出现此错误的原因,考虑到这一点我阅读了一些建议用工厂替换闭包的帖子。
这就是我所做的,我创建了一个工厂并用一个工厂替换了 Module.php
中 TableGateway 中的 DI,并且工作得很好,我的问题是我不知道我这样做是否合适。
谁能告诉我这是否是解决问题的正确方法?
application.config.php
- 之前:
'Admin\Model\PedidosTable' => function($sm) {
$tableGateway = $sm->get('PedidosTableGateway');
$table = new PedidosTable($tableGateway);
return $table;
},
'PedidosTableGateway' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Pedidos());
return new TableGateway('pedidos', $dbAdapter, null, $resultSetPrototype);
},
application.config.php - 之后:
'factories' => array(
'PedidosTable' => 'Admin\Service\PedidosTableFactory',
),
'aliases' => array(
'Admin\Model\PedidosTable' => 'PedidosTable',
),
表格工厂:
namespace Admin\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Admin\Model\Pedidos;
use Admin\Model\PedidosTable;
class PedidosTableFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Pedidos());
$tableGateway = new TableGateway('pedidos', $dbAdapter, null, $resultSetPrototype);
$table = new PedidosTable($tableGateway);
return $table;
}
}
是的,这就是做工厂的方式。例如,您可以在此处 and of course in the Zend "In-Depth" Tutorial: https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/#writing-a-factory-class 查看 SO 中的示例。即使本教程是为 ZF3 编写的,这部分也完全兼容最新的 ZF2 版本。
我正在使用 Zend Framework 2 开发一个系统并在 application.config.php
中打开密钥 config_cache_enabled
闭包收到错误:
Fatal error: Call to undefined method set_state Closure::__()in /home/user/www/myProject.com/data/cache/module-config-cache.app_config.php online 185.
更好地搜索我发现不建议在 Module.php
中使用闭包,因为那是导致配置缓存中出现此错误的原因,考虑到这一点我阅读了一些建议用工厂替换闭包的帖子。
这就是我所做的,我创建了一个工厂并用一个工厂替换了 Module.php
中 TableGateway 中的 DI,并且工作得很好,我的问题是我不知道我这样做是否合适。
谁能告诉我这是否是解决问题的正确方法?
application.config.php
- 之前:
'Admin\Model\PedidosTable' => function($sm) {
$tableGateway = $sm->get('PedidosTableGateway');
$table = new PedidosTable($tableGateway);
return $table;
},
'PedidosTableGateway' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Pedidos());
return new TableGateway('pedidos', $dbAdapter, null, $resultSetPrototype);
},
application.config.php - 之后:
'factories' => array(
'PedidosTable' => 'Admin\Service\PedidosTableFactory',
),
'aliases' => array(
'Admin\Model\PedidosTable' => 'PedidosTable',
),
表格工厂:
namespace Admin\Service;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Admin\Model\Pedidos;
use Admin\Model\PedidosTable;
class PedidosTableFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Pedidos());
$tableGateway = new TableGateway('pedidos', $dbAdapter, null, $resultSetPrototype);
$table = new PedidosTable($tableGateway);
return $table;
}
}
是的,这就是做工厂的方式。例如,您可以在此处