Introduction Yii Framework


Published on

Yii framework overview, basic components and structure

Introduction Yii Framework

  1. 1. Introduction to YiiFrameworkTuan NguyenWeb Developer at Tuoi Tre
  2. 2. Content1.  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.
  3. 3. 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.
  4. 4. 1. Why I choose Yii Framework?To get started, follow these links in order:1. - Learn basic flow of creating new app.2. - Create a simple blog with Yii.3. - Learn features & components of Yii.Advantages:- Yii performance - Optimize with APC cache and Lazy Loading - Very good and clear documents.- Rich of modules and extensions.Disadvantages:- Forum discussions is not really active.- Community is not as big as others.
  5. 5. Yii Rich FeaturesFeatures 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:
  6. 6. Yii Rich FeaturesMVC Design Pattern and Request Handle Workflow-
  7. 7. Yii Rich FeaturesMVC Design Pattern and Request Handle Workflow-
  8. 8. Yii Rich FeaturesNew Application Generator - Demo- Run php yii/framework/yiic.php webapp /location- Run and Test new application
  9. 9. Yii Rich FeaturesNew Application Generator - Demo
  10. 10. Yii Rich FeaturesURLManager configuration- Do not over-use URLManager due to downgrade of performance. (Using .htaccess as much as possible)
  11. 11. Yii Rich FeaturesSupport 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
  12. 12. Yii Rich FeaturesGii Generator: Model, Controller, CRUD,...Your Own generatorDemo:- 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.
  13. 13. Yii Rich FeaturesControllers, Filters, AccessControl and ViewsDemo:- Learn the basic workflow of controllers and views.- Learn using filters with AccessControl.- Learn more about the views:
  14. 14. Yii Rich FeaturesModels (CActiveRecord, DAO...)Demo CActiveRecord:+ Using CActiveRecord to manage Table in Database.- Features of CActiveRecord:+ Rules and Validators+ Behaviors and Events+ Integreate data to Widgets, CActiveForm,...Demo DAO:+
  15. 15. Yii Rich FeaturesModels - CActiveRecord vs DAOCActiveRecord:- 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. methods/ performance
  16. 16. Yii Rich FeaturesForms - CActiveForm- It is based on Model Concept to collect user data.- It support rules and validators like Model.- Demo ContactForm with rules and validators
  17. 17. Yii Rich FeaturesFlexible 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,...)
  18. 18. Yii Rich FeaturesFlexible Object-Oriented Programming (OOP) CWebUserUser 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
  19. 19. Yii Rich FeaturesSession 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.
  20. 20. Yii Rich FeaturesSession and Cookie ManagementCookie:- 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; 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 ),
  21. 21. Yii Rich FeaturesApplication Security (CSRF, Cookie validation,...)- Yii supports CSRF, Cookie Validation,...- Learn more at:
  22. 22. Yii Rich FeaturesAsset ManagersDemo:- Using Asset Managers in Module to publish Asset Files. (Which we cant directly access due to .htaccess restrict)- Demo with Rights module.
  23. 23. Yii Rich FeaturesError 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.
  24. 24. Yii Rich FeaturesMulti-Caching Mechanism- Support: File Cache, Database Cache, APC Cache, Memcache,...Switch between easily.
  25. 25. Yii Rich FeaturesUser 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 - )
  26. 26. Yii Rich FeaturesExtending Yii - ExtensionsThanks to flexible Yii structure, you can easily extend Yii to:+ Override core components.+ Create Behaviors+ Create Widgets+ Create Module+ ...More at
  27. 27. Practical Folder Structure--apps-----common-----backend-----frontend-----console--core----yii project-structure-in-seconds/
  28. 28. Q&AFor more information or discussion:- Nguyễn Anh Tuấn- Email: Website: Facebook: Check my open source Yii CMS:
  29. 29. Thank you!For more information or discussion:- Nguyễn Anh Tuấn- Email: Website: Facebook: Check my open source Yii CMS: