Table of Con­tents

This book is a col­lec­tion of Yii recipes grouped in 13 chap­ters gen­er­ally in­de­pen­dent of each other.

Look­ing for the first edi­tion?

Sec­ond edi­tion

In the sec­ond edi­tion many recipes were up­dated and rewrit­ten to re­flect lat­est Yii and PHP world changes and im­prove over­all ex­pe­ri­ence. Ad­di­tion­ally all er­rata from the first edi­tion was fixed.

There are some to­tally new recipes. These are un­der­lined in table of con­tents.

Chap­ter 1, Under the Hood
Will tell you about most in­ter­est­ing Yii fea­tures hid­den under the hood: events, im­port, au­toload­ing, ex­cep­tions, com­po­nent and wid­get con­fig­u­ra­tion and more.
  • Using get­ters and set­ters
  • Using Yii events
  • Using im­port and au­toload­ing
  • Using ex­cep­tions
  • Con­fig­ur­ing com­po­nents
  • Con­fig­ur­ing wid­get de­faults
  • Using Yii core col­lec­tions
  • Work­ing with re­quests
Chap­ter 2, Router, Con­troller, and Views
This chap­ter is about handy things about Yii URL router, con­trollers, and views: URL rules, ex­ter­nal ac­tions and con­trollers, view clips, dec­o­ra­tors and more.
  • Con­fig­ur­ing URL rules
  • Gen­er­at­ing URLs by path
  • Using reg­u­lar ex­pres­sions in URL rules
  • Cre­at­ing URL rules for sta­tic pages
  • Pro­vid­ing your own URL rules at run­time
  • Using base con­troller
  • Using ex­ter­nal ac­tions
  • Dis­play­ing sta­tic pages with CViewAc­tion
  • Using flash mes­sages
  • Using con­troller con­text in a view
  • Reusing views with par­tials
  • Using clips
  • Using dec­o­ra­tors
  • Defin­ing mul­ti­ple lay­outs
  • Pag­i­nat­ing and sort­ing data
Chap­ter 3, AJAX and jQuery
Yii’s client side is built with jQuery — the widely used, pow­er­ful, and sim­ple to learn JavaScript li­brary. This chap­ter fo­cuses on Yii-spe­cific tricks rather than jQuery it­self.
  • Load­ing a block through AJAX
  • Man­ag­ing as­sets
  • In­clud­ing re­sources into the page
  • Work­ing with JSON
  • Pass­ing con­fig­u­ra­tion from PHP to JavaScript
  • Han­dling vari­able num­ber of in­puts
  • Ren­der­ing con­tent at the client side
Chap­ter 4, Work­ing with Forms
Yii makes work­ing with forms a breeze and doc­u­men­ta­tion on it is al­most com­plete. Still there are some areas that need clar­i­fi­ca­tion and ex­am­ples. Some of the top­ics cov­ered in this chap­ter are cre­at­ing own val­ida­tors and input wid­gets, up­load­ing files, using and cus­tomiz­ing CAPTCHA.
  • Writ­ing your own val­ida­tors
  • Up­load­ing files
  • Adding CAPTCHA
  • Cus­tomiz­ing CAPTCHA
  • Cre­at­ing a cus­tom input wid­get with CIn­putWid­get
Chap­ter 5, Test­ing Your Ap­pli­ca­tion
This chap­ter cov­ers both unit test­ing, func­tional test­ing and gen­er­at­ing code cov­er­age re­ports. Recipes are fol­low­ing test dri­ven de­vel­op­ment ap­proach: you'll write tests for sev­eral small ap­pli­ca­tions and then will im­ple­ment func­tion­al­ity.
  • Set­ting up the test­ing en­vi­ron­ment
  • Writ­ing and run­ning unit tests
  • Using fix­tures
  • Test­ing the ap­pli­ca­tion with func­tional tests
  • Gen­er­at­ing code cov­er­age re­ports
