Cle*_*alk 5 php zend-framework2
我正在使用Zend Framework 2开发一个系统,并config_cache_enabled在application.config.php关闭时关闭密钥收到错误:
致命错误:在/home/user/www/myProject.com/data/cache/module-config-cache.app_config.php在线调用未定义的方法set_state Closure :: __()185.
搜索得更好我发现不建议使用闭包,Module.php因为这是导致配置缓存中出现此错误的原因,考虑到这一点我读了一些建议用工厂替换闭包的帖子.
这就是我做的,我创建了一个工厂,并在工厂取代了TableGateway中的DI Module.php并且工作得很好,我的问题是我不知道我的方式是否正常.
谁能告诉我这是否是解决问题的正确方法?
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);
},
Run Code Online (Sandbox Code Playgroud)
application.config.php - 之后:
'factories' => array(
'PedidosTable' => 'Admin\Service\PedidosTableFactory',
),
'aliases' => array(
'Admin\Model\PedidosTable' => 'PedidosTable',
),
Run Code Online (Sandbox Code Playgroud)
TableFactory:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Al *_* ѫ 1
是的,这就是办工厂的方式。您可以在 SO 中查看示例,例如这里的ZF3 MVC Zend\Authentication 作为服务工厂,当然也可以在 Zend“深入”教程中查看:https ://docs.zendframework.com/tutorials/in-depth-guide/models -and-servicemanager/#writing-a-factory-class。即使本教程是为 ZF3 编写的,这部分也与最新的 ZF2 版本完全兼容。
| 归档时间: |
|
| 查看次数: |
330 次 |
| 最近记录: |