Welcome Guest, Not a member yet? Register   Sign In
constants and custom config settings
#1

[eluser]fivestringsurf[/eluser]
I have always (when not using CI) typically placed constants and config settings in one file "config.php" and included it at the top of each php page. This has given me access to things like: live vs. local status, constants like: max character length, locations, etc...

Where's a good place to include this basic initialization stuff in CI sticking with MVC philosophy of course?
placing constants in ci's config didn't work...and loading a library with this stuff seems kind of sloppy and misplaced.

Any advice would be appreciated; thank you in advance.
#2

[eluser]toopay[/eluser]
place your config files under 'application/config/', then you can load that config file in your controller class(for example) with
Code:
...
public function __construct()
{
    parent::__construct();
    $this->config->load('filename');
}
...
#3

[eluser]InsiteFX[/eluser]
1) place your config file in application/config/filename.php
2) for constants you can add them to application/config/constants.php

The constants file will be autoloaded by CodeIgniter.

To load your config file to as toopay has stated above.

InsiteFX
#4

[eluser]fivestringsurf[/eluser]
thanks guys, you lead me to some new discoveries/solutions (for me in CI). For anyone else interested...

For config array...I extended the controller class and included my custom config file just as advised in the comments.

For constants I didn't want to add to the existing CI constants file so..
I wrote this little guy:
Code:
class config{
    function constants(){
        require(APPPATH.'config/MY_constants'.EXT);
    }
}
And wrote a hook to load the above script pre-controller.
These solutions allow me to have my own separate files for my custom config and constants. Separate from CodeIgniters default files.
works like a charm!
#5

[eluser]InsiteFX[/eluser]
As I stated above the application/config/constants.php is for loading constants.

You just add your own constants at the bottom of the file and CodeIgniter will automatically load it.

InsiteFX
#6

[eluser]fivestringsurf[/eluser]
Again, thanks for the insight, your solution does work, BUT I wanted to have my own file containing my own custom constants. The hooks method was the only way I could accomplish that.
If that's a frowned upon way (even though it works fine) please explain. Either way learning/using hooks was new to me...and in the least I'm psyched to learn some new CI stuff!

Thanks for all your input guys
#7

[eluser]rizkhan[/eluser]
[quote author="InsiteFX" date="1302181063"]1) place your config file in application/config/filename.php
2) for constants you can add them to application/config/constants.php

The constants file will be autoloaded by CodeIgniter.

To load your config file to as toopay has stated above.

InsiteFX[/quote]

I have created a database_tables.php in config folder which contains constants for all database table names and loaded this file in controller class

$this->config->load('database_tables');

but when i try to access in model class for my db query table name then it shows me error

An Error Was Encountered

Your application/config/database_tables.php file does not appear to contain a valid configuration array.

for constant do we need to declare an array for constant?

thanks for your help
#8

[eluser]fivestringsurf[/eluser]
@rizkhan

After experimenting with CI's means of handling configs and constants for several hours here's my take on it:

Things loaded via config->load are part of ci's config array and must be saved as such. If you look in application>config>config.php you will see the defaults...things like:
Code:
$config['uri_protocol']    = 'AUTO';
later if you want to use the config you can recall it this way:
Code:
$this->config->item('uri_protocol')

you can add to the $config array in config.php or make your own file and simply load them from another file like your doing.

Constants are handled a wee bit differently (and no not in a special CI-type-array). The easiest way to define your site-wide constants is to do as @initeFX said and add them to the application>config>constants.php file. Note: there is no native class/method for loading them (that I am aware of) from a custom (non constants.php) file so I got creative... Now...like you I wanted them (my constants) separate and in their own file. That is why I turned on "hooks" and wrote a hook to load my constants pre-controller...this way they are available site-wide.

In the end I realized that i didn't need to add to the $config array and that i wanted all custom constants.
#9

[eluser]k2zs[/eluser]
[quote author="InsiteFX" date="1302257635"]As I stated above the application/config/constants.php is for loading constants.

You just add your own constants at the bottom of the file and CodeIgniter will automatically load it.

InsiteFX[/quote]

How do you call those values in a controllers function?
#10

[eluser]fivestringsurf[/eluser]
constants are called simply by using their name. Note...no qoutes and no $.
They are special...just like the kids that ride the short bus.
example:

Code:
// constants.php
define('MY_CONSTANT', 'whoopeee this value can not be overwritten');

//controller
echo 'some text '. MY_CONSTANT. ' some more text';

...oh yeah all caps is customary but not a requirement




Theme © iAndrew 2016 - Forum software by © MyBB