2012-09-24 14 views
10

Estoy intentando crear un servicio simple en ZF2 que puedo acceder usando en ViewHelperZF2 Creación de servicio simple y acceder a ella a través ViewHelper

Paso 1. he craeted una clase en src/Aplicación/Servicio/Service1.php como sigue

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() 
     { 

     } 

    } 

Paso 2 lo configuran en el archivo module.php de la siguiente manera. Método

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); 
    }); 
} 

Paso 3 finalmente estoy geting en mi ayudante de vista prueba src/Aplicación/Vista/Ayudante/Abc.php() como este, II comentario esta línea $this->sm->get('Application\Service\Service1'); no hay error, tiene que haber algo que me falta en el servicio?

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; 

     } 
    } 

Paso 4 entonces yo estoy llamando mi prueba de ayudante de vista en una de vista como esta.

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

Recibo el siguiente error.

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

¿Qué me falta?

+0

Por favor muestre algunos códigos más (reales). Parece que estás devolviendo la instancia 'Service1' en lugar de' Abc'. Por lo tanto, el mecanismo de vista de ayuda intenta inyectar la instancia de vista en la instancia 'Service1'. –

+0

He cambiado el nombre y todas las referencias de Service1 a My1serve pero sigo recibiendo el mismo error. Llamada al método no definido My1serve :: setView(). No he utilizado el nombre My1serve en ningún otro lugar de mi código, aparte del que ya mostré en cuestión. – Developer

+0

Se adapta perfectamente al problema. No podremos ayudarlo sin un poco más de información. –

Respuesta

5

cambio de la línea de $this->sm->getServiceLocator()->get('Application\Service\Service1'); en el método a continuación

class Abc extends AbstractHelper 
{ 
    protected $sm; 

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

    } 
} 
7

Una alternativa, en PHP 5.4 únicamente, sin configuración específica, sería el uso de traits:

extracto de 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 
    } 

} 
Cuestiones relacionadas