Hello, I am a former CI user and while I feel that YII is much more powerful I still kinda lost about some of its features. Could you, please, help me with this issue:
I have a function that determines user locale and then do some specific stuff depending on locale. it has about 10 lines of code and sets a cookie. Currently I run it in almost every controller using public function init(). I would like to create a model or I think it is a better idea to create a component. Could you please give me an example on how to create a component that outputs "hello world" and then call it from any controller? I hope I will figure the rest by myself. I have read wiki and yii tutorial but still with no luck.
If you want a full application component you can do:
class MyComponent extends CApplicationComponent
{
public $someconfig='somedefault';
public function init() {
// Init this component
// $this->someconfig is already available
}
public function myblabla() {...}
}
// main configuration:
...
'components'=>array(
'something'=>array(
'class'=>'MyComponent',
'someconfig'=>'someothervalue',
),
...
Access from anywhere with:
Yii::app()->something->myblabla();
You can also add ‘something’ to preload like rudenich suggested (but i’d not use the classname here, it should be the id of a component), to always preload this component. Otherwise it’s lazy loaded (on first access).