0% found this document useful (0 votes)
57 views

Introduction To Yii Framework: Tuan Nguyen Web Developer at Tuoi Tre Online

The document introduces the Yii framework and discusses why the author chose to use it. It provides an overview of Yii's features including its MVC design pattern, code generation tools, caching, security features and extension capabilities. Practical examples are given for common tasks like generating models and controllers. The document also recommends a folder structure for web applications built with Yii.

Uploaded by

Daniel Popusoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Introduction To Yii Framework: Tuan Nguyen Web Developer at Tuoi Tre Online

The document introduces the Yii framework and discusses why the author chose to use it. It provides an overview of Yii's features including its MVC design pattern, code generation tools, caching, security features and extension capabilities. Practical examples are given for common tasks like generating models and controllers. The document also recommends a folder structure for web applications built with Yii.

Uploaded by

Daniel Popusoi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Introduction to Yii

Framework
Tuan Nguyen
Web Developer at Tuoi Tre Online
[email protected]
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.
1. Why I choose Yii Framework?
- Demand:

To build a robust and solid CMS. (Easy for both Dev., End-Users)

Previous Frameworks I worked with:

+ Zend Framework.
+ CakePHP.
+ MVC Framework made by myself.

Previous CMS I worked with:

+ Drupal
+ Wordpress
+ Other enterprise CMS.
1. Why I choose Yii Framework?
To get started, follow these links in order:

1. http://www.yiiframework.com/tour/ - Learn basic flow of creating new app.


2. http://www.yiiframework.com/doc/blog/ - Create a simple blog with Yii.
3. http://www.yiiframework.com/doc/guide/ - Learn features & components of
Yii.

Advantages:
- Yii performance - Optimize with APC cache and Lazy Loading -
http://www.yiiframework.com/performance/
- Very good and clear documents.
- Rich of modules and extensions.

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

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

- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
Yii Rich Features
New Application Generator - Demo

- Run php yii/framework/yiic.php webapp /location


- Run and Test new application
Yii Rich Features
New Application Generator - Demo
Yii Rich Features
URLManager configuration

- Do not over-use URLManager due to downgrade of performance.


(Using .htaccess as much as possible)
Yii Rich Features
Support multi-database systems (MySQL, SQLlite,...)

- Yii database is built on top of PDO. You can switch betweeb DBMS (MySQL,
SQL,...) without the need to change application code

- Database configuration in protected/config/main.php


Yii Rich Features
Gii Generator: Model, Controller, CRUD,...Your Own generator

Demo:

- Config database connection first.


- Create new User Table in Database.
CREATE TABLE `yiisample`.`user` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`status` TINYINT( 2 ) NOT NULL DEFAULT '0'
) ENGINE = INNODB;

- Enable Gii in Applicaion


- Using Gii to generate code.
Yii Rich Features
Controllers, Filters, AccessControl and Views

Demo:

- Learn the basic workflow of controllers and views.


- Learn using filters with AccessControl.
- Learn more about the views:
www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
Yii Rich Features
Models (CActiveRecord, DAO...)

Demo CActiveRecord:

+ http://www.yiiframework.com/doc/guide/1.1/en/database.ar

- Using CActiveRecord to manage Table in Database.


- Features of CActiveRecord:

+ Rules and Validators


+ Behaviors and Events
+ Integreate data to Widgets, CActiveForm,...

Demo DAO:

+ http://www.yiiframework.com/doc/guide/1.1/en/database.dao
Yii Rich Features
Models - CActiveRecord vs DAO

CActiveRecord:
- Good for Inputting (rules, validators,...).
- Quick deploy thanks to Gii Generator.
- Easy to handle.

DAO:
- Less memory.
- Good for retrieving data.

Advice:
Caching and choose DAO to deploy application fast first. Implement by DAO
later.
http://www.yiiframework.com/forum/index.php/topic/25825-dao-vs-activerecord-
methods/
http://www.sheldmandu.com/php/php-mvc-frameworks/yii-dao-vs-active-record-
performance
Yii Rich Features
Forms - CActiveForm