Chap­ter 6, Data­base, Ac­tive Record, and Model Tricks
In this chap­ter you’ll learn how to work with data­base ef­fi­ciently, when to use mod­els and when not to, how to work with mul­ti­ple data­bases, how to au­to­mat­i­cally pre-process Ac­tive Record fields and how to use pow­er­ful data­base cri­te­ria.
  • Get­ting data from a data­base
  • Defin­ing and using mul­ti­ple DB con­nec­tions
  • Using scopes to get mod­els for dif­fer­ent lan­guages
  • Pro­cess­ing model fields with AR event-like meth­ods
  • Ap­ply­ing mark­down and HTML
  • High­light­ing code with Yii
  • Au­tomat­ing time­stamps
  • Set­ting an au­thor au­to­mat­i­cally
  • Im­ple­ment­ing sin­gle table in­her­i­tance
  • Using CD­bCri­te­ria
Chap­ter 7, Using Zii Com­po­nents
This chap­ter cov­ers data providers, grids and lists: how to con­fig­ure sort­ing and search, how to use grids with mul­ti­ple re­lated mod­els, how to cre­ate your own col­umn types and more.
  • Using data providers
  • Using grids
  • Using lists
  • Cre­at­ing cus­tom grid columns
Chap­ter 8, Ex­tend­ing Yii
Shows not only how to im­ple­ment your own Yii ex­ten­sion but how to make your ex­ten­sion reusable and use­ful for com­mu­nity. Also we’ll focus on many things you should do to make your ex­ten­sion as ef­fi­cient as pos­si­ble.
  • Cre­at­ing model be­hav­iors
  • Cre­at­ing com­po­nents
  • Cre­at­ing reusable con­troller ac­tions
  • Cre­at­ing reusable con­trollers
  • Cre­at­ing a wid­get
  • Cre­at­ing CLI com­mands
  • Cre­at­ing fil­ters
  • Cre­at­ing mod­ules
  • Cre­at­ing a cus­tom view ren­derer
  • Mak­ing ex­ten­sions dis­tri­b­u­tion-ready
Chap­ter 9, Error Han­dling, De­bug­ging, and Log­ging
In this chap­ter we will re­view log­ging, analysing ex­cep­tion stack trace and will im­ple­ment our own error han­dler.
  • Using dif­fer­ent log routes
  • An­a­lyz­ing the Yii error stack trace
  • Log­ging and using the con­text in­for­ma­tion
  • Im­ple­ment­ing your own smart 404 han­dler
Chap­ter 10, Se­cu­rity
You’ll learn how to keep your ap­pli­ca­tion se­cure ac­cord­ing to gen­eral web ap­pli­ca­tion se­cu­rity prin­ci­ple “fil­ter input es­cape out­put”. We’ll cover such top­ics as cre­at­ing your own con­troller fil­ters, pre­vent­ing XSS, CSRF and SQL in­jec­tions, es­cap­ing out­put and using role based ac­cess con­trol.
  • Using con­troller fil­ters
  • Using CHtml and CHtmlPu­ri­fier to pre­vent XSS
  • Pre­vent­ing SQL in­jec­tions
  • Pre­vent­ing CSRF
  • Using RBAC
Chap­ter 11, Per­for­mance Tun­ing
In this chap­ter we’ll show how to con­fig­ure Yii to gain extra per­for­mance. Also you’ll learn some best prac­tices of de­vel­op­ing an ap­pli­ca­tion that will run smooth until you have very high loads.
  • Fol­low­ing best prac­tices
  • Speed­ing up ses­sions han­dling
  • Using cache de­pen­den­cies and chains
  • Pro­fil­ing an ap­pli­ca­tion with Yii
  • Lever­ag­ing HTTP caching
Chap­ter 12, Using Ex­ter­nal Code
Fo­cuses on using third party code with Yii. We'll use Zend Frame­work, Ko­hana and PEAR but you'll be able to use any code after learn­ing how it works.
  • Using Zend Frame­work from Yii
  • Cus­tomiz­ing the Yii au­toloader
  • Using Ko­hana in­side Yii
  • Using PEAR in­side Yii
  • Using Com­poser with Yii
Chap­ter 13, De­ploy­ment
Cov­ers var­i­ous tips that are es­pe­cially use­ful on ap­pli­ca­tion de­ploy­ment, when de­vel­op­ing ap­pli­ca­tion in team or when you just want to make your de­vel­op­ment en­vi­ron­ment more com­fort­able.
  • Chang­ing the Yii di­rec­to­ries lay­out
  • Mov­ing an ap­pli­ca­tion out of we­b­root
  • Shar­ing the frame­work di­rec­tory
  • Mov­ing con­fig­u­ra­tion parts into sep­a­rate files
  • Using mul­ti­ple con­fig­u­ra­tions to sim­plify the de­ploy­ment
  • Im­ple­ment­ing and ex­e­cut­ing cron jobs
  • Main­te­nance mode

