You are on page 1of 29

Introduction to Yii

Framework
Tuan Nguyen
Web Developer at Tuoi Tre Online
nganhtuan63@gmail.com

Content

1. Why I choose Yii Framework?

2. Yii rich features. (Stuff I like)

3. A practical folder structure for Web Application with Yii.

4. Q&A.

Demand: To build a robust and solid CMS. Previous CMS I worked with: + Drupal + Wordpress + Other enterprise CMS. Why I choose Yii Framework? . (Easy for both Dev. End-Users) Previous Frameworks I worked with: + Zend Framework. + MVC Framework made by myself. 1. . + CakePHP..

Disadvantages: . 2.com/tour/ . http://www. Why I choose Yii Framework? To get started.Yii performance .Forum discussions is not really active.yiiframework. . .Learn features & components of Yii.Very good and clear documents.Rich of modules and extensions. http://www.yiiframework.Optimize with APC cache and Lazy Loading - http://www.com/performance/ .Learn basic flow of creating new app.Create a simple blog with Yii.com/doc/blog/ .yiiframework. follow these links in order: 1. 1. Advantages: .Community is not as big as others.com/doc/guide/ . . 3.yiiframework. http://www.

your own generator.. Filters.check more at: http://www.yiiframework.Working with database (CActiveRecord. AccessControl and Views (demo) • Model .. Module.Extensions and Modules • ..Rules and Validators (demo) • Flexible OOP (Object-Oriented Programming).Rules and Validators... • Extending Yii .... (demo) • Form . SQLlite.). (demo) ---------------------------------------------------------------------------------------------------------------------- • Asset Manager • Error Handler & Log Management • Support multi-caching mechanism. (demo) • Application Security (CSRF.) .. Form. • New Application Generator. (demo) • Controllers.)..Handle user input data . (demo) • Session and Cookie Management. Cookie validation... (demo) • Support multi-database systems (MySQL.com/doc/guide/ .. (demo) • URLManager (demo) • Gii Generator: CRUD. (demo) • User Authentication with RBAC. Yii Rich Features Features I usually work with: • MVC Design Pattern and Request Handle Workflow. Model..

http://www. Yii Rich Features MVC Design Pattern and Request Handle Workflow .yiiframework.mvc .1/en/basics.com/doc/guide/1.

mvc .1/en/basics.http://www.yiiframework. Yii Rich Features MVC Design Pattern and Request Handle Workflow .com/doc/guide/1.

php webapp /location .Demo .Run php yii/framework/yiic. Yii Rich Features New Application Generator .Run and Test new application .

Yii Rich Features New Application Generator .Demo .

Yii Rich Features URLManager configuration .htaccess as much as possible) . (Using .Do not over-use URLManager due to downgrade of performance.

.Database configuration in protected/config/main..Yii database is built on top of PDO.. SQLlite.) ..php .) without the need to change application code . SQL. Yii Rich Features Support multi-database systems (MySQL... You can switch betweeb DBMS (MySQL.

.. `name` VARCHAR( 255 ) NOT NULL . .. Yii Rich Features Gii Generator: Model.Config database connection first. CRUD.Using Gii to generate code. `email` VARCHAR( 255 ) NOT NULL .`user` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY .Enable Gii in Applicaion . `status` TINYINT( 2 ) NOT NULL DEFAULT '0' ) ENGINE = INNODB. . . Controller.Create new User Table in Database. CREATE TABLE `yiisample`.Your Own generator Demo: .

Filters.com/wiki/249/understanding-the-view-rendering-flow/ .Learn more about the views: www. . .Learn using filters with AccessControl.yiiframework. AccessControl and Views Demo: . Yii Rich Features Controllers.Learn the basic workflow of controllers and views.

) Demo CActiveRecord: + http://www.. CActiveForm.1/en/database.1/en/database.yiiframework.com/doc/guide/1.ar . Yii Rich Features Models (CActiveRecord. ... DAO..yiiframework. Demo DAO: + http://www.dao .Features of CActiveRecord: + Rules and Validators + Behaviors and Events + Integreate data to Widgets.Using CActiveRecord to manage Table in Database..com/doc/guide/1.

