Zend framework2 如何在Zend Framework 2(zf2)中从视图访问应用程序配置?

Zend framework2 如何在Zend Framework 2(zf2)中从视图访问应用程序配置?,zend-framework2,zend-view,Zend Framework2,Zend View,我想从视图访问应用程序配置。在ZF 2中如何实现这一点?实际上,您不需要在视图中访问应用程序配置。在MVC中,视图只负责显示/呈现数据(输出),不应包含任何业务或应用程序逻辑 如果您确实想这样做,您只需在控制器中传递到视图,如下所示: <?php namespace YourModule\Controller; use Zend\View\Model\ViewModel; // ... public function anyAction() { $config = $this

我想从视图访问应用程序配置。在ZF 2中如何实现这一点?

实际上,您不需要在视图中访问应用程序配置。在MVC中,视图只负责显示/呈现数据(输出),不应包含任何业务或应用程序逻辑

如果您确实想这样做,您只需在控制器中传递到视图,如下所示:

<?php
namespace YourModule\Controller;

use Zend\View\Model\ViewModel;

// ...

public function anyAction()
{
    $config = $this->getServiceLocator()->get('config');
    $viewModel = new ViewModel();
    $viewModel->setVariables(array('config' => $config ));
    return $viewModel;
}

// ...
?>

因此,在view.phtml文件中

<div class="foo">
 ...
 <?php echo $this->config; ?>
 ...
</div>

...
...

您应该创建一个视图辅助对象

Config.php

<?php
namespace Application\View\Helper;

class Config extends \Zend\View\Helper\AbstractHelper
{
    public function __construct($config)
    {
        $this->key = $config;
    }

    public function __invoke()
    {
        return $this->config;
    }

}
然后,您可以在任何视图中使用配置变量

echo $this->config()->Section->key;
使用ZF2.2.1(不确定在以前的版本中是否可能),您可以使用TUT添加任何帮助程序

# module.config.php
...
'view_helpers' => array(
  'invokables' => array(
     'yourHelper' => 'Application\View\Helper\YourHelper',
  ),
),,

并编写
YourHelper
实现接口
Zend\ServiceManager\ServiceLocatorAwareInterface
。您必须实现接口的两种方法(简单setter和getter)。您现在可以访问服务定位器,并获取配置:

namespace Application\View\Helper;

class YourHelper implements Zend\ServiceManager\ServiceLocatorAwareInterface
{

  public function __invoke()
  {
    $config = $this->getServiceLocator()->getServiceLocator()->get('Config');

    // $config is the object you need

    return $config;
  }

  public function getServiceLocator()
  {
     return $this->serviceLocator;  
  }

  public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
  {
    $this->serviceLocator = $serviceLocator;  
    return $this;  
  }
}
更多信息请访问


在这里访问配置的助手

我创建了带有控制器插件和视图助手的模块,用于在控制器和视图中读取配置

通过composer安装后,您可以轻松使用它

echo $this->configHelp('key_from_config'); //read specific key from config 

$config = $this->configHelp(); //return config object Zend\Config\Config
echo $config->key_from_config;

从控制器操作中传递它?如果没有,请创建一个视图帮助器,并以与本例大致相同的方式向其注入配置,然后编写帮助器方法以访问所需内容。如何从视图帮助器访问它?请查看我链接的示例。用
config
替换
ModelService
,您就有了一个代理配置数组的助手。然后,您只需要为您的助手编写访问数组的方法,并在视图中调用这些方法。我在ZF1中也使用了相同的概念,但如果必须将配置数组传递给助手,我不确定如何在助手中获取配置数组?从哪里可以得到它?您将ServiceManager注入到您的ViewHelper中,然后您可以访问ServiceManager提供的所有内容。注入它,不要调用静态函数。虽然控制器中不应该有业务逻辑,但配置仍可能影响呈现/显示逻辑,例如,配置值告诉视图应该呈现什么主题。我仍然同意应该是控制器通过ViewModel尽可能细粒度地向视图发送配置值,而不是视图从ServiceManager中拉出全局配置不应该在项目配置中,而应该在模型中,这些应该被注入到视图中。
echo $this->configHelp('key_from_config'); //read specific key from config 

$config = $this->configHelp(); //return config object Zend\Config\Config
echo $config->key_from_config;