I wanted to access the application config from a view. How can I achieve that in ZF 2?
Asked
Active
Viewed 1.3k times
6
-
Pass it from your controller action? If not, create a view helper and inject it with configuration in much the same manner as this example http://stackoverflow.com/questions/16082529/calling-a-method-in-model-from-layout-in-zendframework-2/16082884#16082884 then write your helper methods to access what you need. – Crisp May 14 '13 at 12:11
-
How can I access it from a view helper ? – Omar May 14 '13 at 12:30
-
Take a look at the example I linked. Substitute `ModelService` with `config` and you have a helper that proxy's to the Config array. You then just need to write methods for your helper that access the array, and call those in your view. – Crisp May 14 '13 at 12:33
-
I've done the same concept in ZF1 but I'm not sure How to get the Config array inside the helper, If I have to pass it to the helper? From where I can get it? – Omar May 14 '13 at 12:37
-
I've tried $sm = ServiceManagerFactory::getServiceManager(); $sm->get('Config'); but it leaves me with an exception Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for ApplicationConfig – Omar May 14 '13 at 12:38
-
1You inject the ServiceManager into your ViewHelper and then you gain access to everything that the ServiceManager has to offer. Inject it, don't call a static function. – Sam May 14 '13 at 12:50
3 Answers
9
Actually you shouldn't need to access application config inside a view. In MVC, views just responsible for displaying/rendering data (output) and shouldn't contain any business or application logic.
If you really want to do that you can simply pass to view in your controller something like this:
<?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>

edigu
- 9,878
- 5
- 57
- 80
-
2Whilst you shouldn't have business logic in your controller, configuration may still affect rendering/display logic, e.g. a configuration value tells the view what theme it should render. I still agree that it should be the controller that sends configuration values to the view—as fine grained as possible—via the ViewModel, rather than the view pulling the global configuration out of the ServiceManager for example. – Ezequiel Muns May 15 '13 at 01:30
-
The "template ID" should not be in project configuration but in your models and those should be injected into the view. – Rob Apr 10 '18 at 13:05
8
You should 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 config variables in any view.
echo $this->config()->Section->key;

Cagatay Gurturk
- 7,186
- 3
- 34
- 44

Mikhail Prosalov
- 4,155
- 4
- 29
- 41
0
I created the module with controller plugin and view helper for reading a config in controllers and views. GitHub link __ Composer link
After installation via composer you can use it easily.
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;

tasmaniski
- 4,767
- 3
- 33
- 65