First edi­tion

Chap­ter 1, Under the Hood
Will tell you about most in­ter­est­ing Yii fea­tures hid­den under the hood: events, im­port, au­toload­ing, ex­cep­tions, com­po­nent and wid­get con­fig­u­ra­tion and more.
  • Using get­ters and set­ters
  • Using Yii events
  • Using im­port and au­toload­ing
  • Using ex­cep­tions
  • Con­fig­ur­ing com­po­nents
  • Con­fig­ur­ing wid­get de­faults
  • Using Yii core col­lec­tions
  • Work­ing with re­quest
Chap­ter 2, Router, Con­troller, and Views
This chap­ter is about handy things about Yii URL router, con­trollers, and views: URL rules, ex­ter­nal ac­tions and con­trollers, view clips, dec­o­ra­tors and more.
  • Con­fig­ur­ing URL rules
  • Gen­er­at­ing URLs by path
  • Using reg­u­lar ex­pres­sions in URL rules
  • Cre­at­ing URL rules for sta­tic pages
  • Pro­vid­ing your own URL rules at run­time
  • Using base con­troller
  • Using ex­ter­nal ac­tions
  • Dis­play­ing sta­tic pages with CViewAc­tion
  • Using flash mes­sages
  • Using con­troller con­text in a view
  • Reusing views with par­tials
  • Using clips
  • Using dec­o­ra­tors
  • Defin­ing mul­ti­ple lay­outs
  • Pag­i­nat­ing and sort­ing data
Chap­ter 3, AJAX and jQuery
Yii’s client side is built with jQuery — the widely used, pow­er­ful, and sim­ple to learn JavaScript li­brary. This chap­ter fo­cuses on Yii-spe­cific tricks rather than jQuery it­self.
  • Load­ing a block through AJAX
  • Man­ag­ing as­sets
  • In­clud­ing re­sources into the page
  • Work­ing with JSON
  • Pass­ing con­fig­u­ra­tion from PHP to JavaScript
  • Han­dling vari­able num­ber of in­puts
Chap­ter 4, Work­ing with Forms
Yii makes work­ing with forms a breeze and doc­u­men­ta­tion on it is al­most com­plete. Still there are some areas that need clar­i­fi­ca­tion and ex­am­ples. Some of the top­ics cov­ered in this chap­ter are cre­at­ing own val­ida­tors and input wid­gets, up­load­ing files, using and cus­tomiz­ing CAPTCHA.
  • Writ­ing your own val­ida­tors
  • Up­load­ing files
  • Adding CAPTCHA
  • Cus­tomiz­ing CAPTCHA
  • Cre­at­ing a cus­tom input wid­get with CIn­putWid­get
Chap­ter 5, Test­ing Your Ap­pli­ca­tion
This chap­ter cov­ers both unit test­ing, func­tional test­ing and gen­er­at­ing code cov­er­age re­ports. Recipes are fol­low­ing test dri­ven de­vel­op­ment ap­proach: you'll write tests for sev­eral small ap­pli­ca­tions and then will im­ple­ment func­tion­al­ity.
  • Set­ting up the test­ing en­vi­ron­ment
  • Writ­ing and run­ning unit tests
  • Using fix­tures
  • Test­ing the ap­pli­ca­tion with func­tional tests
  • Gen­er­at­ing code cov­er­age re­ports
Chap­ter 6, Data­base, Ac­tive Record, and Model Tricks
In this chap­ter you’ll learn how to work with data­base ef­fi­ciently, when to use mod­els and when not to, how to work with mul­ti­ple data­bases, how to au­to­mat­i­cally pre-process Ac­tive Record fields and how to use pow­er­ful data­base cri­te­ria.
  • Get­ting data from a data­base
  • Defin­ing and using mul­ti­ple DB con­nec­tions
  • Using scopes to get mod­els for dif­fer­ent lan­guages
  • Pro­cess­ing model fields with AR event-like meth­ods
  • Ap­ply­ing mark­down and HTML
  • High­light­ing code with Yii
  • Au­tomat­ing time­stamps
  • Set­ting an au­thor au­to­mat­i­cally
  • Im­ple­ment­ing sin­gle table in­her­i­tance
  • Using CD­bCri­te­ria
