I am working with Zend-framework 3. I want to access the content of config files (local.php) in the controller. please explain. Thank you all in advance.
how to access local.php file from controller in zf3
841 Views Asked by Nitesh At
2
There are 2 best solutions below
0

You can do something like this (it's not tested):
// config.php
return [
'webhost' => 'www.example.com',
'database' => [
'adapter' => 'pdo_mysql',
'params' => [
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase',
],
],
];
And in your controller:
// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');
// Print a configuration datum (results in 'www.example.com')
echo $config->webhost;
Presume, you have this ZF3 package: zend-config If you dont have, you have to include it via composer
composer require zendframework/zend-config
After some research, I found the simplest way to access any config file. Just in your Module.php pass
$container->get('config')
as one of the argument to your controller's constructor and voila you can now access any property in the controller. That much simple. :-) Happy coding..!!