Yii Framework Forum: How to create "hello world" component - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to create "hello world" component Rate Topic: -----

#1 User is offline   Baxtis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-November 10

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

#2 User is offline   rudenich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 30-April 10
  • Location:Ukraine-Kramatorsk

Posted 16 November 2010 - 01:50 PM

in your components folder create a file
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'),
......

2

#3 User is offline   Baxtis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-November 10

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

<?php
class Findlocale

{

     public static function set($query_string)

     {

           echo 'hello world';
     }

}


the in my controller I used:

Findlocale::set('something');


YII is GREAT!
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 16 November 2010 - 02:21 PM

*
POPULAR

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

EDIT:
Fixed example, thanks Muaid.

This post has been edited by Mike: 18 December 2010 - 04:17 AM

7

#5 User is offline   Muaid 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-October 10
  • Location:Saudi Arabia

Posted 18 December 2010 - 02:50 AM

thanks guys this is helpful ..

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

#6 User is offline   Igor Ivanovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 17-October 10
  • Location:Zagreb

Posted 18 December 2010 - 04:17 AM

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

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 December 2010 - 04:18 AM

Fixed, thanks!
0

#8 User is offline   rianday 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 16-December 10

Posted 25 December 2010 - 08:14 PM

wow this tutorial is very nice.. thanks for your tutorial..
0

#9 User is offline   J.J 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 01-April 11

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.. :D.

Yii is Great..
0

#10 User is offline   pligor 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 101
  • Joined: 17-January 11
  • Location:Athens, Greece

Posted 02 July 2011 - 02:18 AM

Also there is this wiki: http://www.yiiframew...ation-component
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users