如何从zf3

时间:2016-10-14 13:08:58

标签: zend-framework config zf3

我正在使用Zend-framework 3.我想访问控制器中配置文件(local.php)的内容。请解释。谢谢大家。

2 个答案:

答案 0 :(得分:0)

你可以这样做(它没有经过测试):

// config.php
return [
   'webhost'  => 'www.example.com',
   'database' => [
     'adapter' => 'pdo_mysql',
     'params'  => [
        'host'     => 'db.example.com',
        'username' => 'dbuser',
        'password' => 'secret',
        'dbname'   => 'mydatabase',
     ],
   ],
];

在你的控制器中:

// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');

// Print a configuration datum (results in 'www.example.com')
echo $config->webhost;

假设你有这个ZF3包:zend-config 如果你没有,你必须通过composer

包含它
composer require zendframework/zend-config

答案 1 :(得分:0)

经过一番研究,我找到了访问任何配置文件的最简单方法。只需在您的Module.php中传递$container->get('config')作为控制器构造函数的参数之一,您现在可以访问控制器中的任何属性。那很简单。 :-)快乐编码.. !!