how to define global constants in codeigniter in constants.php
Solution 1
You don't need to load constants anywhere, they're automatically autoloaded by CI itself, that is the point of them. To use them keep in mind they are constants and not a variable as you're used to with the $. So in your case after defining them, anywhere in your application you could for example use:
if($user->userType == SuperAdmin)
{
do something here;
}
Note that the SuperAdmin is written without a $ preceding it and not inside quotes. Again, after defining them in the constants file you need to do nothing else to begin using them in your application.
Solution 2
First of all use the conventions that you must define the constants in capital letters. Like
define('SUPERADMIN','1');
define('ADMIN','2');
define('CITYADMIN' '3');
you can use the constants in controller like
if($user->userType == SUPERADMIN)
{
//Your code ...
}
and if you want to use in html file/ view file in codeigniter you can use constants in html file like
<?php echo SUPERADMIN; ?>
You can access the constants in this way.
Solution 3
Always follow naming convention for constant in only capital letter. If you declared constant in application/config/constants.php file then it will available through out your application.
Nishant Lad
Php developer currently working in codeigniter framework
Updated on July 09, 2022Comments
-
Nishant Lad almost 2 years
I have stored my global constants in config->constants.php but it creating problem and throwing error I have defined it like this, how to call it in controller, code
define('SuperAdmin','1'); define('Admin','2'); define('CityAdmin' '3'); define('SupportAdmin','4'); define('RestaurantUser','5'); define('FrontUser','6'); define('Other','7');