29
Introduction to Yii Framework Tuan Nguyen Web Developer at Tuoi Tre Online [email protected]

Introduction Yii Framework

Embed Size (px)

DESCRIPTION

Yii framework overview, basic components and structure

Citation preview

Page 1: Introduction Yii Framework

Introduction to Yii Framework

Tuan Nguyen Web Developer at Tuoi Tre Online [email protected]

Page 2: Introduction Yii Framework

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.

Page 3: Introduction Yii Framework

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.

Page 4: Introduction Yii Framework

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.

Page 5: Introduction Yii Framework

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/

Page 6: Introduction Yii Framework

Yii Rich Features MVC Design Pattern and Request Handle Workflow

- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc

Page 7: Introduction Yii Framework

Yii Rich Features MVC Design Pattern and Request Handle Workflow

- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc

Page 8: Introduction Yii Framework

Yii Rich Features New Application Generator - Demo

- Run php yii/framework/yiic.php webapp /location - Run and Test new application

Page 9: Introduction Yii Framework

Yii Rich Features New Application Generator - Demo

Page 10: Introduction Yii Framework

Yii Rich Features URLManager configuration

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

Page 11: Introduction Yii Framework

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

Page 12: Introduction Yii Framework

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.

Page 13: Introduction Yii Framework

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/

Page 14: Introduction Yii Framework

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

Page 15: Introduction Yii Framework

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

Page 16: Introduction Yii Framework

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

Page 17: Introduction Yii Framework

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

Page 18: Introduction Yii Framework

Flexible Object-Oriented Programming (OOP)

User Login Workflow

Yii Rich Features

Login By Form User types his username and password

CUserIdentity will check login information from user You can implement core class here

CWebUser Login by Login Form If identity is ok and no error, the user will be truly logged in to system by creating session for that user. 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. You can implement core class here

Login By Cookie User send its cookie in the request

Page 19: Introduction Yii Framework

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.

Page 20: Introduction Yii Framework

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

Page 21: Introduction Yii Framework

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/

Page 22: Introduction Yii Framework

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.

Page 23: Introduction Yii Framework

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/

Page 24: Introduction Yii Framework

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

Page 25: Introduction Yii Framework

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/ )

Page 26: Introduction Yii Framework

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

Page 27: Introduction Yii Framework

Practical Folder Structure --apps -----common -----backend -----frontend -----console --core ----yii

http://www.yiiframework.com/wiki/374/yiiboilerplate-setup-a-professional-project-structure-in-seconds/

Page 28: Introduction Yii Framework

Q&A For more information or discussion:

- Nguyễn Anh Tuấn

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

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

Page 29: Introduction Yii Framework

Thank you! For more information or discussion:

- Nguyễn Anh Tuấn

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

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