2012-09-24 24 views
10

tôi đang cố gắng để tạo ra một dịch vụ đơn giản trong zf2 mà tôi có thể truy cập sử dụng trong viewhelperzf2 Tạo dịch vụ đơn giản và truy cập nó thông qua viewhelper

Bước 1. Tôi đã craeted một lớp trong src/Ứng dụng/dịch vụ/Service1.php như sau

namespace Application\Service; 
    use Zend\ServiceManager\ServiceLocatorAwareInterface; 
    use Zend\ServiceManager\ServiceLocatorInterface; 

    class Service1 implements ServiceLocatorAwareInterface 
    { 

     public function __construct() 
     { 

     } 

     public function setServiceLocator(ServiceLocatorInterface $serviceLocator) 
     { 

     }  

     public function getServiceLocator() 
     { 

     } 

    } 

Bước 2 tôi thiết lập này trong tập tin module.php như dưới đây. phương pháp

public function getServiceConfig() 
{  
    return array(
     'factories' => array(
      'Application\Service\Service1' => function ($sm) { 
       return new \Application\Service\Service1($sm); 
      }, 
     ) 
    ); 
} 

public function onBootstrap($e) 
{   
    $serviceManager = $e->getApplication()->getServiceManager(); 

    $serviceManager->get('viewhelpermanager')->setFactory('Abc', function ($sm) use ($e) { 
     return new \Application\View\Helper\Abc($sm); 
    }); 
} 

Bước 3 cuối cùng tôi geting ở chế độ xem helper src/Ứng dụng/thử nghiệm Xem/Helper/Abc.php của tôi() như thế này, II comment dòng này $this->sm->get('Application\Service\Service1'); không có lỗi thì phải có một cái gì đó mà tôi đang thiếu trong dịch vụ?

namespace Application\View\Helper; 

use Zend\View\Helper\AbstractHelper; 

    class Abc extends AbstractHelper 
    { 
     protected $sm; 

     public function test() 
     { 
      $this->sm->get('Application\Service\Service1'); 
     } 
     public function __construct($sm) { 
      $this->sm = $sm; 

     } 
    } 

Step4 sau đó tôi kêu gọi xem helper thử nghiệm của tôi tại một trong những quan điểm như thế này.

$this->Abc()->test(); 

Tôi nhận được lỗi sau.

Fatal error: Call to undefined method Application\Service\Service1::setView() in vendor/zendframework/zendframework/library/Zend/View/HelperPluginManager.php on line 127 Call Stack: 

Tôi còn thiếu gì?

+0

Vui lòng hiển thị thêm một số mã (thực). Có vẻ như bạn đang trả về cá thể 'Service1' ở đâu đó thay vì' Abc'. Do đó, cơ chế trợ giúp xem cố gắng tiêm cá thể khung nhìn vào cá thể 'Service1'. –

+0

Tôi đã thay đổi tên và mọi tham chiếu từ Service1 thành My1serve nhưng vẫn nhận được lỗi tương tự Gọi đến phương thức chưa xác định My1serve :: setView(). Tôi đã không sử dụng tên My1serve bất cứ nơi nào khác trong mã của tôi ngoài một trong những mà tôi đã cho thấy trong câu hỏi. – Developer

+0

Hoàn toàn phù hợp với vấn đề. Chúng tôi sẽ không thể giúp bạn nếu không có thêm thông tin. –

Trả lời

5

thay đổi dòng $this->sm->getServiceLocator()->get('Application\Service\Service1'); trong phương pháp dưới đây

class Abc extends AbstractHelper 
{ 
    protected $sm; 

    public function test() 
    { 
     $this->sm->getServiceLocator()->get('Application\Service\Service1'); 
    } 
    public function __construct($sm) { 
     $this->sm = $sm; 

    } 
} 
7

Một thay thế, trong PHP 5.4 chỉ, không có cấu hình cụ thể, sẽ được sử dụng traits:

trích module.config.php:

'view_helpers' => array(
    'invokables' => array(
     'myHelper' => 'Application\View\Helper\MyHelper', 
    ), 

MyHelper.php:

<?php 
namespace Application\View\Helper; 

use Zend\ServiceManager\ServiceLocatorAwareInterface; 

class HeadScript extends \Zend\View\Helper\MyHelper implements ServiceLocatorAwareInterface 
{ 
    use \Zend\ServiceManager\ServiceLocatorAwareTrait; 

    public function __invoke() 
    { 
     $config = $this->getServiceLocator()->getServiceLocator()->get('Config'); 
     // do something with retrived config 
    } 

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