Custom configuration files in CakePHP 3

3.7k Views Asked by At

I have a CakePHP 3.3.14 application where I've created 2 subdirectories, webroot/data/downloads/ and webroot/data/master

I want to put these paths in a custom configuration file and reference them in a Controller. But I can't see how to do this.

I've followed the documentation on Configuration but it's not very clear.

So what I've done:

  • Created config/my_config.php
  • The above file defines an array:

    return [ 'downloadsPath' => 'webroot/data/downloads/', 'masterPath' => 'webroot/data/master/' ];
    
  • In config/bootstrap.php I've put: Configure::load('my_config', 'default');

How do I then use this in a Controller? If I put Configure::read('my_config.masterPath'); it gives an error saying: Class 'App\Controller\Configure' not found

If I add use Cake\Core\Configure; to the top of my Controller, that clears the error but the return value is null:

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

There are 1 best solutions below

3
Marijan On

Loading another config file just extends the default App.config. So just use \Cake\Core\Configure::read('masterPath') and you are good.

EDIT

If it is your goal to have different config paths you could do it like this:

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

Then use the config like this:

<?= \Cake\Core\Configure::read('MyConfig.masterPath') ?>