Best Practice to set constant in Yii

Hello all,

I want to create a global constant that can be accessed from various controllers. So what is the best approach to set a constant in Yii?

Thanks before.

I(personally) am using a static class for constants. I dont know if this is best. I’d like to see any other ideas.

  class MegalanConstants


      const ORDER_MTEL_REPORT_GOOD_ID = 5031;


      const CASH_DEBITCARD = 44;


      const ORDERSTATE_PAYED = 4;



And using this way :


$invoice->mldocstat_id = MegalanConstants::MLDOCSTAT_Completed;


You can use params

I don’t know if it is ‘Best Practice’ but you can just set global variables in the controller component. Since most of your controllers extend this class you have access to the variables.

Example - (note that Yii uses this approach by default)

class Controller extends CController



	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',

	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.


	public $layout='//layouts/column1';


	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.


	public $menu=array();


	 * @var array the breadcrumbs of the current page. The value of this property will

	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

	 * for more details on how to specify this property.


	public $breadcrumbs=array();


	public $entryUrl = '/eventAbstract/admin';  // set this in each of the menus 


	public $cssStyles =    'Generic Box=generic_box;PageImage=pageImage;Float Left=left;Float Right=right;Span8=span-8;Span14=span-14;1pxBorder=border1px';


	public $description = 'Page description';


	public $keywords = 'Page keywords';

Then you can access these globals like so

// in controller or view

$this->description = "some text here";

// pull it from a database

$this->description = SomeModel::model()->findByPk($id)->description;

// do something with it

echo $this->description;

For things that won’t ever change setting them in the main config file under ‘params’ allows you to access the value using.

$color = Yii::app()->params->mycolor 


define('MY_CONSTANT', 439);


I’d probably use Yii::app()->params if it is a configuration. There’s also the good old method: before the application is bootstrapped (/index.php), I include a class where I put all my global constants.

// MyGlobals.php

class MyGlobals


  const APP_NAME = 'Yii website'; // just a sample


// index.php in root

require_once('MyGlobals.php'); // <-----

$yii = '/yii/framework/yii.php';

$config = 'config.php';

Yii::createApplication('WebApplication', $config)->run();

And just call MyGlobals::APP_NAME anywhere.

I too use parameters in main/config.php

I believe, he asked WHERE to put constant definition and not HOW to define them! :)

And, yes - parameters would be the best IMHO!

1 Like

Wow, thanks all for the suggestions.

I think I will use params in config/main.php

Yet an other approach:

Create the constants in their contexts.


class Order ...


const SHIPPED...;


class Customer ...


const ACTIVE...;


I do use params for global application vars and within models or classes those related to a state of an object, that is:

params —> ‘languages’=>array(‘en’,‘es’…

within models or classes —> Language::ACTIVE_LANGUAGE… so in this scenario when I query in other models (i.e. localized i18N attributes) I know what I am asking for…