5

我有一个 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/' ];
    
  • config/bootstrap.php我放:Configure::load('my_config', 'default');

然后我如何在控制器中使用它?如果我把Configure::read('my_config.masterPath');它给出一个错误说:Class 'App\Controller\Configure' not found

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

debug(Configure::read('my_config.masterPath')); // null 
4

1 回答 1

7

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

编辑

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

// my_config.php
return [
    'MyConfig' => [
        'masterPath' => '...',
        ...
    ]
];

然后像这样使用配置:

<?= \Cake\Core\Configure::read('MyConfig.masterPath') ?>
于 2017-03-28T11:44:12.907 回答