- It is based on Model Concept to collect user data.


- It support rules and validators like Model.
- Demo ContactForm with rules and validators
Yii Rich Features
Flexible Object-Oriented Programming (OOP)

- In Yii, you can extend any core class you want. Keep core code clean and
logical.

- Demo with User Components.

To manage Users, Yii use 2 components/classes:

+ CWebUser : a component in application variable. It stores basic user


information of current request (username, email, login by cookie).

+ CUserIdentity : a class to help user "Log in" to system. (Allow users to login
by using File Data, Pre-Defined data or Database data,...)
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, 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.
password core class here
Login by Cookie

If the cookie user send in request


is ok (with some secure check).
The User will be logged into the
system like above.
Login By
Cookie
You can implement core class
User send its
here
cookie in the
request
Yii Rich Features
Session and Cookie Management

- Yii supports Session and Cookie wrapper class.

Session:
- You can use Session Handler by File, Database or Memcache.

'session' => array(


'class' => 'CDbHttpSession',
'connectionID' => 'db',
'autoCreateSessionTable'=>false,
'sessionTableName'=>'gxc_session',
'sessionName'=>'gxc_session_id' //Should Change for Different //Apps
),

You can implement/extend CDbHttpSession for your own use.


Yii Rich Features
Session and Cookie Management

Cookie:

- Writing to Cookie:
Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name', $value);

- Read from Cookie:


$cookie = Yii::app()->request->cookies['cookie_name']->value;

http://www.yiiframework.com/wiki/152/cookie-management-in-yii

Web User Cookie:


'user'=>array(
'class'=>'cms.modules.user.components.GxcUser',

'allowAutoLogin'=>true,
'autoRenewCookie'=>true,
'loginUrl'=>array('site/login'),
'stateKeyPrefix'=>'gxc_u_', //Should Change for Different Apps
),
Yii Rich Features
Application Security (CSRF, Cookie validation,...)

- Yii supports CSRF, Cookie Validation,...


- Learn more at:

http://www.yiiframework.com/doc/guide/1.1/en/topics.security

http://www.yiiframework.com/wiki/275/how-to-write-secure-yii-applications/
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)
- Demo with Rights module.
Yii Rich Features
Error Handler and Log Management

- Setup Error handler in config file.


- Log Management:

+ Write Log to File.


+ Display Log on Website.
+ Intergrate with PHP Quick Profiler extension.

http://www.yiiframework.com/extension/phpquickprofiler/
Yii Rich Features
Multi-Caching Mechanism

- Support: File Cache, Database Cache, APC Cache, Memcache,...Switch


between easily.

http://www.yiiframework.com/doc/guide/1.1/en/caching.data
Yii Rich Features
User Authentication with RBAC.

- Yii supports RBAC for access control.


- In RBAC, there are 3 levels: Role, Task, Operations
- Popular extension to manage RBAC (Rights extension -
www.yiiframework.com/extension/rights/ )
Yii Rich Features
Extending Yii - Extensions

Thanks to flexible Yii structure, you can easily extend Yii to:

+ Override core components.


+ Create Behaviors
+ Create Widgets
+ Create Module
+ ...More at

http://www.yiiframework.com/doc/guide/1.1/en/extension.overview
Practical Folder Structure
--apps
-----common
-----backend
-----frontend
-----console
--core
----yii

http://www.yiiframework.com/wiki/374/yiiboilerplate-setup-a-professional-
project-structure-in-seconds/
Q&A
For more information or discussion:

- Nguyn Anh Tun

- Email: [email protected]
- Website: http://nganhtuan.com
- Facebook: https://www.facebook.com/nganhtuan63

- Check my open source Yii CMS: http://www.gxccms.com


Thank you!
For more information or discussion:

- Nguyn Anh Tun

- Email: [email protected]
- Website: http://nganhtuan.com
- Facebook: https://www.facebook.com/nganhtuan63

- Check my open source Yii CMS: http://www.gxccms.com

You might also like