gpt4 book ai didi

php - Zend Framework 2 - 在 Module.php 中用工厂替换闭包

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:58 26 4
gpt4 key购买 nike

我正在使用 Zend Framework 2 开发一个系统,并在 application.config.php 中打开 key 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;
}
}

最佳答案

是的,这就是做工厂的方式。您可以在此处查看 SO 中的示例 ZF3 MVC Zend\Authentication as a Service Factory当然还有 Zend“深入”教程:https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/#writing-a-factory-class .即使本教程是为 ZF3 编写的,这部分内容也完全兼容最新的 ZF2 版本。

关于php - Zend Framework 2 - 在 Module.php 中用工厂替换闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34125406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com