Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

set shared false for view_helpers doesn't work #4221

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

set shared false for view_helpers doesn't work #4221

samsonasik opened this issue Apr 13, 2013 · 7 comments
Assignees
Milestone

Comments

@samsonasik
Copy link
Contributor

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
Copy link
Contributor Author

it is work if i used :

$this->plugin('mysimplehelper');

but if i used

$this->mysimplehelper();

it does'nt work

@weierophinney
Copy link
Member

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.

@samsonasik
Copy link
Contributor Author

@weierophinney is it a 'bug' ?

@webdevilopers
Copy link
Contributor

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
@webdevilopers
Copy link
Contributor

@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 set shared false for view_helpers doesn't work in ZF 2.1.5 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
@samsonasik
Copy link
Contributor Author

@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
@Ocramius
Copy link
Member

Ocramius commented Dec 5, 2014

Handled in #6786

@Ocramius Ocramius closed this as completed Dec 5, 2014
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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants