set shared false for view_helpers doesn't work #4221

Closed
samsonasik opened this Issue Apr 13, 2013 · 7 comments

Comments

Projects
None yet
4 participants
Contributor

samsonasik commented Apr 13, 2013

i'm using ZF 2.1.5, i have created a simple view helper

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class MySimpleViewHelper extends AbstractHelper
{
        public function __construct()
        {
                 echo 'simple';
        }

        public function __invoke()
        {
        }
}

and create a factory for that :

namespace Application\View\Helper;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MySimpleViewHelperFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $helper = new MySimpleViewHelper;

        return $helper;
    }
}

and register it at view_helpers :

'view_helpers' => array(
    'factories' => array(
        'mysimplehelper' => 'Application\View\Helper\MySimpleViewHelperFactory'
    ),
    'shared' => array(
        'mysimplehelper' => false
    )
),

i tried call from view for multiple time, but echo 'simple'; just called at first time. If i try in 'service_manager' key, it just work.

@samsonasik samsonasik closed this Apr 14, 2013

@samsonasik samsonasik reopened this Apr 14, 2013

Contributor

samsonasik commented Apr 17, 2013

it is work if i used :

$this->plugin('mysimplehelper');

but if i used

$this->mysimplehelper();

it does'nt work

Owner

weierophinney commented Jun 17, 2013

If you want to take a stab at it, take a look at the __call magic method, and see how it differs from the plugin() method.

Contributor

samsonasik commented Jun 28, 2013

@weierophinney is it a 'bug' ?

Contributor

webdevilopers commented Jun 26, 2014

I have form view helper behaviour that could confirm this issue for ZF 2.3.1.

Here is my viewHelper and the configuration:
https://gist.github.com/webdevilopers/b22f7471fd2b8d60cdea

As you can see I set shared services to false:

public function getViewHelperConfig()
{
    return array(
        'invokables' => array(
            'formelement'            => 'Application\Form\View\Helper\FormElement',
            'formautocompletehidden' => 'Application\Form\View\Helper\FormAutocompleteHidden'
        ),
        'shared' => array(
            'formelement'            => false,
            'formautocompletehidden' => false
        ),
    );
}

But when the form and both elements sharing the same view helper are rendered the setFoo() method only echoes once. The $foo variable is shared across both elements.

@Ocramius Ocramius added the bug label Jun 27, 2014

Contributor

webdevilopers commented Jul 2, 2014

@alexp assumes following solution posted on stackoverflow:
http://stackoverflow.com/questions/21272055/multiple-instances-of-view-helper-plugin

looking at Zend\View\Renderer\PhpRenderer::__call() I do think this is still a bug.
You can see it adds the helper to __pluginCache on the first call,
then each subsequent call is then reusing this.
It should allways try to fetch the plugin from the ViewHelperManager    
(just like the plugin() method does) as this will check the shared variable. 

https://github.com/zendframework/zf2/blob/master/library/Zend/View/Renderer/PhpRenderer.php#L395-401

Does this help @weierophinney , @samsonasik ?

samsonasik added a commit to samsonasik/zf2 that referenced this issue Oct 20, 2014

samsonasik added a commit to samsonasik/zf2 that referenced this issue Oct 20, 2014

@samsonasik samsonasik changed the title from set shared false for view_helpers doesn't work in ZF 2.1.5 to set shared false for view_helpers doesn't work Oct 20, 2014

samsonasik added a commit to samsonasik/zf2 that referenced this issue Oct 20, 2014

Contributor

samsonasik commented Oct 20, 2014

@weierophinney @webdevilopers I give it a try at PR #6786 , please let me know if I missed something.
Thank you ;)

samsonasik added a commit to samsonasik/zf2 that referenced this issue Nov 22, 2014

@Ocramius Ocramius added this to the 2.4.0 milestone Dec 5, 2014

@Ocramius Ocramius self-assigned this Dec 5, 2014

Member

Ocramius commented Dec 5, 2014

Handled in #6786

@Ocramius Ocramius closed this Dec 5, 2014

27cm added a commit to 27cm/beta that referenced this issue Apr 14, 2015

[!!!] (vendor): ZF2.3 -> ZF2.4
Переходим на версию Zend Framework 2.4
Баг zendframework/zendframework#4221 создавал сложности.

gianarb pushed a commit to zendframework/zend-view that referenced this issue May 15, 2015

gianarb pushed a commit to zendframework/zend-view that referenced this issue May 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment