How to access application configuration from a view in Zend Framework 2 (zf2)?

I wanted to access the application configuration from the view. How can I achieve this in ZF 2?

+6
source share
4 answers

In fact, you do not need to access the application configuration inside the view. In MVC, views are simply responsible for displaying / rendering data (output) and should not contain any business logic or application.

If you really want to do this, you can simply pass something like this to your controller:

<?php namespace YourModule\Controller; use Zend\View\Model\ViewModel; // ... public function anyAction() { $config = $this->getServiceLocator()->get('config'); $viewModel = new ViewModel(); $viewModel->setVariables(array('config' => $config )); return $viewModel; } // ... ?> 

So in your view.phtml file;

 <div class="foo"> ... <?php echo $this->config; ?> ... </div> 
+9
source

You must create a view helper.

config.php

 <?php namespace Application\View\Helper; class Config extends \Zend\View\Helper\AbstractHelper { public function __construct($config) { $this->key = $config; } public function __invoke() { return $this->config; } } 

Module.php or theme.config.php

 return array( 'helpers' => array( 'factories' => array( 'config' => function ($sm) { return new \Application\View\Helper\Config( $sm->getServiceLocator()->get('Application\Config')->get('config') ); }, ) ), ); 

Then you can use the configuration variables in any view.

 echo $this->config()->Section->key; 
+8
source

With ZF 2.2.1 (not sure if this is possible with previous versions), you can simply add your assistant by injecting something

 # module.config.php ... 'view_helpers' => array( 'invokables' => array( 'yourHelper' => 'Application\View\Helper\YourHelper', ), 

), ...

And write YourHelper using the Zend\ServiceManager\ServiceLocatorAwareInterface interface. You will need to implement two interface methods (simple setter and getters). Now you can contact the service locator and get the configuration:

 namespace Application\View\Helper; class YourHelper implements Zend\ServiceManager\ServiceLocatorAwareInterface { public function __invoke() { $config = $this->getServiceLocator()->getServiceLocator()->get('Config'); // $config is the object you need return $config; } public function getServiceLocator() { return $this->serviceLocator; } public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; return $this; } } 

Additional information at http://robertbasic.com/blog/working-with-custom-view-helpers-in-zend-framework-2

Assistant to access config here https://gist.github.com/elvisciotti/6592837

+7
source

I created a module with a controller plugin and will consider an assistant for reading configurations in controllers and views. GitHub Link __ Composer Link

After installation through the composer, you can easily use it.

 echo $this->configHelp('key_from_config'); //read specific key from config $config = $this->configHelp(); //return config object Zend\Config\Config echo $config->key_from_config; 
0
source

Source: https://habr.com/ru/post/944964/


All Articles