Search code examples
phpcodeigniterconstants

how to define global constants in codeigniter in constants.php


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');

Solution

  • 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.