= Yii =
Yii is a high-performance PHP framework best for developing Web 2.0 applications.
http://www.yiiframework.com/
== Slackware installation ==
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
* cd ~/Downloads
* wget https://github.com/yiisoft/yii/releases/download/1.1.15/yii-1.1.15.022a51.tar.gz
* cp yii-1.1.15.022a51.tar.gz /vars/www/htdocs
* cd /vars/www/htdocs
* tar xvzf yii-1.1.15.022a51.tar.gz
* cd yii-1.1.15.022a51
* chown apache * -R
* chmod 755 /etc/rc.d/rc.httpd
* vim /etc/httpd/httpd.conf
{{{
DirectoryIndex index.html index.php
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
Include /etc/httpd/mod_php.conf # enable the php module
}}}
* vim /etc/httpd/vhosts.conf
{{{
ServerName localhostyii
DocumentRoot "/var/www/htdocs/yii-1.1.15.022a51"
Require all granted
AllowOverride all
}}}
* vim /etc/hosts
{{{
127.0.0.1 localhostyii
}}}
* vim /etc/httpd/php.ini
{{{
date.timezone="Europe/Lisbon"
}}}
* vim /var/www/htdocs/yii-1.1.15.022a51/demos/blog/.htaccess
{{{
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /demos/blog
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
}}}
* vim blog/protected/config/main.php
{{{
'urlManager'=>array(
'showScriptName'=>false,
}}}
* /etc/rc.d/rc.httpd start
* Open URL http://localhostyii/demos/blog/index.php
* http://localhostyii/demos/blog/post/index
== Validation rules ActiveRecord CModel ==
http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules
We specify the validation rules in the rules() method which should return an array of rule configurations.
|| '''Alias''' || '''Class''' ||
||boolean || CBooleanValidator ||
||captcha || CCaptchaValidator ||
||compare || CCompareValidator ||
||email || CEmailValidator ||
||date || CDateValidator ||
||default || CDefaultValueValidator ||
||exist || CExistValidator ||
||file || CFileValidator ||
||filter || CFilterValidator ||
||in || CRangeValidator ||
||length || CStringValidator ||
||match || CRegularExpressionValidator ||
||numerical || CNumberValidator ||
||required || CRequiredValidator ||
||type || CTypeValidator ||
||unique || CUniqueValidator ||
||url || CUrlValidator ||
{{{#!highlight php
public function rules()
{
return array(
array('username, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
);
}
}}}
== Relations ActiveRecord ==
http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship
Override the relations() method of CActiveRecord.
Types relationship:
* self::BELONGS_TO
* self::HAS_MANY
* self::HAS_ONE
* self::MANY_MANY
{{{#!highlight php
public function relations()
{
return array(
'posts'=>array(self::HAS_MANY, 'Post', 'author_id'),
'profile'=>array(self::HAS_ONE, 'Profile', 'owner_id'),
);
}
}}}
== ActiveResource extension ==
Adapted from http://www.yiiframework.com/extension/activeresource
Steps:
* Get source code from https://github.com/Haensel/ActiveResource/
* Add the extension to Yii by placing it in your application's extension folder (for example '/protected/extensions')
* Edit your applications main.php config file and add 'application.extensions.EActiveResource.*' to your import definitions
* Add the configuration for your resources to the main config
{{{
'activeresource'=>array(
'class'=>'EActiveResourceConnection',
'site'=>'http://api.aRESTservice.com',
'contentType'=>'application/json',
'acceptType'=>'application/json',
'queryCacheId'=>'SomeCacheComponent'
)
}}}
* Create a class extending EActiveResource like this (don't forget the model() method!):
ActiveResource implementation:
{{{#!highlight php
class Person extends EActiveResource
{
/* The id that uniquely identifies a person. This attribute is not defined
* as a property
* because we don't want to send it back to the service like a name, surname or
* gender etc.
*/
public $id;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/* Define the resource property of this class */
public function rest()
{
return CMap::mergeArray(
parent::rest(),
array(
'resource'=>'people',
)
);
}
/* Let's define some properties and their datatypes*/
public function properties()
{
return array(
'name'=>array('type'=>'string'),
'surname'=>array('type'=>'string'),
'gender'=>array('type'=>'string'),
'age'=>array('type'=>'integer'),
'married'=>array('type'=>'boolean'),
'salary'=>array('type'=>'double'),
);
}
/* Define rules as usual */
public function rules()
{
return array(
array('name,surname,gender,age,married,salary','safe'),
array('age','numerical','integerOnly'=>true),
array('married','boolean'),
array('salary','numerical')
);
}
/* Add some custom labels for forms etc. */
public function attributeLabels()
{
return array(
'name'=>'First name',
'surname'=>'Last name',
'salary'=>'Your monthly salary',
);
}
}
}}}
Sample usage:
{{{
/* GET to http://api.example.com/person/1 and populates a single Person model*/
$person=Person::model()->findById(1);
/* GET to http://api.example.com/person and populating Person models */
$persons=Person::model()->findAll();
/* create a resource*/
$person=new Person;
$person->name='A name';
$person->age=21;
$person->save(); POST request. Returns false if the model doesn't validate
/* Updating a resource (sending a PUT request)
$person=Person::model()->findById(1);
$person->name='Another name';
$person->save(); //PUT request. Returns false if the model doesn't validate
//or short version
Person::model()->updateById(1,array('name'=>'Another name'));
/* DELETE a resource */
$person=Person::model()->findById(1);
$person->destroy(); //DELETE to http://api.example.com/person/1
//or short version
Person::model()->deleteById(1);
//setting attributes
$person->attributes=$_POST['Person'];
if($person->save())
echo 'yipiie'; //model validated and was saved/updated
}}}
Other methods for EActiveResource:
* public function init()
* public function setAttribute($name,$value)
* public function getAttributesToSend($attributes=null)
* protected function beforeSave()
* public function sendRequest($uri,$method,$params,$data)