I am loading the google-api-php-client library (for oauth 2.0 usage) in my codeigniter project. I would like to have a series of configuration values defined in a config file so that they can be used with this library. I am, however, noticing that the configuration info for the library is loaded before the config file that I have defined.
For example, in autoload.php, I have set the config autoloads as follows:
$autoload['config'] = array('my_config_file');
Within my_config_file.php I have a series of define statements to set the config values:
define('GOOGLE_OAUTH_APPLICATION_NAME','My Application Name');
define('GOOGLE_OAUTH_CLIENT_ID','My App Client ID');
define('GOOGLE_OAUTH_CLIENT_SECRET','My App Client Secret');
I would like to use these in the config for the google-api-php-client library:
global $apiConfig;
$apiConfig = array(
'application_name' => GOOGLE_OAUTH_APPLICATION_NAME,
'oauth2_client_id' => GOOGLE_OAUTH_CLIENT_ID,
'oauth2_client_secret' => GOOGLE_OAUTH_CLIENT_SECRET
);
After doing this (and some debugging), I’ve determined that the config file for the library is executed before the autoloaded config file. This is further shown by the errors I get:
Notice: Use of undefined constant GOOGLE_OAUTH_APPLICATION_NAME ...
Notice: Use of undefined constant GOOGLE_OAUTH_CLIENT_ID ...
Notice: Use of undefined constant GOOGLE_OAUTH_CLIENT_SECRET ...
How do I get it so that these global config constants are defined before the library config is loaded (thus resolving this issue)?
Best practice is to create a separate config file for a library; say
application/config/oauth.php.That config file gets loaded in the constructor of your library with
$this->config->load('oauth');. Of course, you can also just include it in the autoload array.In your library, you then call the config items thusly:
Cheers.