CakePHP 3中的自定义配置文件

And*_*ndy 4 php configuration cakephp cakephp-3.0 cakephp-3.3

我有一个CakePHP 3.3.14应用程序,我创建了2个子目录,webroot/data/downloads/webroot/data/master

我想将这些路径放在自定义配置文件中,并在Controller中引用它们.但我看不出怎么做.

我已经按照配置文档进行了操作,但不是很清楚.

所以我做了什么:

  • 创建 config/my_config.php
  • 上面的文件定义了一个数组:

    return [ 'downloadsPath' => 'webroot/data/downloads/', 'masterPath' => 'webroot/data/master/' ];
    
    Run Code Online (Sandbox Code Playgroud)
  • config/bootstrap.php我说:Configure::load('my_config', 'default');

我如何在控制器中使用它?如果我把Configure::read('my_config.masterPath');它给出一个错误说:没有找到类'App\Controller\Configure'

如果我添加use Cake\Core\Configure;到控制器的顶部,则清除错误但返回值为null:

debug(Configure::read('my_config.masterPath')); // null 
Run Code Online (Sandbox Code Playgroud)

Mar*_*ary 7

加载另一个配置文件只会扩展默认值App.config.所以只需使用\Cake\Core\Configure::read('masterPath'),你就是好人.

编辑

如果你的目标是拥有不同的配置路径,你可以这样做:

// my_config.php
return [
    'MyConfig' => [
        'masterPath' => '...',
        ...
    ]
];
Run Code Online (Sandbox Code Playgroud)

然后像这样使用配置:

<?= \Cake\Core\Configure::read('MyConfig.masterPath') ?>
Run Code Online (Sandbox Code Playgroud)

  • 你还需要在`bootstrap.php`中添加`Configure :: load('my_config','default');` (4认同)