繁体   English   中英

ZF2如何在视图中使用全局变量

[英]ZF2 How to use global variables in the view

在ZF1中,我曾经在application.ini中声明变量

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

在Bootstrap中我做了这个,所以我可以在视图中访问它们

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

什么是ZF2方式,我知道我可以在local.php配置文件中创建一个数组,如:

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

当我想在控制器中访问该变量时,我可以这样做

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

到目前为止一切都很好......但我如何在视图中访问此变量? 我不想在每个控制器中为它创建一个视图变量。 当我在视图phtml文件中尝试以上操作时,我收到错误。

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

有任何想法吗?

你可以创建一个简单的视图助手来充当你的配置的代理,(完全未经测试)。

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;
    }
}

然后你可以在你的视图中做这样的事情,假设你在你的配置中设置了它们:)

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

我个人倾向于每次都将值传递给视图,尽可能保持视图愚蠢。

我之前在另一篇文章中回答了这个问题。

/* 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

就是这样......不需要混淆视图助手,事件管理器,服务管理器或其他任何东西。

请享用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM