I do not understand the Component piece used in the Yii FW.
Is there a concrete (real life) example why I should use that?

- 52,579
- 61
- 190
- 278
-
did you have a look at [basics.component](http://www.yiiframework.com/doc/guide/1.1/en/basics.component) in the Yii docs? You can also look at the implementation of the "native" components, like urlManager, db, user, etc. – ldg Sep 14 '11 at 02:21
-
@ldg Yes I have, Didn't get a good grasp of the idea from that. I am looking for a real life use case (a code example) which will help me fully understand the use of that. Will check the urlManager, as it seems to be just that. – Itay Moav -Malimovka Sep 14 '11 at 13:37
1 Answers
Framework consists of components. The base class for Yii components is CComponent which is basically the base class of everything in Yii. Components can be loaded "on-the-fly" in code or on initation in config. You can read more about it at Yii Guide
The real life example. If you want to build a house, you need some type of material for it, so those bricks or logs will be your Components. You can make different types of them, but basically they will sustain your house and give it needed features.
Here you have an example of Yii Component:
class Y extends CComponent
{
/**
* Returns the images path on webserver
* @return string
*/
public static function getImagesPath()
{
return Yii::app()->getBasePath().DIRECTORY_SEPARATOR.'images';
}
}
Now i can use this class to check resources used by my application: $y = new Y; $y->stats();
Also, if i create a special CBehavior subclass:
class YBehavior extends CBehavior {
/**
* Shows the statistics of resources used by application
* @param boolean $return defines if the result should be returned or send to output
* @return string
*/
public function stats($return = false)
{
$stats = '';
$db_stats = Yii::app()->db->getStats();
if (is_array($db_stats)) {
$stats = 'Requests completed: '.$db_stats[0].' (in '.round($db_stats[1], 5).' sec.)<br />';
}
$memory = round(Yii::getLogger()->memoryUsage/1024/1024, 3);
$time = round(Yii::getLogger()->executionTime, 3);
$stats .= 'Memory used: '.$memory.' Mb<br />';
$stats .= 'Time elapsed: '.$time.' сек.';
if ($return) {
return $stats;
}
echo $stats;
}
}
And then apply this behavior to my Component: $y->attachBehavior('ybehavior', new YBehavior);
Now i can use the method stats with my Y class:
$y->stats()
This is possible because every subclass of CComponent in Yii gives you a possibility to use behaviors, events, getters and setters and much more.
-
The way my brain works, is I need to see a real use case. As the word Component means nothing to me. – Itay Moav -Malimovka Sep 14 '11 at 13:36
-
@Itay Oh, it's pretty easy. As almost any class of the framework is a subclass of CComponent, so it is a Component of the framework. You can open almost any php file in framework folder and if this file contains a class declaration, it will be a Component. For example, if you want to create your own special teleport controller API extension to work with your web application based on Yii framework, the first thing you would do is create TeleportAPI base class extending CComponent and applying needed code. Here you will have your own Yii Component that can you full potential of the framework – Johnatan Sep 14 '11 at 14:22
-
@Itay I've edited my post above to show you some benefits that Yii Component gives you. – Johnatan Sep 14 '11 at 15:05
-
1I think I got it, CComponent is "simply" the basic structure of a class in Yii, which gives a basic set of general abilities. Much like stdClass in PHP relates to all other PHP classes. Thanks. – Itay Moav -Malimovka Sep 14 '11 at 15:10