ZF2 Service Manager factory disappears

For programming and general questions on Zend Framework

ZF2 Service Manager factory disappears

Postby double-1981 on Tue Nov 06, 2012 12:35 am

Hi there,

i got a strange problem with the ZF2.
Im configuring my Service Manager this way:

module.config.php ...
Code: Select all
<?php
return array
(
    //...
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
            'Bricks\Widget' => 'Bricks\Widget\WidgetFactory',
        ),
    ),
    //...
);
 


The belonging class Bricks\Widget\WidgetFactory.php ...
Code: Select all
<?php

namespace BricksWidget
;

use ZendServiceManagerFactoryInterface;
use ZendServiceManagerServiceLocatorInterface;

class WidgetFactory implements FactoryInterface {
    
    public function createService
(ServiceLocatorInterface $serviceLocator)
    {
        $config = $sl->get('Configuration');
        $widgets = $config->widget;
        die(var_dump($widgets,__FILE__,__LINE__));
    }
    
}
 


I expect the dump of the configuration parameters.
But this class will never be instanciated due an ServiceNotFoundException if i call the class through the ServiceManager.

I've checked Zend\ServiceManager\Config.php if the factory will be setted and in fact the factory will be stored on the ServiceManager.

The file where i call the ServiceManager Bricks\View\Helper\Widgets.php ...
Code: Select all
<?php

namespace BricksViewHelper
;

use ZendViewHelperAbstractHtmlElement;
use ZendServiceManagerServiceLocatorAwareInterface;
use ZendServiceManagerServiceLocatorInterface;

