How to create "hello world" component

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.

Thank you in advance.

in your components folder create a file

for example Hello.php


class Hello extends CApplicationComponent


    public function init()


        echo "hellow world";



then in your config/main.php add your component to preload array

return array(


	'name'=>'My Web Application',

	// preloading 'log' component



I have already figured out :)

I have created a file Findlocale.php under protected/components with the following text


class Findlocale


     public static function set($query_string)


           echo 'hello world';



the in my controller I used:



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:








Access from anywhere with:


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


Fixed example, thanks Muaid.

thanks guys this is helpful …

there is on modification in Mike’s post:

it should be :


thanks for you all this was very helpful for me to begin.

Welcome to Yii , i suggest you to read Definitive guide have fun :)

Fixed, thanks!

wow this tutorial is very nice… thanks for your tutorial…

Great tutorial for a newbie like me. Thanks for the tutor… :)

Thank you all the master in here… :D.

[color="#0000FF"][size="4"]Yii is Great…[/size][/color]

Also there is this wiki:

@Mike Great explanation. :)

I am an newbie for yii and want some basic tutorials please any one guide me