6
votes

I have a separate config file for my CakePHP application which is loaded in the bootstrap.php. My question is, how can I access the Configuration's variables in my Controller? I.e. How can I perform the Configure::read('variable') function in Controller? Thanks!

4
I dont understand your question - you already gave the answer. Simply by using Configure::read('variable') in your controller you are accessing it as you wanted to. You just need to make sure you load your custom config files.mark
Yes, sorry for the silly question. I've missed something earlier I'm sure, but it's working now.zdtorok

4 Answers

2
votes

Please read the documentation. There it is explained quite well how to load custom config files: http://book.cakephp.org/2.0/en/development/configuration.html#loading-configuration-files

By default, as documented in the class itself, it will merge its configs with the already loaded configs.

Configure::read('variable')

then loads the content of the new config if it overwrites the app one.

3
votes

like this:

app/Config/example.php

<?php
$config = array(
    'MyConf' => array(
        'secret' => '1234',
        )
);

in your controller

Configure::load('example');
Configure::read('MyConf.secret');
3
votes

In my custom config file /app/Config/myconfig.php I define my config variables:

<?php

$config = array(
    'variable' => 'myValue'
);

In my Action I read can read the config file and get access of the variables:

Configure::load('myconfig', 'default');
$configValue = Configure::read('variable');
echo $configValue;  // myValue
3
votes

Here i have found another good solution.click here

Create a custom config First off, create a file in the app/config directory named however you want.

Example: my_app_settings.php

Now in this file you can add any settings you want using the $config array

    <?php
    $config['MyApp']['mysql_user'] = 'bob';
    $config['MyApp']['mysql_pass'] = 'foobar';

// etc etc

Next up, you need to tell your app to load this new config file. I tend to do it in the bootstrap.php to ensure it is loaded throughout the app (although if there is a better/more advisable place be sure to let me know)

<?php
// app/config/bootstrap.php

// snip

Configure::load('my_app_settings');

Now anywhere in your app you have access to your custom settings

<?php
$mysqlUser = Configure::read('MyApp.mysql_user');