【问题标题】:How to access configs from autoloaded config files in a layout / view script in Zend Framework 2?如何在 Zend Framework 2 的布局/视图脚本中从自动加载的配置文件访问配置?
【发布时间】:2013-04-11 14:03:33
【问题描述】:

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

/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'
);

在一个通用视图脚本中,我可以通过控制器执行此操作,因为控制器可以访问服务管理器,因此可以访问我需要的所有配置:

class MyController extends AbstractActionController {

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

}

是否也可以直接在公共视图中获取配置?

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

【问题讨论】:

  • 我建议使用配置注入的视图助手,在我刚刚在这里给出的答案中将模型类替换为 config -> stackoverflow.com/questions/16082529/…,并使用您的助手作为代理
  • 绝对是@Crisp 提供的方法,但是对于您为什么真的需要它们会有很大的疑问。视图应该呈现,没有别的,它甚至不应该为任何单个配置而烦恼。那是控制器的工作。你到底想做什么?
  • 你当然可以同时使用这两种方法,但我倾向于按照 Sam 的建议将其置于视野之外 :)
  • @Sam 我想允许在开发/登台环境中显示网络分析 JS。
  • 绝对是 viewHelper displayAnalytics() 的解决方案 - viewHelper 将通过 ServiceManager 访问配置,然后输出空字符串或功能分析代码。请参阅清晰提供的答案;)

标签: configuration zend-framework2 production-environment application-settings


【解决方案1】:

(我的实现/解释)Crisp's suggestion

配置视图助手

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

}

应用程序Module

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

布局视图脚本

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

【讨论】:

    【解决方案2】:

    我对 Sam's solution hint 的具体实现/解释(以允许在开发/暂存环境中显示 Web 分析 JS):

    DisplayAnalytics 视图助手

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

    应用程序Module

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

    布局视图脚本

    <?php echo $this->displayAnalytics(); ?>
    

    【讨论】:

      【解决方案3】:

      所以this solution 变得更加灵活:

      /config/application.config.php

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

      /config/autoload/whatever.local.php/config/autoload/whatever.global.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 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;
          }
      
      }
      

      应用程序Module

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

      布局视图脚本

      <?php echo $this->contentForEnvironment('path/to/partial.phtml', [], ['development', 'staging']); ?>
      

      【讨论】:

        猜你喜欢
        • 2013-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        相关资源
        最近更新 更多