Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining Application Constants in Codeigniter

I want to know a clean way of defining Application Constants in Codeigniter. I don't want to change any native file of codeigniter. Hence I don't want to define it in application/config/constants.php as when I need to migrate to newer version of code-igniter I will not be able to copy the native files of codeigniter directly.

I created a file application/config/my_constants.php and defined my constants there. 'define('APP_VERSION', '1.0.0');'

I loaded it using $this->load->config('my_constants');

But I am getting a error

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

Please suggest me a clean way of defining application level constants in code-igniter.

like image 221
piyush Avatar asked May 08 '12 07:05

piyush


4 Answers

Not using application/config/constants.php is nonsense! That is the only place you should be putting your constants. Don't change the files in system if you are worried about upgrading.

like image 97
Mischa Avatar answered Nov 06 '22 00:11

Mischa


    config file (system/application/config/config.php) to set configuration related variables.

    Or use 

    constant file (system/application/config/constants.php) to store site preference constants.


  =======================
  DEFINE WHAT YOU WANT
  =======================
  $config['index_page'] = 'home';
  $config['BASEPATH'] = 'PATH TO YOUR HOST';
like image 30
Khandad Niazi Avatar answered Nov 05 '22 23:11

Khandad Niazi


just a complete answer. (None of the answers show how to use the constants that were declared)

The process is simple:

  1. Defining a constant. Open config/constants.php and add the following line:

    define('SITE_CREATOR', 'John Doe')

  2. use this constant in another file using:

    $myVar = 'This site was created by '.SITE_CREATOR.' Check out my GitHub Profile'

like image 4
IcyFlame Avatar answered Nov 06 '22 00:11

IcyFlame


Instead of using define(), your my_constants.php file should look something like this:

$config['app_version'] = '1.0.0';

Be careful with naming the array key though, you don't want to conflict with anything.

If you need to use define(), I would suggest doing it in the main index.php file, though you will still need to use APP_VERSION to get the value.

like image 3
Nick Pyett Avatar answered Nov 06 '22 00:11

Nick Pyett