Less memory..Quick deploy thanks to Gii Generator.yiiframework. DAO: .Good for retrieving data..Good for Inputting (rules.php/topic/25825-dao-vs-activerecord- methods/ http://www. Advice: Caching and choose DAO to deploy application fast first.). Yii Rich Features Models .sheldmandu. validators. http://www.Easy to handle. .com/forum/index.. . Implement by DAO later.CActiveRecord vs DAO CActiveRecord: . .com/php/php-mvc-frameworks/yii-dao-vs-active-record- performance .

It is based on Model Concept to collect user data.Demo ContactForm with rules and validators .It support rules and validators like Model. . Yii Rich Features Forms .CActiveForm . .

Keep core code clean and logical. you can extend any core class you want. (Allow users to login by using File Data. + CUserIdentity : a class to help user "Log in" to system. email.. . Yii Rich Features Flexible Object-Oriented Programming (OOP) . It stores basic user information of current request (username.In Yii..Demo with User Components.) . Pre-Defined data or Database data. To manage Users. login by cookie).. Yii use 2 components/classes: + CWebUser : a component in application variable.

Yii Rich Features Flexible Object-Oriented Programming (OOP) CWebUser User Login Workflow Login by Login Form CUserIdentity will Login By check login information If identity is ok and no error. password core class here Login by Cookie If the cookie user send in request is ok (with some secure check). the Form from user user will be truly logged in to User types his system by creating session for username and You can implement that user. Login By Cookie You can implement core class User send its here cookie in the request . The User will be logged into the system like above.

Database or Memcache. 'autoCreateSessionTable'=>false. . 'sessionTableName'=>'gxc_session'. 'connectionID' => 'db'. 'session' => array( 'class' => 'CDbHttpSession'. Session: . Yii Rich Features Session and Cookie Management . You can implement/extend CDbHttpSession for your own use.You can use Session Handler by File. 'sessionName'=>'gxc_session_id' //Should Change for Different //Apps ).Yii supports Session and Cookie wrapper class.

. 'loginUrl'=>array('site/login'). 'stateKeyPrefix'=>'gxc_u_'.Writing to Cookie: Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name'.yiiframework. 'allowAutoLogin'=>true.components. .GxcUser'.com/wiki/152/cookie-management-in-yii Web User Cookie: 'user'=>array( 'class'=>'cms. http://www. 'autoRenewCookie'=>true.Read from Cookie: $cookie = Yii::app()->request->cookies['cookie_name']->value. //Should Change for Different Apps ).modules. $value).user. Yii Rich Features Session and Cookie Management Cookie: .

security http://www. Cookie validation.1/en/topics.com/doc/guide/1..Yii supports CSRF.. . Cookie Validation.com/wiki/275/how-to-write-secure-yii-applications/ .Learn more at: http://www.yiiframework.) .yiiframework.. Yii Rich Features Application Security (CSRF....

Demo with Rights module. Yii Rich Features Asset Managers Demo: .Using Asset Managers in Module to publish Asset Files. . (Which we can't directly access due to .htaccess restrict) .

yiiframework. http://www. + Display Log on Website.Setup Error handler in config file. Yii Rich Features Error Handler and Log Management .com/extension/phpquickprofiler/ .Log Management: + Write Log to File. + Intergrate with PHP Quick Profiler extension. .

http://www. Database Cache.Switch between easily. Yii Rich Features Multi-Caching Mechanism .com/doc/guide/1. APC Cache.Support: File Cache.data ... Memcache.yiiframework..1/en/caching.

yiiframework.In RBAC. . Task.com/extension/rights/ ) .Popular extension to manage RBAC (Rights extension - www. Operations .Yii supports RBAC for access control. there are 3 levels: Role. Yii Rich Features User Authentication with RBAC. .

overview .. + Create Behaviors + Create Widgets + Create Module + . you can easily extend Yii to: + Override core components.com/doc/guide/1.More at http://www.Extensions Thanks to flexible Yii structure.yiiframework.. Yii Rich Features Extending Yii .1/en/extension.

com/wiki/374/yiiboilerplate-setup-a-professional- project-structure-in-seconds/ .yiiframework. Practical Folder Structure --apps -----common -----backend -----frontend -----console --core ----yii http://www.

Website: http://nganhtuan. Q&A For more information or discussion: .com .com/nganhtuan63 .Facebook: https://www.Email: nganhtuan63@gmail.com .Check my open source Yii CMS: http://www.gxccms.facebook.com .Nguyễn Anh Tuấn .

Website: http://nganhtuan.gxccms. Thank you! For more information or discussion: .com .com .Check my open source Yii CMS: http://www.Nguyễn Anh Tuấn .com/nganhtuan63 .com .Facebook: https://www.facebook.Email: nganhtuan63@gmail.