How to access configs from autoloaded config files

2019-07-17 05:40发布

问题:

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)?

回答1:

(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');;
}
?>


回答2:

My implementation/interpretation of Sam's solution hint for the concret goal (to permit displaying the web analytics JS within the dev/staging environment):

DisplayAnalytics view helper

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

}

Application Module class

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

Layout view script

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


回答3:

So this solution becomes more flexible:

/config/application.config.php

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

/config/autoload/whatever.local.php and /config/autoload/whatever.global.php

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

ContentForEnvironment view helper

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

}

Application Module class

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

Layout view script

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