如何从Zend框架中布局/视图脚本中自动加载的配置文件中访问配置



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

/config/application.config.php

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

/config/自动装载/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)中的配置?

(我对)Crisp的建议的实现/解释:

配置视图帮助器

<?php
namespace MyNamespaceViewHelper;
use ZendViewHelperAbstractHelper;
use ZendViewHelperPluginManager 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 MyNamespaceViewHelperConfig($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');;
}
?>

我对Sam的具体目标(允许在开发/登台环境中显示web分析JS)的解决方案提示的实现/解释:

DisplayAnalytics视图助手

<?php
namespace MyNamespaceViewHelper;
use ZendViewHelperAbstractHelper;
use ZendViewHelperPluginManager 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 MyNamespaceViewHelperDisplayAnalytics($serviceManager);
                return $helper;
            }
        )
    );
}

布局视图脚本

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

所以这个解决方案变得更加灵活:

/config/application.config.php

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

/config/autolload/whatever.local.php/config/autolload/whatever.global.php

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

ContentForEnvironment view helper

<?php
namespace MyNamespaceViewHelper;
use ZendViewHelperAbstractHelper;
use ZendViewHelperPluginManager 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 MyNamespaceViewHelperContentForEnvironment($serviceManager);
                return $helper;
            }
        )
    );
}

布局视图脚本

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

相关内容

  • 没有找到相关文章

最新更新