Configuration 如何从Zend Framework 2中布局/视图脚本中的自动加载配置文件访问配置?

Configuration 如何从Zend Framework 2中布局/视图脚本中的自动加载配置文件访问配置?,configuration,zend-framework2,production-environment,application-settings,Configuration,Zend Framework2,Production Environment,Application Settings,我想/必须在中管理一些设置,并为视图提供有关当前环境的信息 /config/application.config.php return array( ... 'module_listener_options' => array( ... 'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php') ) ); return array( /

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

/config/application.config.php

return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
/config/autoload/env.local.php

return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
在公共视图脚本中,我可以通过控制器执行此操作,因为控制器可以访问服务管理器以及我需要的所有配置:

class MyController extends AbstractActionController {

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

}
也可以直接在公共视图中获取配置吗

如何访问布局视图脚本(
/module/Application/view/layout/layout.phtml
)中的配置?
(我的实现/解释):

配置视图帮助程序

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

}
<?php
namespace MyNamespace\View\Helper;

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

class DisplayAnalytics extends AbstractHelper {

    protected $serviceManager;

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

    public function __invoke() {
        if ($this->serviceManager->getServiceLocator()->get('Config')['environment'] == 'development') {
            $return = $this->view->render('partials/partial-bar.phtml');
        }
        return $return;
    }

}
<?php
namespace MyNamespace\View\Helper;

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

class ContentForEnvironment extends AbstractHelper {

    protected $serviceManager;

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

    /**
     * Returns rendered partial $partial,
     * IF the current environment IS IN $whiteList AND NOT IN $blackList,
     * ELSE NULL.
     * Usage examples:
     * Partial for every environment (equivalent to echo $this->view->render($partial)):
     *  echo $this->contentForEnvironment('path/to/partial.phtml');
     * Partial for 'live' environment only:
     *  echo $this->contentForEnvironment('path/to/partial.phtml', ['live']);
     * Partial for every environment except 'development':
     *  echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']);
     * @param string $partial
     * @param array $whiteList
     * @param array $blackList optional
     * @return string rendered partial $partial or NULL.
     */
    public function __invoke($partial, array $whiteList, array $blackList = []) {
        $currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment'];
        $content = null;
        if (!empty($whiteList)) {
            $content = in_array($currentEnvironment, $whiteList) && !in_array($currentEnvironment, $blackList)
                ? $this->view->render($partial)
                : null
            ;
        } else {
            $content = !in_array($currentEnvironment, $blackList)
                ? $this->view->render($partial)
                : null
            ;
        }
        return $content;
    }

}
布局视图脚本

// do whatever you want with $this->config()['environment'], e.g.
<?php
if ($this->config()['environment'] == 'live') {
    echo $this->partial('partials/partial-foo.phtml');;
}
?>
<?php echo $this->displayAnalytics(); ?>
<?php echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']); ?>
//用$this->config()['environment']做任何你想做的事,例如。

我的具体目标的实现/解释(允许在开发/登台环境中显示web analytics JS):

DisplayAnalytics视图帮助程序

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

}
<?php
namespace MyNamespace\View\Helper;

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

class DisplayAnalytics extends AbstractHelper {

    protected $serviceManager;

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

    public function __invoke() {
        if ($this->serviceManager->getServiceLocator()->get('Config')['environment'] == 'development') {
            $return = $this->view->render('partials/partial-bar.phtml');
        }
        return $return;
    }

}
<?php
namespace MyNamespace\View\Helper;

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

class ContentForEnvironment extends AbstractHelper {

    protected $serviceManager;

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

    /**
     * Returns rendered partial $partial,
     * IF the current environment IS IN $whiteList AND NOT IN $blackList,
     * ELSE NULL.
     * Usage examples:
     * Partial for every environment (equivalent to echo $this->view->render($partial)):
     *  echo $this->contentForEnvironment('path/to/partial.phtml');
     * Partial for 'live' environment only:
     *  echo $this->contentForEnvironment('path/to/partial.phtml', ['live']);
     * Partial for every environment except 'development':
     *  echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']);
     * @param string $partial
     * @param array $whiteList
     * @param array $blackList optional
     * @return string rendered partial $partial or NULL.
     */
    public function __invoke($partial, array $whiteList, array $blackList = []) {
        $currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment'];
        $content = null;
        if (!empty($whiteList)) {
            $content = in_array($currentEnvironment, $whiteList) && !in_array($currentEnvironment, $blackList)
                ? $this->view->render($partial)
                : null
            ;
        } else {
            $content = !in_array($currentEnvironment, $blackList)
                ? $this->view->render($partial)
                : null
            ;
        }
        return $content;
    }

}
布局视图脚本

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

因此变得更加灵活:

/config/application.config.php

return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
/config/autoload/whatever.local.php和/config/autoload/whatever.global.php

return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
return array(
    ...
    'module_listener_options' => array(
        ...
        'config_glob_paths' => array('config/autoload/{,*.}{global,local}.php')
    )
);
return array(
    // allowed values: development, staging, live
    'environment' => 'development'
);
ContentForEnvironment视图帮助程序

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

}
<?php
namespace MyNamespace\View\Helper;

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

class DisplayAnalytics extends AbstractHelper {

    protected $serviceManager;

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

    public function __invoke() {
        if ($this->serviceManager->getServiceLocator()->get('Config')['environment'] == 'development') {
            $return = $this->view->render('partials/partial-bar.phtml');
        }
        return $return;
    }

}
<?php
namespace MyNamespace\View\Helper;

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

class ContentForEnvironment extends AbstractHelper {

    protected $serviceManager;

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

    /**
     * Returns rendered partial $partial,
     * IF the current environment IS IN $whiteList AND NOT IN $blackList,
     * ELSE NULL.
     * Usage examples:
     * Partial for every environment (equivalent to echo $this->view->render($partial)):
     *  echo $this->contentForEnvironment('path/to/partial.phtml');
     * Partial for 'live' environment only:
     *  echo $this->contentForEnvironment('path/to/partial.phtml', ['live']);
     * Partial for every environment except 'development':
     *  echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']);
     * @param string $partial
     * @param array $whiteList
     * @param array $blackList optional
     * @return string rendered partial $partial or NULL.
     */
    public function __invoke($partial, array $whiteList, array $blackList = []) {
        $currentEnvironment = $this->serviceManager->getServiceLocator()->get('Config')['environment'];
        $content = null;
        if (!empty($whiteList)) {
            $content = in_array($currentEnvironment, $whiteList) && !in_array($currentEnvironment, $blackList)
                ? $this->view->render($partial)
                : null
            ;
        } else {
            $content = !in_array($currentEnvironment, $blackList)
                ? $this->view->render($partial)
                : null
            ;
        }
        return $content;
    }

}
布局视图脚本

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


我建议使用一个注入了配置的视图帮助器,在我刚才给出的答案->中用config替换模型类,并使用你的帮助器作为代理,当然是@Crisp提供的方法,但是对于为什么你真的需要它们,会有强烈的疑问。一个视图应该呈现,没有别的,它甚至不应该为任何单个配置而烦恼。这就是管制员的工作。你到底想做什么?你当然可以使用这两种方法,但我倾向于按照Sam的建议将其排除在视图之外:)@Sam我想允许在开发/登台环境中显示web analytics JS。这绝对是viewHelper
displayAnalytics()的解决方案
-viewHelper将通过ServiceManager访问配置,然后输出空字符串或功能分析代码。参见crisp提供的答案;)