簡體   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