2015-12-07 20 views
5

Tôi đang phát triển một hệ thống sử dụng Zend Framework 2 và xoay config_cache_enabled trong application.config.php đóng cửa chính nhận được một lỗi:Zend Framework 2 - thay thế đóng cửa bởi nhà máy ở 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.

tìm kiếm tốt hơn tôi thấy đó không phải là đề nghị sử dụng đóng cửa trong Module.php vì đó là nguyên nhân gây ra lỗi này trong bộ nhớ cache cấu hình, suy nghĩ về nó Tôi đọc một số bài viết đề nghị thay thế các bao đóng bởi nhà máy.

Đó là những gì tôi đã làm, tôi tạo ra một nhà máy và thay thế DI trong TableGateway tại Module.php bởi một Nhà máy và làm việc một cách hoàn hảo, câu hỏi của tôi là tôi không biết nếu nó là OK theo cách tôi đã làm.

Bất cứ ai có thể cho tôi biết nếu đây là cách chính xác để giải quyết vấn đề?

application.config.php - trước:

'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 - sau:

'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

Biểu mẫu này là chính xác, có. Tuy nhiên, tôi khuyên bạn nên chuyển sang sử dụng phương pháp ma thuật __invoke. Tôi sẽ đăng một mẫu mã sau đó với một lời giải thích cho nó. – Stanimir

+0

Vâng, được rồi ... – tasmaniski

Trả lời

0

Vâng, đây là cách để làm nhà máy. Bạn có thể xem các ví dụ trong SO ví dụ tại đây ZF3 MVC Zend\Authentication as a Service Factory và tất nhiên trong Hướng dẫn "Trong chiều sâu" Zend: https://docs.zendframework.com/tutorials/in-depth-guide/models-and-servicemanager/#writing-a-factory-class. Ngay cả khi hướng dẫn này đã được viết cho ZF3, phần này hoàn toàn tương thích với phiên bản ZF2 mới nhất.

Các vấn đề liên quan