Yii Framework

Jedi Guide

Created by Jorge Barata González

Overview

  • CComponent
  • Active Record Scopes
  • Url Manager
  • Logging

CComponent

CComponent

  • Getters & Setters
  • Behaviors
  • Events

<?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

Getters & Setters


<?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

Behaviors


<?php
class Human extends CComponent {
}
 
$human = new Human;
$human->attachbehavior('jedi', new Jedi);

echo $human->midichlorians; //9000
					

CComponent

Behaviors

CController, CModel, CConsoleCommand


<?php
class Foo extends CModel {
	...
	public function behaviors()
	{
	    return array(
		    'behaviorName'=>array(
			    'class'=>'path.to.BehaviorClass',
			    'property1'=>'value1',
			    'property2'=>'value2',
			)
	    );
	}
				

CComponent

Events


<?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()
					

Active Record Scopes

Active Record Scopes

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();
					

Url Manager

Url Manager

Configuration


<?php
array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                'pattern1'=>'route1',
                'pattern2'=>'route2',
                'pattern3'=>'route3',
            ),
        ),
    ),
);
)
					

Url Manager

Url Manager

CUrlManager

↑

Application

CApplication

↑

Controller

CController

↑

View

Url Manager

array('//module/controller/action', 'param1'=>1, 'param2'=>2, ...)

Yii::app()->createUrl();
$this->createUrl(); // Controller or View
					

Logging

Logging

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',
                ),
            ),
        ),
    ),
)
					

Logging


<?php
Yii::log('ola k ase', 'error');
					

protected/application.log

Logging

Logs are not processed until the application ends.


<?php
Yii::app()->end();
Yii::app()->log->processLogs();
Yii::getLogger()->flush(true);
					

THE END