Chap­ter 7, Using Zii Com­po­nents
This chap­ter cov­ers data providers, grids and lists: how to con­fig­ure sort­ing and search, how to use grids with mul­ti­ple re­lated mod­els, how to cre­ate your own col­umn types and more.
  • Using data providers
  • Using grids
  • Using lists
  • Cre­at­ing cus­tom grid columns
Chap­ter 8, Ex­tend­ing Yii
Shows not only how to im­ple­ment your own Yii ex­ten­sion but how to make your ex­ten­sion reusable and use­ful for com­mu­nity. Also we’ll focus on many things you should do to make your ex­ten­sion as ef­fi­cient as pos­si­ble. This chap­ter is avail­able for free.
  • Cre­at­ing model be­hav­iors
  • Cre­at­ing com­po­nents
  • Cre­at­ing reusable con­troller ac­tions
  • Cre­at­ing reusable con­trollers
  • Cre­at­ing a wid­get
  • Cre­at­ing CLI com­mands
  • Cre­at­ing fil­ters
  • Cre­at­ing mod­ules
  • Cre­at­ing a cus­tom view ren­derer
  • Mak­ing ex­ten­sions dis­tri­b­u­tion-ready
Chap­ter 9, Error Han­dling, De­bug­ging, and Log­ging
In this chap­ter we will re­view log­ging, analysing ex­cep­tion stack trace and will im­ple­ment our own error han­dler.
  • Using dif­fer­ent log routes
  • An­a­lyz­ing the Yii error stack trace
  • Log­ging and using the con­text in­for­ma­tion
  • Im­ple­ment­ing your own smart 404 han­dler
Chap­ter 10, Se­cu­rity
You’ll learn how to keep your ap­pli­ca­tion se­cure ac­cord­ing to gen­eral web ap­pli­ca­tion se­cu­rity prin­ci­ple “fil­ter input es­cape out­put”. We’ll cover such top­ics as cre­at­ing your own con­troller fil­ters, pre­vent­ing XSS, CSRF and SQL in­jec­tions, es­cap­ing out­put and using role based ac­cess con­trol.
  • Using con­troller fil­ters
  • Using CHtml and CHtmlPu­ri­fier to pre­vent XSS
  • Pre­vent­ing SQL in­jec­tions
  • Pre­vent­ing CSRF
  • Using RBAC
Chap­ter 11, Per­for­mance Tun­ing
In this chap­ter we’ll show how to con­fig­ure Yii to gain extra per­for­mance. Also you’ll learn some best prac­tices of de­vel­op­ing an ap­pli­ca­tion that will run smooth until you have very high loads.
  • Fol­low­ing best prac­tices
  • Speed­ing up ses­sions han­dling
  • Using cache de­pen­den­cies and chains
  • Pro­fil­ing an ap­pli­ca­tion with Yii
Chap­ter 12, Using Ex­ter­nal Code
Fo­cuses on using third party code with Yii. We'll use Zend Frame­work, Ko­hana and PEAR but you'll be able to use any code after learn­ing how it works.
  • Using Zend Frame­work from Yii
  • Cus­tomiz­ing the Yii au­toloader
  • Using Ko­hana in­side Yii
  • Using PEAR in­side Yii
Chap­ter 13, De­ploy­ment
Cov­ers var­i­ous tips that are es­pe­cially use­ful on ap­pli­ca­tion de­ploy­ment, when de­vel­op­ing ap­pli­ca­tion in team or when you just want to make your de­vel­op­ment en­vi­ron­ment more com­fort­able.
  • Chang­ing the Yii di­rec­to­ries lay­out
  • Mov­ing an ap­pli­ca­tion out of we­b­root
  • Shar­ing the frame­work di­rec­tory
  • Mov­ing con­fig­u­ra­tion parts into sep­a­rate files
  • Using mul­ti­ple con­fig­u­ra­tions to sim­plify the de­ploy­ment
  • Im­ple­ment­ing and ex­e­cut­ing cron jobs
  • Main­te­nance mode