如何从Zend Framework 2中的布局/视图脚本中自动加载的配置文件访问配置? [英] How to access configs from autoloaded config files in a layout / view script in Zend Framework 2?

查看:203
本文介绍了如何从Zend Framework 2中的布局/视图脚本中自动加载的配置文件访问配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想/必须在 ZF1风格中管理一些设置,并向视图提供有关当前环境的信息。



/config/application.config.php

  return array(
...
'module_listener_options'=> array(
...
'config_glob_paths'=> array('config / autoload / {,*。} {global,local} .php')

);

/config/autoload/env.local.php

 返回数组(
//允许值:development,staging,live
'environment'=>'development'
);

在一个常见的视图脚本中,我可以通过控制器,因为控制器可以访问服务管理器等所有配置我需要:

 类MyController扩展AbstractActionController {

public function myAction ){
return new ViewModel(array(
'currentEnvironment'=> $ this-> getServiceLocator() - > get('Config')['environment'],
) );
}

}



如何在布局视图脚本中访问配置( / module / Application / view / layout / layout.phtml )?

解决方案

Crisp的建议



配置视图助手

 <?php 
namespace MyNamespace\View\Helper;

使用Zend\View\Helper\AbstractHelper;
使用Zend\View\HelperPluginManager作为ServiceManager;

类扩展AbstractHelper {

protected $ serviceManager;

public function __construct(ServiceManager $ serviceManager){
$ this-> serviceManager = $ serviceManager;
}

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

}

$ c>模块 class

  public function getViewHelperConfig(){
return array(
'factories'=> array(
'config'=> function($ serviceManager){
$ helper = new \MyNamespace\View\Helper\ Config($ serviceManager);
return $ helper;
},

);
}

布局视图脚本
$ b

  //使用$ this-> config()['environment']做任何你想做的事,例如
<?php
if($ this-> config()['environment'] =='live'){
echo $ this-> partial('partials / partial -foo.phtml');;
}
?>


I would like / have to manage some settings in ZF1 style and provide the view with the infomation about the current environment.

/config/application.config.php

return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);

/config/autoload/env.local.php

return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);

In a common view script I can do it over the controller, since the controllers have access to the Service Manager and so to all configs I need:

class MyController extends AbstractActionController {

    public function myAction() {
        return new ViewModel(array(
            'currentEnvironment' => $this->getServiceLocator()->get('Config')['environment'],
        ));
    }

}

Is it also possible to get the configs in a common view directly?

How can I access the configs in a layout view script (/module/Application/view/layout/layout.phtml)?

解决方案

(My implementation/interpretation of) Crisp's suggestion:

Config view helper

<?php
namespace MyNamespace\View\Helper;

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

class Config extends AbstractHelper {

    protected $serviceManager;

    public function __construct(ServiceManager $serviceManager) {
        $this->serviceManager = $serviceManager;
    }

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

}

Application Module class

public function getViewHelperConfig() {
    return array(
        'factories' => array(
            'config' => function($serviceManager) {
                $helper = new \MyNamespace\View\Helper\Config($serviceManager);
                return $helper;
            },
        )
    );
}

Layout view script

// do whatever you want with $this->config()['environment'], e.g.
<?php
if ($this->config()['environment'] == 'live') {
    echo $this->partial('partials/partial-foo.phtml');;
}
?>

这篇关于如何从Zend Framework 2中的布局/视图脚本中自动加载的配置文件访问配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