class Widgets extends AbstractHtmlElement implements
    ServiceLocatorAwareInterface
{
    
    
/**
     * @var ServiceLocator
     */
    protected $serviceLocator;
    
    
/**
     * Set the service locator.
     *
     * @param ServiceLocator $serviceLocator
     * @return Widget
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }
    
    
/**
     * Get the service locator.
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
    
    public function __invoke
()
    {
        $sl = $this->getServiceLocator();
        die(var_dump($sl->has('Bricks\Widget')));
        $sl = $sm->get('widget');        
        die
(var_dump(get_class($sl),__FILE__,__LINE__));
    }
    
}
 


it outputs bool(false)

In Conclusion:

The ServiceManager will be setted up correctly but the factory disappears.
I couldn't find any reason for this behaviour and need help.

Thanks in advance
With best regards

Sven
double-1981
 
Posts: 3
Joined: Tue Nov 06, 2012 12:19 am

Re: ZF2 Service Manager factory disappears

Postby double-1981 on Tue Nov 06, 2012 2:15 am

$sm->getRegisteredServices() returns ...

Code: Select all
array(4) { ["invokableClasses"]=> array(80) { [0]=> string(5) "cycle" [1]=> string(11) "declarevars" [2]=> string(10) "escapehtml" [3]=> string(14) "escapehtmlattr" [4]=> string(8) "escapejs" [5]=> string(9) "escapecss" [6]=> string(9) "escapeurl" [7]=> string(8) "gravatar" [8]=> string(8) "headlink" [9]=> string(8) "headmeta" [10]=> string(10) "headscript" [11]=> string(9) "headstyle" [12]=> string(9) "headtitle" [13]=> string(9) "htmlflash" [14]=> string(8) "htmllist" [15]=> string(10) "htmlobject" [16]=> string(8) "htmlpage" [17]=> string(13) "htmlquicktime" [18]=> string(12) "inlinescript" [19]=> string(4) "json" [20]=> string(6) "layout" [21]=> string(17) "paginationcontrol" [22]=> string(11) "partialloop" [23]=> string(7) "partial" [24]=> string(11) "placeholder" [25]=> string(16) "renderchildmodel" [26]=> string(19) "rendertoplaceholder" [27]=> string(9) "serverurl" [28]=> string(9) "viewmodel" [29]=> string(4) "form" [30]=> string(10) "formbutton" [31]=> string(11) "formcaptcha" [32]=> string(11) "captchadumb" [33]=> string(15) "formcaptchadumb" [34]=> string(13) "captchafiglet" [35]=> string(17) "formcaptchafiglet" [36]=> string(12) "captchaimage" [37]=> string(16) "formcaptchaimage" [38]=> string(16) "captcharecaptcha" [39]=> string(20) "formcaptcharecaptcha" [40]=> string(12) "formcheckbox" [41]=> string(14) "formcollection" [42]=> string(9) "formcolor" [43]=> string(8) "formdate" [44]=> string(12) "formdatetime" [45]=> string(17) "formdatetimelocal" [46]=> string(11) "formelement" [47]=> string(17) "formelementerrors" [48]=> string(9) "formemail" [49]=> string(8) "formfile" [50]=> string(10) "formhidden" [51]=> string(9) "formimage" [52]=> string(9) "forminput" [53]=> string(9) "formlabel" [54]=> string(9) "formmonth" [55]=> string(17) "formmulticheckbox" [56]=> string(10) "formnumber" [57]=> string(12) "formpassword" [58]=> string(9) "formradio" [59]=> string(9) "formrange" [60]=> string(9) "formreset" [61]=> string(7) "formrow" [62]=> string(10) "formsearch" [63]=> string(10) "formselect" [64]=> string(10) "formsubmit" [65]=> string(7) "formtel" [66]=> string(8) "formtext" [67]=> string(12) "formtextarea" [68]=> string(8) "formtime" [69]=> string(7) "formurl" [70]=> string(8) "formweek" [71]=> string(14) "currencyformat" [72]=> string(10) "dateformat" [73]=> string(12) "numberformat" [74]=> string(9) "translate" [75]=> string(15) "translateplural" [76]=> string(22) "zenddevelopertoolstime" [77]=> string(24) "zenddevelopertoolsmemory" [78]=> string(29) "zenddevelopertoolsdetailarray" [79]=> string(13) "brickswidgets" } ["factories"]=> array(6) { [0]=> string(10) "navigation" [1]=> string(3) "url" [2]=> string(8) "basepath" [3]=> string(7) "doctype" [4]=> string(14) "bricksresource" [5]=> string(8) "resource" } ["aliases"]=> array(0) { } ["instances"]=> array(16) { [0]=> string(9) "viewmodel" [1]=> string(7) "doctype" [2]=> string(9) "serverurl" [3]=> string(8) "basepath" [4]=> string(9) "translate" [5]=> string(9) "headtitle" [6]=> string(8) "headmeta" [7]=> string(14) "bricksresource" [8]=> string(8) "headlink" [9]=> string(10) "headscript" [10]=> string(3) "url" [11]=> string(10) "navigation" [12]=> string(10) "escapehtml" [13]=> string(7) "partial" [14]=> string(9) "escapeurl" [15]=> string(13) "brickswidgets" } }


There is neither a configuration nor an application.
What is my mistake?

Thanks in advance
With best regards

Sven
double-1981
 
Posts: 3
Joined: Tue Nov 06, 2012 12:19 am

Re: ZF2 Service Manager factory disappears

Postby double-1981 on Tue Nov 06, 2012 3:53 am

Finaly i found the solution.
It takes me ours but i've found the solution on the net.

I had to add getServiceLocator() a second time to receive the global on:

Code: Select all
<?php

namespace BricksViewHelper
;

use ZendViewHelperAbstractHtmlElement;
use ZendServiceManagerServiceLocatorAwareInterface;
use ZendServiceManagerServiceLocatorInterface;

class Widgets extends AbstractHtmlElement implements
    ServiceLocatorAwareInterface
{
    
    
/**
     * @var ServiceLocatorInterface
     */
    protected $serviceLocator;
    
    
/**
     * Set the service locator.
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return Widget
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }
    
    
/**
     * Get the service locator.
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
    
    public function __invoke
()
    {        
/* Here is the hint */
        $sl = $this->getServiceLocator()->getServiceLocator();

        die(var_dump($sl->get('Bricks\Widget')));
        $factory = new BricksWidgetWidgetFactory();
        $widgets = $factory->createService($this->getServiceLocator());
        die(var_dump($widgets));
    }
    
}
 


All Works now.

Grettings

Sven
double-1981
 
Posts: 3
Joined: Tue Nov 06, 2012 12:19 am


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 11 guests