2015-12-07 21 views
5

我正在開發使用Zend Framework 2的系統和轉動鑰匙config_cache_enabledapplication.config.php關閉收到一個錯誤:Zend框架2 - 取代的工廠倒閉在Module.php

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中的閉包,因爲這是導致配置緩存中發生此錯誤的原因,考慮到它,我閱讀了一些建議由工廠替換閉包的文章。

這就是我所做的,我創建了一家工廠,並用工廠替換了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); 
}, 

application.config.php - 後:

'factories' => array(
    'PedidosTable' => 'Admin\Service\PedidosTableFactory', 
), 
'aliases' => array(
    'Admin\Model\PedidosTable' => 'PedidosTable', 
), 

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; 
    } 
} 
+1

這個approuch是正確的,是的。但是,我建議你切換到通過魔術方法__invoke。稍後我會發佈一個代碼示例,並給出解釋。 – Stanimir

+0

是的,沒關係...... – tasmaniski

回答