Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 777
Nishant Lad Avatar asked Dec 21 '12 13:12

Nishant Lad


3 Answers

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.

like image 167
Rick Calder Avatar answered Oct 07 '22 03:10

Rick Calder


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.

like image 45
NomanJaved Avatar answered Oct 07 '22 02:10

NomanJaved


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.

like image 20
Kishor Gavande Avatar answered Oct 07 '22 01:10

Kishor Gavande