Page 1 of 1
How to create "hello world" component
#1
Posted 16 November 2010 - 01:40 PM
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.
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.
#2
Posted 16 November 2010 - 01:50 PM
in your components folder create a file
for example Hello.php
inside
then in your config/main.php add your component to preload array
for example Hello.php
inside
class Hello extends CApplicationComponent { public function init() { echo "hellow world"; } }
then in your config/main.php add your component to preload array
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Web Application',
// preloading 'log' component
'preload'=>array('log','Hello'),
......
#3
Posted 16 November 2010 - 01:52 PM
I have already figured out 
I have created a file Findlocale.php under protected/components with the following text
the in my controller I used:
YII is GREAT!
I have created a file Findlocale.php under protected/components with the following text
<?php
class Findlocale
{
public static function set($query_string)
{
echo 'hello world';
}
}the in my controller I used:
Findlocale::set('something');YII is GREAT!
#4
Posted 16 November 2010 - 02:21 PM

POPULAR
If you want a full application component you can do:
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).
EDIT:
Fixed example, thanks Muaid.
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).
EDIT:
Fixed example, thanks Muaid.
This post has been edited by Mike: 18 December 2010 - 04:17 AM
#5
Posted 18 December 2010 - 02:50 AM
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.
there is on modification in Mike's post:
Quote
Access from anywhere with:
Yii::app()->myblabla();
it should be :
Yii::app()->something->myblabla();
thanks for you all this was very helpful for me to begin.
#9
Posted 06 April 2011 - 01:45 PM
Great tutorial for a newbie like me. Thanks for the tutor.. 
Thank you all the master in here..
.
Yii is Great..
Thank you all the master in here..
Yii is Great..
Share this topic:
Page 1 of 1

Help
















