Created by Jorge Barata González
<?php
class Foo extends CComponent {
public $bar = 1;
}
$foo = Yii::createComponent('Foo');
echo $foo->bar; // 1
$foo = Yii::createComponent(array(
'class'=>'Foo',
'bar'=2
);
echo $foo->bar; // 2
CComponent
<?php
class Jedi extends CComponent {
/* read-write property */
protected $_lightsaber;
public function getLightsaber() {
return $this->_lightsaber;
}
public function setLightsaber($value) {
return $this->_lightsaber = $value;
}
/* read only property */
public function getMidichlorians() {
return 9000;
}
}
$jedi = new Jedi;
$jedi->lightsaber = 'red';
echo $jedi->lightsaber; // red
echo $jedi->midichlorians; // 9000
echo $jedi->getMidichlorians(); // 9000
CComponent
<?php
class Human extends CComponent {
}
$human = new Human;
$human->attachbehavior('jedi', new Jedi);
echo $human->midichlorians; //9000
CComponent
CController, CModel, CConsoleCommand
<?php
class Foo extends CModel {
...
public function behaviors()
{
return array(
'behaviorName'=>array(
'class'=>'path.to.BehaviorClass',
'property1'=>'value1',
'property2'=>'value2',
)
);
}
CComponent
<?php
public function onAttack($event)
{
$this->raiseEvent('onAttack',$event);
}
$human->onAttack=$callback; // or $component->onAttack->add($callback);
<?php
'handleOnAttack' // handleOnAttack() is a global function
array($object,'handleOnAttack') // using $object->handleOnAttack()
array('Jedi','handleOnAttack') // using Jedi::handleOnAttack()
Query criteria that can be combined between them
<?php
class Post extends CActiveRecord
{
...
public function scopes()
{
return array(
'published'=>array(
'condition'=>'status=1',
),
'recently'=>array(
'order'=>'create_time DESC',
'limit'=>5,
),
);
}
}
$posts = Post::model()->published()->recently()->findAll();
Configuration
<?php
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'pattern1'=>'route1',
'pattern2'=>'route2',
'pattern3'=>'route3',
),
),
),
);
)
Url Manager CUrlManager
↑
Application CApplication
↑
Controller CController
↑
View
array('//module/controller/action', 'param1'=>1, 'param2'=>2, ...)
Yii::app()->createUrl();
$this->createUrl(); // Controller or View
Configuration
<?php
array(
......
'preload'=>array('log'),
'components'=>array(
......
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'trace, info',
'categories'=>'system.*',
),
array(
'class'=>'CEmailLogRoute',
'levels'=>'error, warning',
'emails'=>'admin@example.com',
),
),
),
),
)
<?php
Yii::log('ola k ase', 'error');
protected/application.log
Logs are not processed until the application ends.
<?php
Yii::app()->end();
Yii::app()->log->processLogs();
Yii::getLogger()->flush(true);