Introduction Yii Framework

7,087 views
6,802 views

Published on

Yii framework overview, basic components and structure

Introduction Yii Framework

  1. 1. Introduction to YiiFrameworkTuan NguyenWeb Developer at Tuoi Tre Onlinenganhtuan63@gmail.com
  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. 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.
  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: http://www.yiiframework.com/doc/guide/
  6. 6. Yii Rich FeaturesMVC Design Pattern and Request Handle Workflow- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
  7. 7. Yii Rich FeaturesMVC Design Pattern and Request Handle Workflow- http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
  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: www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
  14. 14. Yii Rich FeaturesModels (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
  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.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
  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;http://www.yiiframework.com/wiki/152/cookie-management-in-yiiWeb 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:http://www.yiiframework.com/doc/guide/1.1/en/topics.securityhttp://www.yiiframework.com/wiki/275/how-to-write-secure-yii-applications/
  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.http://www.yiiframework.com/extension/phpquickprofiler/
  24. 24. Yii Rich FeaturesMulti-Caching Mechanism- Support: File Cache, Database Cache, APC Cache, Memcache,...Switch between easily.http://www.yiiframework.com/doc/guide/1.1/en/caching.data
  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 - www.yiiframework.com/extension/rights/ )
  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 athttp://www.yiiframework.com/doc/guide/1.1/en/extension.overview
  27. 27. Practical Folder Structure--apps-----common-----backend-----frontend-----console--core----yiihttp://www.yiiframework.com/wiki/374/yiiboilerplate-setup-a-professional- project-structure-in-seconds/
  28. 28. Q&AFor more information or discussion:- Nguyễn Anh Tuấn- Email: nganhtuan63@gmail.com- Website: http://nganhtuan.com- Facebook: https://www.facebook.com/nganhtuan63- Check my open source Yii CMS: http://www.gxccms.com
  29. 29. Thank you!For more information or discussion:- Nguyễn Anh Tuấn- Email: nganhtuan63@gmail.com- Website: http://nganhtuan.com- Facebook: https://www.facebook.com/nganhtuan63- Check my open source Yii CMS: http://www.gxccms.com

×