Defining Application Constants in Codeigniter
Asked Answered
A

7

12

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.

Ashil answered 8/5, 2012 at 7:17 Comment(3)
Please follow the guide it will help you codeigniter.com/user_guide/libraries/config.htmlStagger
It's not very useful, because, as it says in the guide, codeigniter will merge your file into the constants array, so you'll get the same result and openning one more file than usual, so it will be slower.Saum
Any files in application folders are for you to edit. Just avoid changing anything in system folder.Papillon
P
39

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.

Palliative answered 8/5, 2012 at 7:46 Comment(2)
The reason I avoid using application/config/constants.php for my custom application constants is I wanted to keep my custom application constants seperate from the General Code-Igniter defined constant. I am keeping my custom config parameters in a separate file application/config/my_config.php and not mixing it with application/config/config.php In the same line I wanted to keep my custom application constants in seperate file. For now I am going to use application/config/constants.php to define my constants. Thanks for your reply.Ashil
You can include_once 'my_constants.php' from constants.php to keep those files separate.Amersham
S
4

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'

Stelmach answered 13/8, 2014 at 13:56 Comment(0)
S
3

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.

Sampan answered 8/5, 2012 at 12:18 Comment(0)
S
1
    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';
Staciastacie answered 7/1, 2014 at 10:26 Comment(0)
G
0

Please refer this:

http://ellislab.com/forums/viewthread/56981/

Define variable in to constants & add value on array

$ORDER_STATUS  = array('0'=>'In Progress','1'=>'On Hold','2'
                 =>'Awaiting Review','3'=>'Completed','4'
                 =>'Refund Requested','5'=>'Refunded');
Grantley answered 17/1, 2014 at 9:28 Comment(0)
T
0

You can accomplish your goal by adding constants to your own config file, such as my_config.php.

You would save this file in the application/config folder, like this: application/config/my_config.php.

It is very common to have a separate config file for each application you write, so this would be easy to maintain and be understood by other CI programmers.

You can instruct CI to autoload this file or you can load it manually, as needed. See the CI manual on "Config class".

Turnspit answered 27/11, 2017 at 10:1 Comment(0)
S
0

Let me suggest that you use composer.json to autoload your own Constants.php file, like this:

enter image description here

Schoolhouse answered 2/8, 2022 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.