Andy
Andy

Reputation: 5395

Custom configuration files in CakePHP 3

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:

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 

Upvotes: 5

Views: 3874

Answers (1)

Marijan
Marijan

Reputation: 1865

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') ?>

Upvotes: 7

Related Questions