phpzend-frameworkpartial-views

Pass a value from controller to partial in ZF2


I want to pass some value from controller to view partial, basically, view partial is set using the placeholder in Application\Module.php, here is the code that does it.

<?php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;
use Zend\ServiceManager\ServiceManager;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'initBreadcrumbAndActionButtonWidget'));
        $moduleRouteListener->attach($eventManager);
        $serviceManager = $e->getApplication()->getServiceManager();
    }

    public function initBreadcrumbAndActionButtonWidget(\Zend\Mvc\MvcEvent $e)
    {
        $serviceManager = $e->getApplication()->getServiceManager();
        $config = $serviceManager->get('config');
        $viewHelperManager = $this->getViewHelperManager($e->getApplication()->getServiceManager());
        $partialHelper = $viewHelperManager->get('partial');
        $partial = $partialHelper('widgets/breadcrumb', array(
            'config' => $config,
            'actionButtons' => $config['action-buttons']
        ));
        $placeHolder = $viewHelperManager->get('placeholder');
        $placeHolder('widgets/breadcrumb')->set($partial);
    }

    public function getViewHelperManager($serviceManager)
    {
        $stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view/')));
        $resolver = new Resolver\AggregateResolver();
        $resolver->attach($stack);
        $renderer = new PhpRenderer();
        $renderer->setResolver($resolver);
        $viewHelperManager = $serviceManager->get('ViewHelperManager');
        return $viewHelperManager;
    }

    public function getConfig()
    {
        return array_merge_recursive(
            include __DIR__ . '/config/module.config.php',
        );
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

It is all working good till here, however now i want to pass a value from controller to the partial widgets/breadcrumb, after several attempt i thought implementing a ViewHelper could be a solution, and i implemented following view helper.

<?php
namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class Variable extends AbstractHelper
{
    protected $data;

    public function __invoke()
    {
        return $this;
    }

    public function set($identifier, $data)
    {
        $this->data[$identifier] = $data;
        return $this;
    }

    public function get($identifier)
    {
        return $this->data[$identifier];
    }
}

In controller i assign the value to ViewHelper like this.

$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
$variable = $viewHelperManager->get('variable');
$variable->set('stack', 'overflow');

And in partial, to access the value i use the get method.

$this->variable()->get('stack');

Now my problem is that get() is rendered first hence the value set in controller has no effect.

My question is how can i make the set() gets rendered first so i can have the value in partial, or if there is any better approach of passing the value to partial please suggest.

Thanks.


Solution

  • Baaah, i called the view helper in wrong action, it worked when i moved the $variable->set('stack', 'overflow'); in the action where i wanted the value to pass.

    Not sure if there can be a better solution, but this does the job for me to pass the value after i found no way in ZF2 that satisfies my requirement.