Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 How to use global variables in the view

In ZF1 I used to declare variables in the application.ini

brandname = "Example"
weburl    = "http://www.example.com/"
assetsurl = "http://assets.example.com/"

And in the Bootstrap I did this so i could access them in the view

define('BRANDNAME', $this->getApplication()->getOption("brandname"));
define('WEBURL', $this->getApplication()->getOption("weburl"));
define('ASSETSURL', $this->getApplication()->getOption("assetsurl"));

Whats the ZF2 way to do this, I know that i can create an array in the local.php config file like:

return array(
    'example' => array(
        'brandname' => 'Example',
        'weburl'    => 'http://www.example.com/',
        'asseturl'  => 'http://assets.example.com/',
    ),
);

When I want to access that variable in the controller I can do

$config = $this->getServiceLocator()->get('Config');
$config['example']['brandname']);

So far so good... but how do i access this variable in the view? I don't want to create a view variable for it in every controller. And when i try the above in a view phtml file i get an error.

Zend\View\HelperPluginManager::get was unable to fetch or create an instance for getServiceLocator

Any ideas?

like image 738
Peter Avatar asked Jul 30 '13 14:07

Peter


2 Answers

You could create a sinmple view helper to act as a proxy for your config, (totally un tested).

Module.php

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'configItem' => function ($helperPluginManager) {
                $serviceLocator = $helperPluginManager->getServiceLocator();
                $viewHelper = new View\Helper\ConfigItem();
                $viewHelper->setServiceLocator($serviceLocator);

                return $viewHelper;
            }
        ),
    );
}

ConfigItem.php

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceManager; 

/**
 * Returns total value (with tax)
 *
 */
class ConfigItem extends AbstractHelper
{
    /**
     * Service Locator
     * @var ServiceManager
     */
    protected $serviceLocator;

    /**
     * __invoke
     *
     * @access public
     * @param  string
     * @return String
     */
    public function __invoke($value)
    {
        $config = $this->serviceLocator->get('config');
        if(isset($config[$value])) {
            return $config[$value];
        }

        return NULL;
        // we could return a default value, or throw exception etc here
    }

    /**
     * Setter for $serviceLocator
     * @param ServiceManager $serviceLocator
     */
    public function setServiceLocator(ServiceManager $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }
}

You could then do something like this in your view, assuming you have them set in your config of course :)

echo $this->configItem('config_key');
echo $this->configItem('web_url'); 

I would personally tend to just pass the values through to the view every time though, keeping the view a dumb as possible.

like image 90
Andrew Avatar answered Nov 16 '22 16:11

Andrew


I answered this before on a different post.

/* Inside your action controller method */
// Passing Var Data to Your Layout
$this->layout()->setVariable('stack', 'overflow');

// Passing Var Data to Your Template
$viewModel = new ViewModel(array( 'stack' => 'overflow' ));


/* In Either layout.phtml or {Your Template File}.phtml */
echo $this->stack; // Will print overview

That's it... No need to mess with view helpers, event manager, service manager, or anything else.

Enjoy!

like image 1
Timothy Perez Avatar answered Nov 16 '22 17:11

Timothy Perez