You are on page 1of 206

January 1, 2013

[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Web.Application.Development.with.Yii.and.PHP
2
ND
Edition
Public: Packpub
Year: 20!
"ran#late: $on%t&'
(upport: )u*nhanh+lon%,*ahoo.com
Email: phamducbactk0,%mail.com
$-i n.i /0u :
10u ti2n 'in ch3c c4c b5n m6t n7m m8i n%9p tr:n ni;m vui< cu6c #=n% h5nh
ph3c b2n %ia />nh v: n%?-i th@n.
1=i v8i nhAn% n%?-i m8i bBt /0u tCi /; n%hD c4c
b5n hEc )u*Fn Yii cookbook development /F 'em
rG hHn cIu tr3c hJ th=n% cKa Yii v: kh4m ph4
ph0n khun% '?Hn% cKa n. tr?8c khi thLc hiJn hEc
ebook n:*.
M:i nNt v; Yii OrameworkP
Yii Oramework do m6t t2n t:u khLa '@* dLn% l2n dLa v:o viJc lI* nhAn% c4i
ha* cKa rIt nhi;u Oramework tQn% hRp l5i th:nh m6t Oramework ri2n%< n. chK
*Su dLa tr2n lT thu*St A%ile v: Prado< Uend Oramework v: m6t #= th? viJn
hAu Vch m: th:nh< t2n cKa anh ta l: Wuian% Xue.
YZc /6 : N. rIt thVch hRp cho c4c dL 4n t0m trun% v: c4c dL 4n nh[ %i3p
'@* dLn% nhanh )u\n trD v: thao t4c v8i n%?-i d]n%< c. thF '@* dLn% /F l:m
viJc theo nh.m < tu* nhi2n nSu b5n '@* dLn% dL 4n l8n th> /^n% l2n #_ d`n%
Oramework n:* v> n. khCn% thVch hRp cho lBm.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Ph0n : a:i /bt cIu h>nh *ii Oramework
c5n v:o tran% chK : http:ddwww.*iiOramework.comddownloadd
Download phi2n b\n m8i nhIt v; #au /. %i\i nNn ee'tract heref< #au /. rename l5i l:
cookbook< cop* Oolder cookbook m8i %i\i nNn v:o a:g'amppghtdoc#g< b5n ph\i down
'ampp v; rhi c:i /bt #etup th:nh cCn% th> ch5* 'ampp center thiSt l9p apache v:
m*#)l runnin%.
http://localhost/appyii/requirements/index.php
Ph0n m;m ch5y local: Xampp, Wamp, ko nn dng appserv v bD lii( nguyn nhn mnh ko
biSt, t=t nhIt nn trnh).
Hobc xi lun host th9t nSu nh c di;u kiJn.
1. Chun b
Tr?8c tin b5n ph\i cIu hnh windows dF s_ d`ng d?Rc php v8i cmd
-Gi\ s_ b5n ci apache mbc dDnh trn WinXP (C:\xampp\htdocs).
-B5n thiSt l9p l5i biSn mi tr?-ng (Environment Variables) bjng cch vo:
Start -> My Computer (right click!) -> Advanced Tab -> Environment Variables ->
Click Path in System variables (windows 7 l Path) -> Edit.
-Click vo biSn PATH v chEn Edit. L?u l d^ng c xo b[ cc d?-ng dkn d thn
t5i trong textbox m ngn cch chng v8i nhau bjng dIu ";".
-TiSp d b5n thm vo nhAng d?-ng dkn sau: "C:\xampp\php" v "
C:\xampp\htdocs\appyii\framework". L?u s_a d?-ng dkn cho ph hRp v8i my b5n
nha .
-Khli d6ng my tnh l5i.
2.To ng dng Yii mi
-YiiRoot l th? m`c nHi b5n d ci dbt Yii
-Webroot l th? m`c gc chZa web
-T^ dng lJnh, dSn webroot cKa b5n v thLc hiJn: Vo start -> run->cmd
C:\
Cd\
Cd C:\xampp\htdocs\
C:\xampp\htdocs\>appyii!ram"#or$yiic #"bapp d"%yii
&u khng c th g tip cd appyii
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
'( tip cd framework -> g yiic webapp devyii.
)d"%yii*+> T,n ng dng
Cr"at" a -"b app.ication und"r /-"brootd"%yii01 )Y"s2&o*
Y"s)y*
V9y l khung x?Hng cKa Yii d d?Rc t5o ra thnh cng
Trch dkn:devyii/
index.php file Zng d`ng
index-test.php file kiFm tra chZc nng
assets/ chZa ti nguyn Zng d`ng
css/ chZa CSS
images/ chZa hnh \nh
themes/ chZa giao diJn
protected/ chZa cc file d?Rc b\o vJ
M:o app*iidOrameworkd chu*Fn Oolder m8i t5o l: dev*ii ra n%o:i Oolder web#ite
a:d'amppdhtdoc#. 1?-n% dkn m8i cKa app l: a:d'amppdhtdoc#ddev*ii.
copy folder framework cKa app*ii #an% dev*ii.
Vo file index.php thay dQi d?-ng dkn thnh :
$yii=dirname(__FILE__).'/framework/yii.php';
B5n c thF truy c9p vo Zng d`ng t^ trnh duyJt
http://localhost/devyii/index.php
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
mk< ho:n tIt cIu h>nh /F tiSn h:nh coder.
333. T o contro.."r
-Vo th m c ng d ng
M:
%cd /Weroo!/de"yii
C:/
Cd C:/xampp/htdocs/devyii
-Kh i d ng Yii Shell
M:
%#ii$oo!/framework/yiic %he&&
Cd C:/xampp/htdocs/devyii
C:/xampp/htdocs/devyii> C:/xampp/htdocs/devyii/framework/yiic shell
T o th controller tn message c hnh d ng l helloWorld
C php: controller <controller-lD> [action-lD]
VD:
M:
con!ro&&er me%%a'e he&&oWor&d
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
-MessageController d d c t o thnh cng th m c: protected/controllers/
-Bn c nh d n cng t o ra view t i th m c: protected\views\message
-Chng ta cng c th truy c p vo hnh d ng helloWorld t trnh duy t
Default: http://localhost/devyii/index.php?r=message -> ch y vo index
http://localhost/devyii/index.php?r=message/helloWorld
4333. T o .i,n $ t
-Yii c h tr s n hm t o lin k t trong class CHtml
-VD:
T o lin k t t i View goodbye
M:
(p)(*php echo +,!m&--&ink(./oodye.0array('me%%a'e/'oodye')); *)(/p)
T o lin k t t i View Hello
M:
(p)(*php echo +,!m&--&ink(.,e&&o.0array('me%%a'e/he&&oWor&d')); *)(/p)
35.C6i 7 t databas"
1.Yii h tr c8c da.abas" sau:
-MySQL 4.l or later
-PostgresSQL 7.3 or later
-SQLite 2 and 3
-Microsoft SQL Server 2000 or later
-Oracle
2. 9 t n i % i databas"
-Th c hi n file /protected/config/main.php
-M c d nh n dang kch ho t dng ny l database c a khung x ng Yii
M:
'd'=)array(
'connec!ion1!rin'' =)
'%2&i!e-'.dirname(__FILE__).'/../da!a/!e%!dri"e.d'0
)0
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
- Gi s ta c l b ng trong Mysql nh sau:
M:
+$E34E 435LE IF 674 E8I141 9!&_:%er9 (
9id9 in!(;;) 674 6<LL 3<47_I6+$E=E640
9:%ername9 "archar(;>?) 674 6<LL0
9pa%%word9 "archar(;>?) 674 6<LL0
9emai&9 "archar(;>?) 674 6<LL0
@$I=3$# AE# (9id9)
);
- Chng ta ph i kch ho t dng sau trong /protected/config/main.php d k t n i v i Mysql
(b / / v s a l i c u hnh)
M:
// :ncommen! !he fo&&owin' !o :%e a =y1BL da!aa%e

'd'=)array(
'connec!ion1!rin'' =) 'my%2&-ho%!=&oca&ho%!;dname=de"yii'0
'em:&a!e@repare' =) !r:e0
':%ername' =) 'roo!'0
'pa%%word' =) ''0
'char%e!' =) ':!f?'0
)0
-Ngoi ra ta c th k t n i v i ci database khc (thay d i dng 'connectionString')
SQLite: sqlite:/path/to/dbfile
MySQL: mysql:host=localhost;dbname=testdb
PostgreSQL: pgsql:host=localhost;port=5432;dbname=testdb
SQL Server: mssql:host=localhost;dbname=testdb
Oracle: oci:dbname=//localhost:l52l/testdb
53. T o C:;<
- CRUD l cc ch c nng create, read, update v delete l b ng trong database
1. 9=ch ho t c>ng c 'ii
- Gii h tr sinh m t d ng cc ch c nng nh Controller, Crud, Form, Model, Module.
- kch ho t Gii ta vo file config /protected/config/main.php
- Kch ho t do n code sau
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
M:
// :ncommen! !he fo&&owin' !o ena&e !he /ii !oo&
''ii'=)array(
'c&a%%'=)'%y%!em.'ii./ii=od:&e'0
'pa%%word'=)';;;;'0
// If remo"ed0 /ii defa:&!% !o &oca&ho%! on&y. Edi! caref:&&y
!o !a%!e.
'ipFi&!er%'=)array(';>C.D.D.;'0'--;')0
)0
- i password m i d dng nh p Gii
- Vo Gii thng qua url:
http:// localhost/devyii/index.php?r=gii
2. T o us"r ?od".
-Vo m c Model Generator d t o Model cho b ng user
- Table Prefix: vi t ph n ti n t c a b ng (n u c) vo dy, dy b ng c a ta tn l
tbl_user nn ti n t s l tbl_
- Table Name: ph n cn l i c a tn b ng l user (ta c th t o t d ng t t c b ng b ng
cch nh p d u vo dy)
- Model Class: tn l p c a model l User (t sinh ra)
- Cn l i d m c d nh v click Preview
- Sau d click Generate d t o file Model c a User t i th m c /protected/models/User.php
3. T o C:;<:
- Vo m c Crud Generator
- Model Class: di n chnh xc tn l p Model v a t o l User
- Controller lD: di n user (khng vi t hoa)
- Cn l i d m c d nh v click Preview
- Sau d click Generate d t o Crud
@. Truy c p C:;<
- Vo url: http://localhost/devyii/index.php?r=user
- Click vo cc ch c nng nh Create User, Manage User, n s b t ta dng nh p, ta c
th s d ng user v pass sau: demo/demo (ti kho n bnh th ng) hay admin/admin (ti
kho n admin)
- V i ti kho n admin ta c th truy c p vo trang qu n l sau
http://localhost/devyii/index.php?r=user/admin
5. URL Thn thi n
Vo protected/config/main.php b comment urlManager...
$yii=dirname(__FILE__).'/framework/yii.php';
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
XII CC TRNG D LIU TRONG FORM
$this->pageTitle="Thay d i tiu d trang t i dy";
C8ch 1: <Ang !orm b ng c8ch dAng #idg"t.
//AhEi !Fo wid'e! %G dHn' 3IaJ cho Form 3c!i"e
$form=$!hi%K)e'inWid'e!('+3c!i"eForm'0array(
'id'=)':%erKform'0
'ena&e3IaJLa&ida!ion'=)tru
e0
'ena&e+&ien!La&ida!ion'=)t
rue0
'foc:%'=)array($mode&0'name
')
));
(*php //AiMm !ra NO: "Po mode& NQ NRn' chSa *
echo $formK)error1:mmary($mode&);
*)
(di" c&a%%="row")
(*php //AhEi !Fo !rSTn' name
echo '4Un !r:y cVp-';
echo $formK)!eJ!Fie&d($mode&0'name');
echo $formK)error($mode&0'name');
*)
(/di")
Th,m Captcha cho !orm
-N u ch a b t "gd2" th ta vo file php.ini r i s a l i dng ny ! "
M PHP:
eJ!en%ion=php_'d>.d&&
(*php $!hi%K)wid'e!('++ap!cha'0array(
':!!onLae&' =)
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
'LWy code mXi'0
'c&icka&eIma'e' =)
!r:e0
'ima'e7p!ion%' =
) array('id' =) 'cap!chaim'')
)); *)
:B: )C C n* T o !orm trong %i"#
Xem thm cc Method d add cc Field khc cho form Here
Ngoi cch dng 'CActiveForm' ta c th dng 'Chtml' d t o form: CHtml
Vi t L i form trn b ng cch dng 'CHtml':
M PHP:
(di" id='form')
(*php
echo +,!m&--e'inForm();
*)
(di" id='row')
(*php echo +,!m&--ac!i"eLae&($mode&0'name');*)
(r/)
(*php echo +,!m&--ac!i"e4eJ!Fie&d($mode&0'name')*)
(/di")
(di" id='row')
(*php echo +,!m&--ac!i"eLae&($mode&0'emai&');*)
(r/)
(*php echo +,!m&--ac!i"e4eJ!Fie&d($mode&0'emai&')*)
(/di")
(di" id='row')
(*php echo +,!m&--ac!i"eLae&($mode&0'pa%%');*)
(r/)
(*php echo +,!m&--ac!i"e@a%%wordFie&d($mode&0'pa%%')*)
(/di")
(di" id='row')
(*php echo +,!m&--ac!i"eLae&($mode&0'repa%%');*)
(r/)
(*php echo +,!m&--ac!i"e@a%%wordFie&d($mode&0'repa%%')*)
(/di")
(di" id='row')
(*php echo +,!m&--ac!i"eLae&($mode&0'da!e');*)
(r/)
(*php echo +,!m&--ac!i"e4eJ!Fie&d($mode&0'da!e')*)
(/di")
(di" id='%end')
(r/)
(*php echo +,!m&--%:mi!5:!!on('YZn' A[
'0array('id'=)'frm$e''0'name'=)'frm$e''));*)
(/di")
(*php
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
echo +,!m&--endForm();
*)
(/di")
Sau khi t o form xong th t i b c ti p theo l add cc rng bu c cho thng tin m user
nh p vo v d nh :
Cc text box khng d c b tr ng
Email phi dng d ng
Pass ph i l n h n 6 nh h n l5
Repass phi y chang Pass
Date: phi h p l theo ki u (dd-mm-yyyy) hay (mm-dd-yyyy)
Add thm captcha ch ng dng k hng lo t
Th,m Captcha cho !orm
-Yii c h tr s n th vi n captcha cho chng ta, v captcha th nhn cng r t l cool. #
T"st x"m cD th r"nd"r captcha hay $h>ng
- u tin truy c p th link ny d test xem c captcha hay khng ci d.
http://localhost/devyii/index.php?r=site/captcha n u ra nh v y l ok
-N u n khng hi n captcha th ta nn check l i xem d load "gd2" hay ch a b ng cch
ch y m c test c a Yii
gi ng nh hnh l load r i, n vng hay d th xem l i c u hnh webserver !
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
-N u ch a b t "gd2" th ta vo file php.ini r i s a l i dng ny ! "
M PHP:
eJ!en%ion=php_'d>.d&&
\ dW: .;. !rSXc n]0 mWy c^i kh^c NM defa:! N_n' !^y m^y0 re%!ar we%er"er
"P !e%! &Fi0 n`: "an &bi KK) !hci cPi &Fi win cho n] &Pnh
- t o captcha ta thm vo ph n view do n code sau:
M PHP:
(*php $!hi%K)wid'e!('++ap!cha'0array(
':!!onLae&' =)
'LWy code mXi'0
'c&icka&eIma'e' =)
!r:e0
'ima'e7p!ion%' =
) array('id' =) 'cap!chaim'')
)); *)
V i:
'buttonLabel' : hi n th m t link v i n i dung m ta set, khi click vo d s d i string
ca captcha
'clickablelmage': khi click vo image s d i sang hnh khc.
'imageOptions': set cc thu c tnh Html c a captcha
- M t captcha d c rendered b i m t hnh d ng c a l p CCaptchaAction, do d n u ch $
add bn view s khng hi n th hnh nh, v captcha khng d c render. Do d ta vi t thm
bn Controller:
M PHP:
p:&ic f:nc!ion ac!ion%()
d

re!:rn array(
'cap!cha' =) array (
'c&a%%' =) '++ap!cha3c!ion'0
'ack+o&or' =)'efff
'
)0
);
f
V i
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
'backColor': mu n n nh #fff l mu den,
'foreColor': mu ch captcha.
'height': chi u cao nh
'width': Chi u r ng
'maxLength': chi u di t i da c a ch captcha
'minLength': d di t i thi u c a string.
'verifyCode': l y gi tr m xc nh n
Cu i cng ra ci form nh v y:
- rng bu c cc thng tin m e nh p vo ta vi t m t ph ng th c trong Model nh sau:
M PHP:
p:&ic f:nc!ion r:&e%()
d
re!:rn array (
array ('code'0'cap!cha'0
'a&&owEmp!y'=)g
++ap!cha--check$e2:iremen!%()0
'me%%a'e' =) '=Q 8^c 6hVn 6hVp Ah
cn' YRn''
)0
array('emai&'0'emai&'0'me%%a'e' =)'da!!ri:!ef khcn' hh
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
p &i')0
array('name0emai&0pa%%0repa%%0da!e0code'0're2:ired'0'me
%%a'e'=).da!!ri:!ef khcn' NShc \ !rjn'.)0
array('repa%%'0'compare'0'compare3!!ri:!e'=)'pa%%'0'me
%%a'e'=).da!!ri:!ef phki chlnh J^c.)0
array('pa%%'0'&en'!h'0'min'=)'m'0'me%%a'e'=).da!!ri:!e
f khcn' Nn No dPi .)0
array('da!e'0'da!e'0'forma!'=)'ddKmmK
yyyy'0'me%%a'e'=).da!!ri:!ef phki chlnh J^c dFn' dddKmmKyyyyf.)0
);
f
ah?Hn% cBt /0u v8i Yii nramework
. au=n #4ch n:* d:nh cho ai o
NSu b5n l: m6t l9p tr>nh vi2n php v8i c4c kp n7n% hiFu biSt v; mmP
e$9p tr>nh h?8n% /=i t?Rn%f v: mu=n '@* dLn% hJ th=n% hiJn /5i v8i c4c
Zn% d`n% web th> cu=n #4ch n:* d:nh cho b5n<khCn% c. bIt kq m6t *2u
c0u %> /F /Ec cu=n #4ch Yii n:*.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
2. "5o controller helloworld
1F t5o n2n m6t controller tron% *ii b5n c. ! c4ch< c4ch m6t l: nSu b5n /r l:m
viJc )uen v8i *ii b5n c. thF t5o t^ m6t Oile m8i v: khai b4o n. v8i t2n cla##
/?Rc kS th^a t^ acomponent< c4ch hai l: t5o t^ dsn% lJnh tron% cmd c4ch
n:* b5n c. thF tham kh\o t^ ebook *ii cookbook hobc tr2n ditn /:n
*iivn.com< c4ch ! n:* c. ph0n dt hHn cho c4c b5n m8i t>m hiFu *ii /. l: t5o
controller t^ m6t cCn% c` tron% *ii c. t2n l: uii.
aIu h>nh uii
"r?8c khi #_ d`n% uii ch3n% ta c0n ph\i conOi% Zn% d`n% <
ch3n% ta ph\i tru* c9p v:o cIu h>nh Zn% d`n% chVnh t5i Oile tron% /Da
chv : protected/config/main.php enull cIu h>nh
a:g'amppghtdoc#gdev*iigprotectedgconOi%gmain.phpf c4c b5n l?u T l0n
#au tCi chv %G tBt /Da chv b5n ph\i tL v:o webroot eth? m`c web %=cf /F
t>m.
ah3n% ta v:o Oile v: thI* dsn% code #au /@* :
return arra*e
wba#ePathwxydirnamee++nz$E++f.Dz{Ea"m{Y+(EPA{A"m{.w..w<
wnamewxywY* Web Applicationw<
ddload th:nh ph0n lo%
wpreloadwxyarra*ewlo%wf<
dd tL /6n% load model v: th:nh ph0n cla##
wimportwxyarra*e
wapplication.model#.|w<
wapplication.component#.|w<
f<
'moduIes'=>array(
// Khng comment sau khi kch hot cng c ny
/*
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
'gii'=>array(
'cIass'=>'system.gii.GiiModuIe',
'password'=>'Enter Your Password Here',
//Nu xa i Gii s mc nh t IocaIhost.Hy iu chinh cn thn.
'ipFiIters'=>array('127.0.0.1','::1'),
),
*/
),
c5n b[ hai dsn% check comment /i v: /i;n m9t kh}u cKa b5n< hobc b5n c.
thF l:m theo tCi v8i m9t kh}u l: :
~hi /. /o5n module /?Rc tha* /Qi nh? #au:
'moduIes'=>array(
'gii'=>array(
'cIass'=>'system.gii.GiiModuIe',
'password'=>'1111',
'ipFiIters'=>array('127.0.0.1','::1'),
),
ah3n% ta v:o tr>nh du*Jt kiFm tra th_ : http:ddlocalho#tddev*iidinde'.phporx%ii
(au /. nhIn pa## l: b5n # tru* c9p v:o /?Rc Gii
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
c5n c. thF nh>n thI* cCn% c` chVnh
Controller Generator (T o ra controller)
Crud Generator (T o ra CRUD (C p nh t-s a-xa))
Form Generator (T o ra Form)
Model Generator (T o ra Model)
Module Generator (T o ra Module)
D nhi2n /F t5o m6t controller ta click v:o link Controller Generator menu
t5i /@* c. 2 tr?-n% l: ControIIerID v ActionID b5n viSt v:o /. controller v:
action b5n mu=n t5o <v8i tCi th> tCi t5o controller message v: action l:
helloworld /F ho:n tIt ch3n% ta nhIn v:o Preview #au /. uenerate ch3n% ta
# t5o ra /?Rc.
1F kiFm tra c4c b5n v:o protected/controller/ cc bn s thy file
MessageController.php , kim tra trong protected/iews bn s thy folder
message ! trong folder "# l! action helloworld.php
$im tra tr%n tr&nh d'y(t bn g) * http*//localhost/deyii/inde+.php,
r-message/helloworld bn thy k.t /'0 sa'*
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
1h2 3y l! bn "4 c# th to "25c 6 file controller ! action helloworld bn c#
th !o file MessageController " ti.n h!nh code theo 7 c8a bn .9:c ban
";' bn s thy nh<ng d=ng code sa'*
cIass MessageControIIer extends ControIIer
{
pubIic function actionHeIIoworId()
{
$this->render('heIIoworId');
}
.
}
Nu bn g http://IocaIhost/devyii/index.php?r=message m4* # tr\ v;
thCn% b4o lii 0
"hCn% th?-n% khi ch5* v:o controller theo mC h>nh YMa hJ th=n% # t>m theo
action /0u ti2n l: inde'< v> thS n2n controller hiJn t5i b5n khli t5o ch?a c.
actionznde'ef v: khi b5n c0n th2m action mbc /Dnh bjn% dsn% lJnh:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp
cla## Ye##a%eaontroller e'tend# aontroller

pubIic $defauItAction = 'heIIoworId';


Nh? v9* th> khi ch5* link tr2n n. # khCn% csn b4o lii 0 nAa bli v> hJ
th=n% /r nh9n action mbc /Dnh l: helloworld
Giai on cui cng
Miew# l: %iai /o5n cu=i c]n% /F 'uIt ra dsn% thCn% tin t?Hn% t4c trLc tiSp t8i
Zn% d`n% v> thS n2n /Hn %i\n l: b5n ml Oile
protecteddview#dme##a%edhelloword.php v: cu#tom t]* T vV d`:
hyHello Worlddhy
"r2n tr>nh du*Jt # hiFn thD nh? #au: http:ddlocalho#tddev*iidinde'.phpo
rxme##a%e
Thm ni dung ng
"ron% view# Oile b5n c. thF l:m bIt kq cCn% viJc li2n )uan t8i php bjn% c4ch
#_ d`n%ctec c4c th ml th /.n% cKa php nh? b>nh th?-n% ophp < oy
MV d` th2m th-i %ian cKa hJ th=n%:
h!yophp echo dateeD Y u:i:# " Yf oydh!y
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
{eOre#h l5i tr>nh du*Jt b5n # thI* n%:* %i- cKa hJ th=n%.
N%o:i c4ch nh3n% mr php /F hiFn thD n%:* %i- ra ta csn c. m6t c4ch kh4c
cLc kq )uan trEn% /. l: %enerate dA liJu t^ model ra view thCn% )ua
controller /. l: ph?Hn% thZc ho5t /6n% theo mC h>nh YMa.
ah3n% ta c]n% 'em vV d` #au: b5n v:o
protecteddcontrollerdYe##a%eaontroller.php v: thI* actionHelloworldef:
public Ounction actionHelloworldef

thi#yrenderewhelloworldwf

ah3n% ta khai b4o m6t biSn m8i v: render n. #an% view# bjn% c4ch #au:
pubIic function actionHeIIoworId()
{
$theTime = date("D M j G:i:s T Y");
$this->render('heIIoworId',array('time'=>$theTime));
}
"iSp /. b5n v:o protecteddview#dme##a%edhelloworld.php v: hiFn thD biSn time
/. nh? #au :
ca* %io la:h!yophp echo time oydh!y
$?u l5i Oile view v: v:o tr>nh du*Jt 'em kSt )u\:
http:ddlocalho#tddev*iidinde'.phporxme##a%e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Nh? v9* l: c4c b5n v^a l:m )uen viJc thao t4c dA liJu theo mC h>nh YMa <
b@* %i- ta l:m th_ c4ch m6t /. l: tL t5o action m8i theo T m>nh <%i\ #_ tCi t5o
action %oodb*e:
cla## Ye##a%eaontroller e'tend# aontroller

P
pubIic function actionGoodbye()
{
$this->render('goodbye');
}
...

"iSp theo ta v:o tron% protecteddview#dme##a%ed v: t5o m8i Oile


%oodb*e.php m6t c4ch /Hn %i\n l: b5n cop* Oile helloworld.php v: pa#te t5i
/. #au /. rename th:nh %oodb*e.php v: th2m v:o Oile m8i v8i n6i dun% #au
/@*:
a hreOxdhelloworlddinde'.phporxme##a%ed%oodb*eyuoodb*eday
1F kiFm tra b5n v:o http:ddlocalho#tddev*iidinde'.phporxme##a%ed%oodb*e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"iSn h:nh m6t #= #L trR %i3p cKa Yii ahtml
"h9t ma* mBn< Yii hi trR c4c ph?Hn% thZc /F c. thF #_ d`n% tron% view#
template c4c ph?Hn% thZc tnh /. thn t5i t^ #L trR %i3p cKa Oramework v8i
cla## CHtmI. "ron% tr?-n% hRp ch3n% ta mu=n t5o c4c link tnh v8i
controllerzD dactionzD tha* v> c4c cIu tr3c link# cH b\n cKa H"Y$. 1F #_ d`n%
link helper ch3n% ta th2m /o5n mr #au v:o Oile helloworld.php tron% Oolder
protecteddview#dme##a%edhelloworld.php:
<p><?php echo CHtmI::Iink('Goodbye',array('message/goodbye')); ?
></p>
M:o tr>nh du*Jt 'em kSt )u\ : http:ddlocalho#tddev*iidinde'.phporxme##a%e #
hiFn thD t?Hn% tL nh? h>nh b2n tr2n.
"?Hn% tL b5n th_ #_a tron% Oile %oodb*e.php tron%
protecteddview#dme##a%ed%oodb*e.php:
hyuoodb*e< Yii developerdhy
pyophp echo aHtml::linkewHellow<arra*ewme##a%edhelloworldwffoydpy
M:o tr>nh du*Jt ta c. kSt )u\ : http:ddlocalho#tddev*iidinde'.phpo
rxme##a%ed%oodb*e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ah?Hn% 2 ui8i thiJu n% d`n% Developer Yii
X@* dLn% m6t dL 4n m8i tr2n Yii Oramework
Ch:ng ta c# th ti.p t>c gi< i(c th%m cc ?ng d>ng "@n gi0n nh2
Ahello,worldB tC Dii, nh2ng "iE' "# s khFng thGc sG gi:p bn hi' "25c E
yii framework trong ng< c0nh c8a ?ng d>ng ngo!i "Hi thGc, " l!m "25c "iE'
n!y ch:ng ta c;n +Iy dGng mJt ?ng d>ng mKi,"# l! nJi d'ng c8a ch2@ng
n!y.
Ch:ng ta s giKi thi(' proLectMtaskMtracking application N dG n * taskNnhi(m
>OM tracking Ncch th?cO c8a ?ng d>ng "25c gPi l! Developer Yii. C# nhiE'
proLect /'0n l7 khc ! iss'eMtrackingMapplication N cc vIn /; M cch th?c
c8a ?ng d>ng "# trong ngo!i "Hi th2c. Qn s "Rt ra cI' hSi * ATaU sao ph0i
+Iy dGng n# ,B, bVi & n# tr0 E cc th'Jc tWnh c@ b0n c8a ?ng d>ng ! +a
h@n c# t3p l(nh trong ?ng d>ng.
Gii thiu v DeveIoper Yii
DeveIoper Yii bao %hm ! *Su t= chVnh l: dL 4n eproectf < n%?-i #_ d`n%
eu#erf< c4c vIn /; ei##uef.
"ron% /. u#er bao %hm hai tr5n% th4i :
Anon*mou# l: m6t kh4ch n%o:i /7n% nh9p v:o hJ th=n% khCn% c0n ph\i
thCn% )ua viJc '4c thLc )u*;n tru* c9p hE chv c. )u*;n /7n% kT t:i kho\n v:
kVch ho5t t:i kho\n< khCn% c. )u*;n )u\n trD.
N%?-i d]n% /?Rc '4c thLc eauthenticatedf l: bIt kq n%?-i d]n% n:o /?Rc
'4c thLc c4c ti2u chu}n thCn% )ua )u4 tr>nh /7n% nh9p v:o hJ th=n%<n%?-i
#_ d`n% n:* c. c4c )u*;n /0* /K v; )u\n trD dL 4n< thiSt l9p dL 4n< c9p nh9t
dL 4n.
a4c dL 4n eProect#f
Wu\n trD c4c dL 4n chVnh thLc hiJn tron% Zn% d`n% "rack(tar< m6t dL 4n
/?Rc thF hiJn d?8i nhi;u mZc /?Rc thLc hiJn bli m6t ha* nhi;u n%?-i
a4c vIn /; ei##uef:
a4c vIn /; tron% dL 4n %hm ! lo5i #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Xa hHn: a4c item /?Rc tr>nh ditn 'a hHn tron% thS %i8i thLc khi /?Rc th2m
v:o Zn% d`n% vV d` : thLc thi h:m lo%in /F '4c thLc u#er.
a4c chZc n7n%: ztem /?Rc hiFn thD c0n ph\i ho:n th:nh c4c cCn% viJc khCn%
%bp lii v: #L c=.
a4c lii: ztem khCn% ho5t /6n% ho:n h\o do lii ph4t #inh tron% hJ th=n% hobc
t4c /6n% b2n n%o:i.
a. ! tr5n% th4i chVnh tron% c4c vIn /; /. l: :
ah?a bBt /0u<
cBt /0u<
~St th3c.
"h:nh vi2n cKa dL 4n c. thF th2m nhi;u vIn /; t8i dL 4n cn% nh? viJc
/i;u chvnh v: '.a item.ah3n% c. thF /?Rc /7n% kT t^ b\n th@n n%?-i viSt
hobc th:nh vi2n cKa dL 4n.
c@* %i- ch3n% ta # 'em khun% l:m viJc cKa Zn% d`n% m8i:
"^ biFu /h n:* ta c. thF nh>n #H )ua chi tiSt cKa dL 4n
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
~hi m6t n%?-i d]n% bBt /0u tru* c9p Zn% d`n%< hE ph\i /7n% nh9p v: '4c
thLc )u*;n tru* c9p t8i Zn% d`n%< nSu /7n% nh9p th:nh cCn% hE # /?Rc
'em c4c dL 4n hiJn t5i cKa hE hobc hE c. thF th2m m8i.
~hi lLa chEn m6t dL 4n n:o /. # kVch ho5t v:o chi tiSt cKa tran% /.<chi tiSt
cKa dL 4n # hiFn thD thCn% tin danh #4ch c4c vIn /; tron% dL 4n<n%o:i ra hE
cn% c. thF th2m m8i< chvnh #_a< '.a /i.
1. l: tIt c\ nhAn% %> tron% Zn% d`n% b5n c0n ph\i )uan t@m.
Quan h d Iiu
ah3n% ta cn% ph\i )uan t@m v; mC h>nh dA liJu # l:m viJc <ch3n%
ta # #_ d`n% activerecord /?Rc kS th^a t^ Yii n. # l?u dA dA liJu m:
ch3n% ta mu=n chu*Fn t8i Yodel. ah3n% ta c. thF nh>n #H )ua v; biFu /h cKa
dL 4n :
ah3n% ta c. thF nh>n #H )ua v:o b\n% biFu v: thI* c. c4c thu6c tVnh
#au:
Y6t n%?-i d]n% # c. nhi;u vIn /; kh4c nhau v> thS b\n% biFu # c. li2n kSt
0 hobc nhi;u.
Y6t n%?-i d]n% cn% # c. nhi;u dL 4n nh?n% mii m6t dL 4n /?Rc t5o ra
bli Vt nhIt m6t n%?-i bli thS n2n c. )uan hJ ..| li2n )uan t8i 0 Pnhi;u.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Yii m6t dL 4n # c. nhi;u vIn /; kh4c nhau< nh?n% m6t vIn /; kh4c nhau
chv thu6c tron% m6t dL 4n m: thCi v> v9* n2n c. )uan hJ 0 ..| vIn /; chZa
/Ln% tron% dL 4n
~St n=i t8i cH #l dA liJu edataba#ef
Yii hi trR h0u hSt c4c c#dl d5n% DcY( < PDm bao %hm:
(W$ite: #)lite:dpathdtoddbOile
Y*(W$: m*#)l:ho#txlocalho#tdbnamexte#tdb
Po#t%re(W$: p%#)l:ho#txlocalho#tportx!2dbnamexte#tdb
(W$ (erver: m##)l:ho#txlocalho#tdbnamexte#tdb
mracle: oci:dbnamexddlocalho#t:2dte#tdb
"5i /@* tCi # #_ d`n% m*#)l l:m cH #l dA liJu.
c5n v:o protecteddconOi%dmain.php v: t>m dsn% #au:
wdbwxyarra*e
wconnection(trin%w xy
w#)lite:w.dirnamee++nz$E++f.wd..ddatadte#tdrive.dbw<
f<
dd uncomment the Oollowin% to u#e a Y*(W$ databa#e
/*
wdbwxyarra*e
wconnection(trin%w xy
wm*#)l:ho#txlocalho#tdbnamexte#tdrivew<
wemulatePreparew xy true<
wu#ernamew xy wrootw<
wpa##wordw xy ww<
wchar#etw xy wutO&w<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f<
*/
c5n c. thF nh>n thI* hiJn t5i mbc /Dnh *ii #_ d`n% #)lite < tu* nhi2n tCi l5i
mu=n #_ d`n% m*#)l n2n b5n #_a l5i thCn% #= v: b[ dsn% check comment
cKa m*#)l< tha* v:o /. b5n th2m dsn% check comment v:o #)lite v: b5n
c. kSt )u\ #au:
/*
wdbwxyarra*e
wconnection(trin%w xy
w#)lite:w.dirnamee++nz$E++f.wd..ddatadte#tdrive.dbw<
f<
*/
dd uncomment the Oollowin% to u#e a Y*(W$ databa#e
wdbwxyarra*e
wconnection(trin%w xy
wm*#)l:ho#txlocalho#tdbnamexdev*iiw<
wemulatePreparew xy true<
wu#ernamew xy wrootw<
wpa##wordw xy ww<
wchar#etw xy wutO&w<
f
"5i /@* tCi khai b4o c4c thCn% #= cKa m*#)l %hm u#ername< pa##word<
databa#e c. t2n l: dev*ii< cCn% viJc tiSp theo cKa b5n l: v:o phpm*admin
v: t5o databa#e c. t2n dev*ii:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
NhIn create v: databa#e cKa b5n /r /?Rc t5o ra.
!.X@* dLn% a{D cho proect
~S ho5ch nh? #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
1F kSt th3c ch?Hn% n:*< dL 4n cKa b5n c0n ph\i cho phNp u#er t5o m8i
proect< lLa chEn item tron% danh #4ch proect< c9p nh9t v: #_a '.a c4c
proect /..
aCn% viJc cKa ch3n% ta b@* %i- l: :
"hiSt kS cH #l dA liJu /F hi trR cho proect
X@* dLn% c4c b\n% biFu c0n thiSt v: c4c /=i t?Rn% dA liJu kh4c /?Rc
/Dnh n%ha tron% b\n%
"5o Yii A{ eActive{ecordf e~Vch ho5t c\n %hif model cla## cho phNp
n%?-i d]n% dt d:n% thao t4c v8i b\n% c#dl
"5o Yii controller cla## # l: n%Ci nh: chZa c4c h:m #au:
. "5o m8i proect
2. $iJt k2 ra danh #4ch dL 4n
!. a9p nh9t dA liJu li2n )uan v8i proect
. X.a c4c dA liJu tron% proect
"5o Oile Miew# tron% *ii v: hiFn thD cIu tr3c cH b\n #au:
. HiFn thD Oorm cho phNp n%?-i d]n% t5o m8i proect
2. HiFn thD danh #4ch tIt c\ proect
!. HiFn thD Oorm cho phNp u#er c9p nh9t thCn% tin tron% proect
. "h2m m6t n3t '.a /F u#er c. thF '.a proect /.
M9* l: m~< ch3n% ta bBt /0u tiSn h:nh '@* dLn% theo cIu tr3c tr2n
!. "5o b\n% cho proect
ah3n% ta hr* )ua* trl l5i c4i biFu /h mC h>nh u#erproecti##ue
chZa c4c kSt n=i v: ch3n% ta thiSt kS b\n% theo mC h>nh /. < b\n% proect bao
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
%hm nhAn% tr?-n% #au: id einte%erf inde' <name evarchar 2f t2n proect<
de#cription te't not null mC t\ proect< create+time datetime n%:*
t5o<create+u#er+id inte%er n%?-i d]n% t5o ra proect /.<update+time
datetime n%:* c9p nh9t l5i proect<update+time+u#er+id inte%er mr u#er
c9p nh9t dL 4n /.< v: tIt nhi2n id # l: kh.a chVnh< b5n c. thF tL t5o b\n%
bjn% )uer* #)l hobc /Hn %i\n hHn l: chNp /o5n d?8i /@* rhi d4n v:o tab
)uer* ()l tron% phpm*admin
a{EA"E "Ac$E tbl+proect e
id zN"ef Nm" N$$ auto+increment<
name varchare2f Nm" N$$<
de#cription te't Nm" N$$<
create+time DA"E"zYE deOault N$$<
create+u#er+id zN"EuE{ deOault N$$<
update+time DA"E"zYE deOault N$$<
update+u#er+id zN"EuE{ deOault N$$<
P{zYA{Y ~EY eidf
f ENuzNE x znnoDc
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
!.2 "5o A{ eActive-Recordf model cla##
(au khi ch3n% ta /r c. b\n% dA liJu tbl+proect rhi ch3n% ta
c0n t5o Yii model cla## cho phNp ch3n% ta dt d:n% )u\n trD dA
liJu tron% b\n%. ah3n% tCi 'in %i8i thiJu la*er Yii# m{Y<
Active-Record
c5n c0n /7n% nh9p v:o http:ddlocalho#tddev*iidinde'.phpo
rx%iiddeOaultdlo%in %G m9t kh}u #au /. nhIn enter /F v:o uii
1F t5o model b5n click v:o Yodel uenerator<
"5i /@* %hm c4c tr?-n% #au:
"able PreOi' c\n% ch3n% ta v^a t5o l: tbl+proect v: c. preOi' eti;n t= f
l: tbl+
"able Name : "2n thLc cKa b\n% ch3n% ta v^a t5o l: tbl+proect
Yodel cla## # tL #inh ra l: Proect nSu khCn% b5n hr* /i;u chvnh /3n%
nh? v9* enhi;u b5n mBc lii #ai n%8 n%}n l /o5n n:* n2n /Sn khi v:o
tron% code thBc mBc l: #ao em %enerate t^ tron% %ii m: khCn% hiFn thD
/?Rc dA liJuf
"iSp /. b5n /i;n /0* /K # %i=n% h>nh #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
aCn% viJc csn l5i /Hn %i\n l: click v:o Preview y uenerate l:
b5n /r ho:n th:nh thiSt kS model nhanh ch.n% tron% *ii .
1F kiFm tra b5n v:o protecteddmodel# # thI* Oile m8i /?Rc t5o ra c. t2n
Project.php nHi /@* l: thZ m: *ii tL /6n% #inh ra model cho b5n
c5n c. thF ml Oile proect.php ra /F 'em cIu tr3c< n. l: m6t cla## proect
/?Rc kS th^a t^ databa#e la*er l: CactiveRecord
"iSp theo l: h:m khli t5o tr\ v; model cha:
p'blic static f'nction modelNXclass1ame-YYC9Z[[YYO
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
\
ret'rn parent**modelNXclass1ameO]
^
"iSp theo l: h:m khai b4o t2n b\n% tron% a(D$ et2n b\n% n:* tron% databa#e
phpm*adminddev*iif
p'blic f'nction table1ameNO
\
ret'rn _tblYproLect_]
^
"iSp theo l: h:m rule#ef cun% cIp c4c )u* tBc cn% l: nHi /Dnh n%ha c4c
ph0n t_ tron% b\n%
H:m tiSp theo l: relation#ef # t5o ra c4c m=i )uan hJ %iAa b\n% n:* v8i c4c
b\n% kh4c mEi thZ c4c b5n # /?Rc hEc hSt tron% c4c ch?Hn% t8i /@*.
"iSp theo l: khai b4o nhrn cKa c4c ph0n t_ tron% b\n% attribute$abel#ef
M: h:m mbc /Dnh cu=i c]n% l: h:m (earch et>m kiSm f theo ti2u chu}n /?Rc
kS th^a t^ adataProvider.
!.! ~Vch ho5t a{D cho proect
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ah3n% ta v^a t5o ra A{ model< tiSp /Sn l: %> o< theo mC h>nh YMa
th> rG r:n% ch3n% ta c0n ph\i c. controller /F thLc thi c4c h:nh /6n% t^ model
hobc '_ lT /0u v:o t^ u#er /Dnh danh< tiSp /. ch3n% ta ph\i %enerate c4c biSn
dA liJu v: item t^ model #an% view /F thLc hiJn tr?n% b:* dA liJu.~S ho5ch
n:* t?ln% ch^n% kh. kh7n nh?n% th9t /Hn %i\n v8i Yii nh- cCn% c` a{D
eareatet5o m8i<{ead /Ec< pdate c9p nh9t< Delete X.af
ah3n% ta )ua* trl l5i uii v: click v:o /?-n% link menu: arud uenerator /F
kVch ho5t tVnh n7n% n:*:
"5i /@* # 'uIt hiJn 2 tr?-n% l: Yodel cla## l: t2n Yodel b5n mu=n %enerate
/. l: Proect< v: controller cla## # tL /6n% /?Rc #inh ra v8i t2n Proect el?u T
nSu khCn% viSt hoa chA P th> ph\i #_a l5i th:nh viSt hoaf<
tiSp theo b5n nhIn previewy %enerate. HJ th=n% # ph\n hhi thCn% b4o
th:nh cCn% th9t /Hn %i\n /3n% khCn% b5n c. thF v:o tron%
protecteddcontroller v: b5n # nh>n thI* Oile m8i c. t2n Proectaontroller.php <
tiSp theo b5n v:o protecteddview#d c4c b5n cn% # nh>n thI* Oolder proect
v8i c4c Oile view /0* /K< t>m kiSm< c9p nh9t< #_a< '.aP
!. "5o m8i proect
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
(au khi /r c. ho:n th:nh /0* /K mC h>nh aruD rhi b5n c. thF tiSn h:nh kiFm
th_ v: t5o m8i m6t proect bjn% viJc v:o tr>nh du*Jt %G :
http:ddlocalho#tddev*iidinde'.phporxproect
"It nhi2n ban /0u # khCn% c. b\n %hi n:o v: b5n # nh>n thI* dsn% chA
no re#ult Oound< ta click v:o create proect l menu b2n ta* ph\i theo h>nh
#au:
HJ th=n% # *2u c0u b5n /7n% nh9p v: b5n # /7n% nh9p v8i t:i
kho\n mbc /Dnh cKa Yii Oramework l: : Account: demo < Pa##word: demo
1@* l: m6t t:i kho\n /F te#t u#er tron% *ii v: %G 'on% b5n # thI* m6t Oorm
*2u c0u nh9p c4c dL 4n< c4c b5n /i;n b^a v:o # c. kSt )u\ %i=n% #au /@*:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
1i;u n:* n%ha l: hJ th=n% /r thLc hiJn th:nh cCn% m6t b\n %hi v: /i v:o chi
tiSt b\n %hi /. < b@* %i- ta t5o th2m v:i b\n %hi nAa rhi )ua* trl l5i /?-n% link
http:ddlocalho#tddev*iidinde'.phporxproectdinde' # c. kSt )u\ t?Hn% tL #au:
1@* chVnh l: li#t item cKa dL 4n< khi b5n th2m m8i b5n /r chu*Fn #an% link
cKa action Add< b5n chvnh #_a th> tZc l: b5n /an% thLc thi action edit< v: b5n
click v:o chi tiSt m6t b\n %hi b5n /r /i v:o action detail
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
!. X4c thLc hRp lJ c4c tr?-n% dA liJu tron% norm
~hi l:m viJc v8i A{ model cla## khCn% chv c. norm tron% Yii< thiSt l9p '4c
thLc c4c )u* tBc 'un% )uanh c4c tr?-n% dA liJu cKa norm l: c0n thiSt< /i;u
/. %i3p cho viJc kiFm tra nhAn% dA liJu /bc biJt v: h5n chS lii tron% c#dl< n.
/?Rc thLc hiJn /0* /K tron% m6t m\n% /Dnh n%ha l h:m rule#ef nh? tCi /r
n.i tron% model Proect.php
ah3n% ta v:o Protecteddmodel#dProect.php v: ch3n% ta /r thI* h:m n:* /r
/?Rc /nh n%ha mbc /Dnh khi %enerate v8i m6t #= )u* tBc:
d||
| ,tr\ v; m\n% dA liJu '4c thLc cho c4c ph0n t_ cKa model
|d
public Ounction rule#ef

dd c5n chv /Dnh n%ha c4c )u* tBc cho c4c ph0n t^ /. khi m: ch3n%
dd # nh9n /?Rc thCn% tin t^ Oorm input e/0u v:o ch3n% ta v^a nh9p Tf
return arra*e
arra*ewname< de#criptionw< wre)uiredwf< ddt2n v: mC t\ /?Rc *2u c0u
arra*ewcreate+u#er+id< update+u#er+idw< wnumericalw< dd*2u c0u kiFu #=
n%u*2n
winte%ermnl*wxytruef<
arra*ewnamew< wlen%thw< wma'wxy2f<dd*2u c0u t=i /a khCn% )u4 2 kT tL
arra*ewcreate+time< update+timew< w#aOewf<
dd (au /@* l: khai b4o )u* tBc th?-n% /?Rc #_ d`n% bli #earch
dd Mui lsn% '.a /i c4c ph0n t_ khCn% mu=n t>m kiSm.
arra*ewid< name< de#cription< create+time< create+u#er+id<
update+time< update+u#er+idw< w#aOew< wonwxyw#earchwf<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f

Ph?Hn% thZc {ule#ef tr\ v; m6t m\n% nhAn% )u* tBc<mii )u* tBc l: theo /Dnh
d5n% #au:
Arra*eDanh #4ch ph0n t_< X4c thLc< onxyDanh #4ch kDch b\n<P th2m
nhi;u thCn% tin kh4cf
(au /@* l: m6t lo5t c4c ti2u chu}n '4c thLc /?Rc /Dnh n%ha t^ Yii ver#ion
..2
booIean: 1Dnh danh cKa acooleanMalidator< ~iFm tra ph0n t_ c. chZa %i4
trD true hobc Oal#e e/3n%#aif
captcha: 1Dnh danh cKa aaaptchaMalidator< ~iFm tra %i4 trD cKa ph0n t_ c.
%i=n% v8i h>nh \nh captcha nh9p v:o ha* khCn%
compare: 1Dnh danh cKa aaompareMalidator< (o #4nh hai ph0n t_ v: kiFm
tra ch3n% %i=n% nhau ha* kh4c nhau
emaiI: 1Dnh danh cKa aEmailMalidator< ~iFm tra %i4 trD ph0n t_ d5n%
email addre##
date: 1Dnh danh cKa aDateMalidator< ~iFm tra %i4 trD ph0n t_ d5n%
date< time< or datetime value
defauIt: 1Dnh danh cKa aDeOaultMalueMalidator< th2m v:o mbc /Dnh %i4 trD
ph0n t_
exist: 1Dnh danh cKa aE'i#tMalidator< ~iFm tra %i4 trD ph0n t_ c. thn t5i
tron% table column tron% databa#e
fiIe: 1Dnh danh cKa anileMalidator< ~iFm tra %i4 trD ph0n t_ c. chZa t2n
uploadOile ha* khCn%
fiIter: 1Dnh danh cKa anilterMalidator< chu*Fn /Qi %i4 trD ph0n t_ v8i %i4 trD
m8i cKa Oilter
in: 1Dnh danh cKa a{an%eMalidator< validate# iO the data i# within a
pre#peciOied
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ran%e oO value#< or e'i#t# within a #peciOied li#t oO value#
ddc.c dDch nAa mJt )u4 < /o5n cu=i bao %hm kiFm tra /6 d:i elen%thf< kiFm tra
/3n% v8i biFu thZc to4n hEc emathf< kiFm tra d5n% #= n%u*2n enumericalf<
kiFm tra %i4 trD c. /?Rc *2u c0u bBt bu6c ha* khCn%ere)uiredf< kiFm tra thu6c
tVnh c. thu6c d5n% /bc biJt ha* khCn% et*pef< kiFm tra %i4 trD c. ph\i l: du*
nhIt ha* khCn% euni)uef< kiFm tra dA liJu c. ph\i l: /o5n {$ ha* khCn%eurlf
Iength: Alia# oO a(trin%Malidator< validate# whether the len%th oO the
attribute value i# within a #peciOied ran%e
match: Alia# oO a{e%ularE'pre##ionMalidator< u#e# a re%ular e'pre##ion
to validate the attribute value
numericaI: Alia# oO aNumberMalidator< validate# whether the attribute value
i# a valid number
required: Alia# oO a{e)uiredMalidator< validate# whether the attribute
value i# empt* or not
type: alia# oO a"*peMalidator< validate# whether the attribute value i# oO a
#peciOic data t*pe
unique: Alia# oO ani)ueMalidator< validate# that the attribute value i#
uni)ue< and i# compared a%ain#t a databa#e table column
urI: Alia# oO arlMalidator< validate# whether the attribute value i# a
valid {$
!. 1Ec proect
1@* chVnh l: chi tiSt cKa m6t item b5n c. thF v:o /?-n% link #au /F 'em th_
m6t item:
http:ddlocalho#tddev*iidinde'.phporxproectdviewidx
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
!. a9p nh9t v: '.a proect
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
~hi c4c b5n nh>n v:o menu b2n ta* ph\i # thI* h:n% lo5t c4c link menu nh?
#au:
~hi b5n c9p nh9t v: t5o< 'em danh #4ch< /Ec chi tiSt /;u /?Rc nh?n% khCn%
thF '.a hobc )u\n lT v: bD b4o lii acce## denid bli v> b5n /an% #_ d`n%
account d:nh cho u#er khCn% /?Rc )u*;n '4c thLc< v> thS b5n lo%out ra
n%o:i v: /7n% nh9p l5i bjn% account m8i l: : Account: admin < Pa##word:
admin < b5n # ho:n to:n c. thF '.a v: 'em m`c )u\n lT /?Rc.
c5n c. thF nh>n thI* b2n tr2n c. h:n% lo5t c4c C lEc t>m /. l: do b5n
khai b4o b2n h:m rule#ef< b5n c. thF v:o tron% /. v: '.a /i chv /F l5i 2
tr?-n% l: id< name th> c. v hRp lT hHn.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Chong 3 . Qun I cc vn
(Issue)
`3y l! hFm nay "4 l! nam mKi bchMd6 ph:t ng!y eb/eb/6ebf, tFi "ang nggi V gh. "
V b%n cnh "hi di(n Ki t25ng "!i i2ng jo `2@ng Tr;n k'hc T'n, hFm nay th3t "Fng
ng2Hi "i c;' may c;' lJc, c=n tFi th& ln l!m i(c, cFng i(c mang con ng2Hi ta ".n sG ch.t
ch#c, m(t mSi cang thmng nh2ng lao "Jng l! inh /'ang h4y "#n nh3n n#, "2Hng phh "Fng
nghnt ng2Hi cH hoa giang kWn nh<ng bFng c:c !ng ! bFng hgng "S rGc, cc "Fi t&nh nhIn
tay trong tay do b2Kc, bn bo /'Iy /';n b%n nha',nh&n sang b%n tay tri l! "hi di(n Ki
thp rpa cq kWnh, mJt sG y%n lRng ! trnh mUch nh2 hn c# c8a n#,sc# l chtm gi# nhiE'
ch0 c# l5i Wch g& cho i(c hPc c8a cc bn n%n ch:ng ta t3p tr'ng !o cFng i(c but ";' +Iy
dGng b0ng d< li(' th? 6 l! iss'es .v /'%n tr2Kc khi !o l!m cwng ph0i ch:c mPi ng2Hi nam
mKi 'i x hnh ph:c ch? hehey
"ron% c4c ch?Hn% tr?8c ch3n% ta /r thLc thi h:m cH b\n 'un% )uanh
thLc thF proect< b@* %i- ch3n% ta thLc hiJn v8i i##ue# ch?Hn% n:* kh4 d:i v>
v9* c4c b5n c= %Bn% t9p trun%.
Proect l: th:nh ph0n chK /5o cKa Zn% d`n% m8i tu* nhi2n mii dL 4n
tL ch3n% khCn% th9t #L ho:n h\o< proect chZa /Ln% nhAn% vIn /; li2n )uan
khi ch3n% ta mu=n )u\n trD Zn% d`n%<l3c /. )u\n trD c4c vIn /; tron% dL 4n l:
m`c ti2u chVnh thLc hiJn tron% Zn% d`n%< ch3n% ta # mu=n th2m m6t #=
h:m chZa vIn /; )u\n trD cH b\n.
~S ho5ch 'a hHn
ah3n% ta /r t5o /?Rc th:nh ph0n )u\n trD proect< nh?n% ch3n% ta
ch?a c. c4ch n:o )u\n lT c4c vIn /; ei##uef li2n )uan /Sn dL 4n /.< khi kSt
th3c ch?Hn% n:* ch3n% ta mon% mu=n l: thLc hiJn /?Rc tIt c\ a{D tron%
vIn /; cKa dL 4n hobc c4c nhiJm v` eta#kf.tron% dA liJu l model<m6t nhiJm
v` eta#kf # luCn luCn chv c. m6t thu6c tVnh cKa i##ue. ah3n% ta cn% mu=n
trVch ra tIt c\ a{D tron% i##ue# c]n% v8i proect< /i;u /. c. n%ha l: iss'es
th'Jc E Nbelong toO proLect N92' 7 "Iy l! /'an h( M relationshipO.
#er ph\i lLa chEn c4c dL 4n /r thn t5i v: l:m viJc< liJt k2 danh #4ch
thLc hiJn a{D v8i c4c vIn /; tron% dL 4n.
ah3n% ta hr* nh>n danh #4ch /; n%hD b5n c0n l:m nh? #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
. "hiSt kS biFu /h dA liJu v: '@* dLn% /=i t?Rn% /F hi trR c4c vIn
/; tron% dL 4n
2. "5o Yii model cla## cho phNp Zn% d`n% dt d:n% khli t5o kSt n=i
v8i dA liJu tron% b\n%
!. "5o controller cla## # chZa c4c h:m cho phNp ch3n% ta thLc
hiJn :
"5o m8i vIn /;
$Ec danh #4ch c4c vIn /; thn t5i tron% proect t^ databa#e
a9p nh9td/i;u chvnh c4c vIn /;
X.a c4c vIn /; /.
. "5o view# Oolder /F render %iao diJn n%?-i d]n% cho c4c action
c@* %i- ch3n% ta /i v:o thLc hiJn
III.1 Thit k biu
"ron% ch?Hn% tr?8c ch3n% ta c. thF nh>n #H )ua v; T t?ln% cKa thLc
thF i##ue#< ch3n% ta thLc hiJn n. c. c4c tr?-n% dA liJu %hm : name t2n vIn
/;< t*pe thu6c tVnh vIn /;< owner b\n th@n< re)ue#tor n%?-i *2u c0u<
#tatu# tr5n% th4i< v: de#cription mi2u t\. ah3n% tCi cn% /; n%hD #au khi
t5o b\n% tbl+proect ch3n% ta c0n m6t #= thCn% tin kh4c nh? date#< time#<v:
u#er c9p nh9t b\n% /.."u* nhi2n type , owner, re/'estor, stat's b\n th@n
ch3n% l: nhAn% thLc thF. "ron% /. owner b\n th@n< re)ue#tor n%?-i *2u
c0u l: hai n%?-i d]n% #_ d`n% hJ th=n% v: hE # t4c /6n% v:o b\n% dL liJu
thCn% )ua b\n% tbl+u#er. ah3n% ta bBt /0u li2n t?ln% t8i 'ser tron% b\n%
tbl+proect /. l: create+u#er+id v: update+u#er+id l: nhAn% /Dnh danh cKa
u#er c9p nh9t ha* t5o ra dL 4n< /F '4c thLc /?Rc ch3n% ta c0n kh.a n%o5i
eOorei%n ke*f tham chiSu t8i b\n% tbl+u#er< l3c /. ownerYid ! re/'estorYid
tron% b\n% tbl+i##ue# cn% # c. kh.a n%o5i tham chiSu t8i b\n% tbl+u#er. Dt
hiFu con m l2n /?Rc
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
III.1.1 nh ngha mt s quan h (relationships
"^ khi ch3n% ta %i8i thiJu b\n% tbl+u#er < ch3n% ta c0n /Dnh n%ha l5i
)uan hJ %iAa u#er v: proect< tron% ch?Hn% tr?8c c4c u#er # li2n )uan t8i 0
hobc nhi;u proect< ch3n% ta cn% c. thF thiSt l9p proect c. 1 hobc nhi;u
u#er. ah3n% ta %Ei )uan hJ %iAa hai b\n% tr2n l: )uan hJ nhi;u nhi;u
emany - to - many). "h9t dt d:n% /F '@* dLn% )uan hJ man*toman* tron%
model< ch3n% ta c. thF nh>n #H )ua v; )uan hJ cH b\n cKa thLc thF )uan hJ
%iAa u#er< proect< i##ue#. Proect c. thF c. 0 hobc nhi;u u#er<m6t u#er c0n
li2n )uan t8i Vt nhIt m6t proect nh?n% c. thF li2n )uan nhi;u<z##ue# ebelon%
tof thu6c v; mt v: chv m6t proect < khi proect c. thF c. t^ 0 /Sn nhi;u
u#er. Y6t vIn /; /?Rc kSt th3c v: /?Rc /7n% kT bli en%?-i *2u c0uf v: chv
du* nhIt m6t u#er.
mr Yore : hobc nhi;u
0 or Yore : 0 hobc nhi;u
E'actl* mne: ahv m6t
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
III.1.! "#$ %&ng 'i t()ng t* nh+ng quan h tr(,- '.
ah3n% ta # ph\i t5o ra ! b\n% m8i bao %hm tbl+i##ue< tbl+u#er v:
b\n% trun% %ian tbl+proect+u#er+a##i%nment
Bng t/l0issue
create table tblYiss'eN
id intNbbO not n'll primary key a'toYincrement,
name archarN6ddO not n'll,
description te+t,
proLectYid intNbbO z{|Z}9T 1}99,
typeYid intNbbO z{|Z}9T 1}99,
stat'sYid intNbbO z{|Z}9T 1}99,
ownerYid intNbbO z{|Z}9T 1}99,
re/'esterYid intNbbO z{|Z}9T 1}99,
createYtime datetime z{|Z}9T 1}99,
createY'serYid intNbbO z{|Z}9T 1}99,
'pdateYtime datetime z{|Z}9T 1}99,
'pdateY'serYid intNbbO z{|Z}9T 1}99
O{1~1{-nnozQ]
aop* v: pa#te t5o b\n% cho n. nhanh nh?n% ch3 T nh>n c4c tr?-n% dA
liJu m: nh8.
Tip theo I tabIe t/l0user
create table tblY'serN
id intNbbO not n'll primary key a'toYincrement,
'sername archarNfeO not n'll 'ni/'e key,
email archarNbeeeO not n'll,
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
password archarN6ddO not n'll,
lastYloginYtime datetime z{|Z}9T 1}99,
createYtime datetime z{|Z}9T 1}99,
createY'serYid intNbbO z{|Z}9T 1}99,
'pdateYtime datetime z{|Z}9T 1}99,
'pdateY'serYid intNbbO z{|Z}9T 1}99
O{1~1{-nnozQ]
V cui cng I bng trung gian
create table tblYproLectY'serYassignmentN
proLectYid intNbbO 1T 1}99 a'toYincrement,
'serYid intNbbO 1T 1}99,
MZD ${D NproLectYid,'serYidO
O{1~1{-nnozQ]
To kha ngoi quan h gia tbI_issue v tbI_project
Zlter table tblYiss'e Zdd constraint fkYiss'eYproLect foreign
keyNproLectYidO references tblYproLectNidO on delete cascade on 'pdate restrict
To kha ngoi quan h gia user vi owner (bn thn ngi )
tbI_issue v tbI_user
alter table tblYiss'e add constraint fkYiss'eYowner foreign
keyNownerYidO references tblY'serNidO on delete cascade on 'pdate restrict
To kha ngoi quan h gia user v ngi yu cu (requestor)
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
alter table tblYiss'e add constraint fkYiss'eYre/'ester foreign
keyNre/'esterYidO references tblY'serNidO on delete cascade on 'pdate restrict
To kha ngoi quan h gia bng trung gian vi project
alter table tblYproLectY'serYassignment add constraint fkYproLectY'ser
foreign keyNproLectYidO references tblYproLectNidO on delete cascade on 'pdate
restrict
To kha ngoi quan h gia bng trung gian v user
alter table tblYproLectY'serYassignment add constraint
fkY'serYproLects foreign keyN'serYidO references tblY'serNidO on delete
cascade on 'pdate restrict
(au khi thLc hiJn 'on% c4c )uer* 'on% b5n hr* ml m6t #= cCn% c`
theo dGi mC h>nh #chema databa#e /F c. thF nh>n thI* mC h>nh dA liJu v8i
c4c kh.a
III.1.1 23o 45 (4-tive 5e-or% mo%el -lass
B@* %i- ch3n% ta /r c. /0* /K c4c b\n%< b?8c tiSp theo l: t5o model
v8i Yii model A{ cho phNp ch3n% ta dt d:n% thao t4c v8i b\n% tron% Zn%
d`n%< ch3n% ta cn% /r l:m viJc n:* tron% viJc t5o model cKa Proect l
ch?Hn% tr?8c v> thS n2n thao t4c # khCn% c. %> tha* /Qi nhi;u< ch3n% ta v:o
Gii
III.1.6 23o Issue 7o%el -lass
M:o uii v8i /?-n% dkn : http:ddlocalho#tddev*iidinde'.phpo
rx%iiddeOaultdlo%in v: nhIn ch3n% ta # v:o menu %enerate
ah3n% ta click v:o ModeI Generator < t5i tr?-n% table preOi' ch3n% ta
/i;n tbl+ e/@* l: ti;n t= b\n%f
"5i tr?-n% table name ta /i;n : tbl+i##ue< t5i tr?-n% Yodel cla## ta
/i;n z##ue
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"iSp theo b5n nhIn preview y %enerated.
ah3n% ta c. thF v:o protecteddmodel# /F kiFm tra v: # thI* z##ue.php
zzz.. "5o #er model cla##
MiJc t5o #er model cla## l: khCn% c0n thiSt bli v> b\n th@n Yii /r
hi trR thao t4c v8i u#er khi #_ d`n% Yii# DAm < A{ model cun% cIp m6t ciFu
/h )uan hJ /=i t?Rn% embect {elational Yappin% m{Yf cho Zn% d`n% %i3p
ch3n% ta dt d:n% )u\n lT /=i t?Rn% tron% domain.
zzz.. "5o z##ue a{D operation
c5n v:o uii v: click v:o menu link CRUD Generator #au /. /i;n v:o tr?-n%
Yodel ala## : z##ue< /i;n v:o tr?-n% controller id : z##ue e$?u T z l /0u /?Rc
viSt hoaf y preview y %enerated.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
(au khi thLc hiJn 'on% ta /r ho:n th:nh v: v:o tr>nh du*Jt kiFm tra:
http:ddlocalho#tddev*iidinde'.phporxi##ue
zzz.. "5o m8i z##ue
(au khi thLc hiJn 'on% b5n t5o m8i m6t v:i b\n %hi v: v:o /7n% nh9p v8i
acc: admin pa##: admin khi hJ th=n% *2u c0u /7n% nh9p
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zzz..& "h2m thu6c tVnh drop down
ah3n% ta # t5o ! thu6c tVnh dropdown bao %hm cu%#<neature#< "a#k#. ~hi
ch3n% ta t5o m8i i##ue # c. ! lLa chEn cho Oorm Oield.
M:o protecteddmodel#dz##ue cla## v: /Dnh n%ha c4c hjn% #au:
con#t "YPE+cux0
con#t "YPE+nEA"{Ex
con#t "YPE+"A(~x2
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"iSp theo '@* dLn% h:m %et"*pemption#ef # tr\ v; %i4 trD cho c4c thu6c tVnh
tr2n:
public Ounction %et"*pemption#ef

return arra*e
#elO::"YPE+cuxywcu%w<
#elO::"YPE+nEA"{Exywneaturew<
#elO::"YPE+"A(~xyw"a#kw<
f

zzz.. "h2m thu6c tVnh dropdown v:o view#


M:o protecteddview#di##ued+Oorm.php v: t>m tr?-n% Type tron% Oorm:
di class-row
,php echo XformMlabel{+NXmodel,_typeYid_O] ,
,php echo XformMte+t|ieldNXmodel,_typeYid_O] ,
,php echo XformMerrorNXmodel,_typeYid_O] ,
/di
"iSp theo b5n nh>n l2n /o5n /0u cKa Oile # c. /o5n khai b4o Oorm:
,php Xform-XthisMbeginidgetN_CZctie|orm_, arrayN
_id_-_iss'eMform_,
_enableZLa+`alidation_-false,
OO] ,
1@* l: c4ch /Dnh n%ha m6t Oorm #_ d`n% aactivenorm tron% Yii.
"iSp theo b5n v:o z##ueaontroller.php tron%
protecteddcontrollerdz##ueaontroller v: nh>n v:o h:m actionareateef.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
p'blic f'nction actionCreateNO
\
Xmodel-new ss'e]
// }ncomment the following line if ZZ alidation is needed
// XthisMperformZLa+`alidationNXmodelO]
ifNissetNXY[T_ss'e_OO
\
XmodelMattrib'tes-XY[T_ss'e_]
ifNXmodelMsaeNOO
XthisMredirectNarrayN_iew_,_id_-XmodelMidOO]
^
XthisMrenderN_create_,arrayN
_model_-Xmodel,
OO]
^
"5i /@* ch3n% ta c. thF nh>n thI* view# /?Rc render< n. /an% tr\ v; m6t
khli t5o cKa z##ue model< v: %i4 trD tr\ v; %Ei l: model.
c@* %i- ch3n% ta )ua* l5i Oile view# /F 'em c4ch '_ lT cKa tr?-n% Type tron%
Oorm. Dsn% /0u l: :
OormylabelE'emodel<wt*pe+idwf
Dsn% n:* #_ d`n% aactivenorm::labelE'ef ph?Hn% thZc chu*Fn )ua nhrn
H"Y$ cho i##ue model v8i ph0n t_ typeYid. N. cn% /?Rc khli t5o tron%
model cla## v: #_ lT c4c nhrn ph0n t_ cho nhAn% nhrn ta mu=n tha* /Qi. 1.
l: h:m z##ue::attribute$abel#ef /?Rc #_ d`n% /F )u*St /Dnh lLa chEn label<
nSu ch3n% ta nh>n v:o Oorm th> nhrn cKa typeYid l: "*pe /?Rc chu*Fn )ua
t^ h:m attribute$abel#ef.
p'blic f'nction attrib'te9abelsNO
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
\
ret'rn arrayN
_id_ - _z_,
_name_ - _1ame_,
_description_ - _zescription_,
_proLectYid_ - _roLect_,
8t$pe0i%8 9: 82$pe8;
_stat'sYid_ - _[tat's_,
_ownerYid_ - _wner_,
_re/'esterYid_ - _e/'ester_,
_createYtime_ - _Create Time_,
_createY'serYid_ - _Create }ser_,
_'pdateYtime_ - _}pdate Time_,
_'pdateY'serYid_ - _}pdate }ser_,
O]
^
TrV li iew Ki tr2Hng t$pe0i% ta c# th nh&n s@ /'a nh2 sa'*
,php echo XformMlabel{+NXmodel,_typeYid_O] ,
(_ d`n% labelE'ef cn% c. thF '_ lT /?Rc dsn% tVch /[ khi Oield# dA liJu /?Rc
*2u c0u. labelE'ef # th2m m6t c## cla## t2n aHtml::re)uireda## v8i mbc
/Dnh l: re)uired v: aHtml::aOter{e)uired$abel v8i mbc /Dnh l: #pan
cla##xre)uiredy|d#pany #au mii ph0n t_ /?Rc *2u c0u.
Dsn% tiSp theo ophp echo Oormyte'tnieldemodel<t*pe+idfoy #_ d`n%
aactivenorm::te'tnieldef ph?Hn% thZc /F chu*Fn )ua d5n% te'tinput Oield
cho ph0n t_ t*pe+id tron% z##ue model.cIt kq )u* tBc kiFm tra dA liJu /;u
/?Rc /Dnh n%ha tron% z##ue::rule#ef < ph?Hn% thZc n:* # '4c thLc c4c )u*
tBc v8i Oorm /0u v:o.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
au=i c]n% l: ophp echo Oormyerroremodel<t*pe+idfoy /?Rc #_ d`n%
aactivenorm::erroref /F chu*Fn )ua '4c thLc lii li2n )uan v8i ph0n t_
t*pe+id.
c5n th_ nh>n l5i tVnh '4c thLc c4c thu6c tVnh dA liJu < typeYid c6t /?Rc /Dnh
n%ha l: inte%er tron% b\n% Y*()l v> v9* uii %enerated v8i )u* tBc '4c thLc
tron% z##ue::rule#ef ph?Hn% thZc n:* /Dnh n%ha nh? #au:
public Ounction rule#ef

dd Nm"E: *ou #hould onl* deOine rule# Oor tho#e attribute# that
dd will receive u#er input#.
return arra*e
arra*ewnamew< wre)uiredwf<
array('project_id, type_id, status_id, owner_id, requester_id,
create_user_id, update_user_id', 'numericaI', 'integerOnIy'=>true),
ahVnh v> '4c thLc )u* tBc n:* n2n khi b5n /i;n d5n% chA tron% Oorm v8i
tr?-n% "*pe # %bp b4o l=i #au /@*:
"iSp theo /F hiFn thD dropdown li#t ch3n% ta c0n tha* thS dsn% c cKa t*pe+id
tron% protecteddview#di##ued+Oorm.php:
div cla##xrowy
ophp echo OormylabelE'emodel<wt*pe+idwf oy
ophp echo Oormyte'tnieldemodel<wt*pe+idwf oy
ophp echo Oormyerroremodel<wt*pe+idwf oy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ddivy
"ha* thS th:nh :
div cla##xrowy
ophp echo OormylabelE'emodel<wt*pe+idwf oy
ophp
echo OormydropDown$i#temodel<wt*pe+idw< modely%et"*pemption#eff
oy
ophp echo Oormyerroremodel<wt*pe+idwf oy
ddivy
Xon% 'uCi v:o tr>nh du*Jt 'em kSt )u\: http:ddlocalho#tddev*iidinde'.phpo
rxi##uedcreate
1F '4c thLc t*pe+id tron% Oorm b5n c0n %i8i h5n '4c thLc e{an%e Malidationf
v8i aran%eMalidator cKa Yii hi trR< b5n th2m v:o h:m z##ue::rule#ef dsn% #au:
arra*ewt*pe+idw< winw< wran%ewxy#elO::%etAllowed"*pe{an%eeff<
public Ounction rule#ef

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
dd Nm"E: *ou #hould onl* deOine rule# Oor tho#e attribute# that
dd will receive u#er input#.
return arra*e
arra*ewnamew< wre)uiredwf<
array('type_id', 'in', 'range'=>seIf::getAIIowedTypeRange()),
arra*ewproect+id< t*pe+id< #tatu#+id< owner+id<
re)ue#ter+id< create+u#er+id< update+u#er+idw< wnumericalw<
winte%ermnl*wxytruef<
arra*ewnamew< wlen%thw< wma'wxy2f<
arra*ewde#cription< create+time< update+timew< w#aOewf<
dd "he Oollowin% rule i# u#ed b* #earchef.
dd Plea#e remove tho#e attribute# that #hould not be
#earched.
arra*ewid< name< de#cription< proect+id< t*pe+id< #tatu#+id<
owner+id< re)ue#ter+id< create+time< create+u#er+id< update+time<
update+u#er+idw< w#aOew< wonwxyw#earchwf<
f

"a '@* dLn% th2m h:m %etAllowed"*pe{an%eef tron% cla##:


public #tatic Ounction %etAllowed"*pe{an%eef

return arra*e
#elO::"YPE+cu<
#elO::"YPE+nEA"{E<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
#elO::"YPE+"A(~<
f

H:m validator rule# n:* kiFm tra input /0u v:o tr\ v; c. njm tron% danh #4ch
ha* khCn%< khi b5n #_a /Qi thCn% tin v8i /0u v:o l: kSt )u\ # thS n:* :
zzz..0 (_a l5i tr?-n% dA liJu owner !
re/'ester
Y6t vIn /; ch3n% ta %bp ph\i v8i viJc t5o Oorm z##ue l: Oield owner v:
re)ue#ter l: OreeOorm te'tinput . "u* nhi2n ch3n% ta biSt rjn% %i4 trD inte%er
tron% b\n% i##ue /an% %iA /Dnh danh kh.a n%o5i eOorei%n ke*f t8i c6t id cKa
b\n% tbl+u#er. M> thS n2n ch3n% ta c0n th2m dropdown Oield cho ch3n%.
ah3n% ta khCn% thF biSt /Vch '4c mii kS ho5ch ch3n% ta l:m cho ph;n t
type ! stat's, cwng nh2 owner v: re/'esters /?Rc tiSn h:nh t^ tbl+u#er.
Nh? v9* mii n%?-i d]n% tron% hJ th=n% # li2n )uan v8i proect bli nhAn%
vIn /; ei##uef li2n )uan. M: nhAn% i##ue /. khCn% thF dropdown v8i dA liJu
t^ b\n% tbl+u#er. ah3n% ta c0n lEc ra danh #4ch /0u v:o cKa nhAn% n%?-i
d]n% c. li2n )uan t8i proect /..
1i;u n:* /?Rc /; n%hD tron% kS ho5ch 'a hHn l ch?Hn% n:*< ch3n% ta c0n
)u\n lT vIn /; v8i khCn% chv n6i dun% cKa proect /bc biJt. N%ha l: proect
/bc biJt # /?Rc chEn tr?8c khi b5n c. thF t5o vIn /; ei##uef m8i. ~hi m6t
proect /?Rc chEn ch3n% ta c0n chBc chBn c\ b\n th@n eownerf v: n%?-i *2u
c0u ere)ue#terf dropdown lLa chEn /?Rc trVch lEc ra nhAn% u#er li2n )uan t8i
dL 4n.
zzz.. ~iFm tra n6i dun% proect
ah3n% ta mu=n chBc chBn rjn% n6i dun% proect hRp lJ /?Rc hiFn thD
tr?8c khi ch3n% ta cho phNp tru* c9p t8i )u\n trD z##ue. 1F l:m /i;u n:*
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ch3n% ta c0n l:m viJc thLc thi c4i %Ei l: b6 lEc eOilterf. Y6t b6 lEc tron% *ii l:
nhAn% /i;u chvnh cIu h>nh /?Rc thLc hiJn tr?8c khi hobc #au khi m6t
controller action /?Rc thLc hiJn. Y6t vV d` v; lJnh l: nSu ch3n% ta mu=n chBc
chBn n%?-i d]n% /r /7n% nh9p t^ viJc thLc hiJn controller action method.
ah3n% ta c. thF viSt /Hn %i\n b6 lEc tru* c9p /. # kiFm tra c4c *Su t= tr?8c
khi action /. /?Rc thLc hiJn.
zzz..2 1Dnh n%ha Oilter# eb6 lEcf
Y6t Oilter#eb6 lEcf c. thF /?Rc /Dnh n%ha nh? m6t controller cla## hobc
n. c. thF l: m6t cla##. ~hi #_ d`n% kS hobc /Hn %i\n e#implemethodf< t2n
ph?Hn% thZc ph\i bBt /0u bjn% t^ filter v: c. m6t kT tL /bc biJt. aho vV d`
nSu ch3n% ta /an% t5o m6t b6 lEc ph?Hn% thZc : #omeYethodName< ta #
viSt Oilter nh? #au:
public Ounction Oilter(omeYethodNameeOilterahainf

...

a. m6t kS ho5ch kh4c /F #_ d`n% Oilter /. l: d]n% cla## thLc hiJn


Oilter lo%ic< khi #_ d`n% cla## Oilter ph\i /?Rc kS th^a t^ aOilter v: /?Rc
t\i Vt nhIt prenilteref hobc po#tnilteref method )u*St /Dnh mZc lo%ic /F thLc
hiJn tr?8c hobc #au m6t action /?Rc khli t5o.
zzz..! "h2m m6t nilter
c@* %i- ch3n% ta # th2m m6t Oilter tron% z##ueaontroller cla## /F /i;u
khiFn kiFm tra m6t proect hRp lJ. ah3n% ta tiSn h:nh kS ho5ch:
Yl protecteddcontroller#dz##ueaontroller.php v: viSt h:m Oilter #au l
/o5n d?8i %0m cla## e#au Ounction #earchf.
public Ounction OilterProectaonte'teOilterahainf
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

Oilterahainyrunef

mk< b@* %i- ch3n% ta /r /Dnh n%ha m6t Oilter< tu* nhi2n ch?a c. %>
nhi;u l /@* /Hn %i\n chv l: thLc thi Oilter eOilterahainyruneff< c4i m: '_ lT
Oilter v: cho phNp thLc hiJn c4c ph?Hn% thZc action /?Rc lEc bli method<
/i;u n:* man% t8i m6t /iFm kh4c< $:m c4ch n:o ch3n% ta /Dnh n%ha c4i
action method c. #_ d`n% Oilter o
zzz.. Action Oilter# /bc biJt
Yii Oramework c. cla## cH b\n cKa controller l: acontroller. N. l: m6t Oilter
method c0n /?Rc t\i #au mii thLc /Hn /bc biJt cKa action tron% c4i Oilter c0n
/?Rc 4p d`n%. "hLc tS ph?Hn% thZc n:* /r /?Rc t\i tron% z##ueaontroller
cla##.1i;u /. /?Rc l:m /0* /K khi ch3n% ta #_ d`n% uii tool /F tL /6n% t5o
ra cla## n:*. N. /r t5o ra accessControl Oilter c4i m: /?Rc /Dnh n%ha tron%
acontroller</F #_ lT m6t #= h:nh /6n% cH b\n /F chBc chBn rjn% n%?-i d]n%
/an% #_ d`n% /?Rc ph@n )u*;n #_ d`n% acction n:*.NSu b5n khCn% /7n%
nh9p v: click v:o link Create Issue b5n # bD chu*Fn h?8n% t8i tran% lo%in v:
/?Rc /Dnh danh cho t8i khi b5n /?Rc phNp t5o m8i i##ue.Acce## controll Oilter
'_ lT /i;u n:*. ah3n% ta # kh4m ph4 nhi;u hHn v; Oilter .
1F bBt /0u ch3n% ta c0n th2m m8i Oilter cho m\n% cIu h>nh
z##ueaontroller::Oilter#ef echo v:o c5nh h:m Oilter c cn% /?Rcf
public Ounction Oilter#ef

return arra*e
wacce##aontrolw< dd perOorm acce## control Oor a{D operation#
'projectContext + create', //check to ensure vaIid project
context
f
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

nilter# n:* tr\ v; m\n% cIu h>nh < tron% h:m Oilter# tr?8c ch3n% ta t5o
proectaonte't Oilter c4i m: /?Rc /Dnh n%ha nh? m6t ph?Hn% thZc v: 4p
d`n% th2m c\ action create . aIu h>nh n:* cho phNp v8i c4c c3 ph4p <
/F #_ d`n% v8i c4c Oilter /?Rc 4p d`n% v: khCn% /?Rc 4p d`n%.
aho vV d` ch3n% ta mu=n 4p d`n% v8i tIt c\ c4c action n%o5i tr^ action
pdateef v: actionMiewef ta l:m nh? #au:
{eturn arra*e
proectaonte't update< view<
f
c5n # khCn% c]n% ha* c]n% c\ hai action c]n% m6t l3c<
zzz.. "h2m m6t Oilter lo%ic
mk< b@* %i- ch3n% ta /Dnh n%ha Oilter v: ch3n% ta cIu h>nh n. %Ei khi
actionareateef /?Rc kVch ho5t v8i z##ue controller cla##.
"u* nhi2n n. vkn khCn% thLc hiJn theo lo%ic<khi /. ch3n% ta mu=n chBc chBn
proect conte't Oilter tr?8c khi action /?Rc khli /6n%<ch3n% ta c0n /bt lo%ic
Oilter tr?8c khi %Ei Oilterahainyrunef.
ah3n% ta # th2m thu6c tVnh proect t^ controller cla##<ah3n% ta # #_ d`n%
)uer* #trin% echuii tru* vInf t^ tham #= tron% {$ /F /Dnh danh proect.N%o:i
preAction Oilter # kiFm tra /F 'em ph0n t_ tron% proect c. thn t5i ha* null<nSu
/3n% n. # #_ d`n% )uer* #trin% tham #= /F khli /6n% viJc lLa chEn proect
tron% /Dnh danh kh.a chVnh eprimar* ke*f. NSu th:nh cCn% action # thLc thi<
nSu thIt b5i n%o5i lJ # /?Rc 'uIt ra< #au /@* l: code : c5n hr* tha* thS n6i
dun% c4c h:m Oilter bjn% c4c h:m m8i d?8i /@* tron%
protecteddcontrollerdz##ueaontrller.php
cla## z##ueaontroller e'tend# aaontroller

....
d||
| ,var private propert* containin% the a##ociated Proect model in#tance.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
|d
private +proect x null
d||
| Protected method to load the a##ociated Proect model cla##
| ,param inte%er proectzd the primar* identiOier oO the a##ociated Proect
| ,return obect the Proect data model ba#ed on the primar* ke*
|d
protected Ounction loadProecteproectzdf

ddiO the proect propert* i# null< create it ba#ed on input id
iOethi#y+proectxxxnullf

thi#y+proectxProect::modelefyOindc*Pkeproectzdf
iOethi#y+proectxxxnullf

throw new aHttpE'ceptione0<Yeu cau proect khon% ton tai.wf

return thi#y+proect

d||
| zncla## deOined Oilter method< conOi%ured Oor u#e in the above Oilter#ef
| method. zt i# called beOore the actionareateef action method i# run in
| order to en#ure a proper proect conte't
|d
public Ounction OilterProectaonte'teOilterahainf

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
dd#et the proect identiOier ba#ed on uE" input re)ue#t variable#
iOei##ete+uE"wpidwff
thi#yloadProecte+uE"wpidwf
el#e
throw new aHttpE'ceptione0!<ahi cac proect dac biet hien ra truoc khi thuc hien
action na*.wf
ddcomplete the runnin% oO other Oilter# and e'ecute the re)ue#ted action
Oilterahainyrunef

...

(au khi tha* thS ho:n chvnh b5n v:o tr>nh du*Jt kiFm tra:
http:ddlocalho#tddev*iidinde'.phporxi##uedcreate
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zzz.. "h2m Proect zD
ch?Hn% t5o proect ch3n% ta /r th2m nhi;u proect /F kiFm tra hJ th=n%<
tu* nhi2n nhAn% ai ch?a t5o th> vui lsn% v:o http:ddlocalho#tddev*iidinde'.phpo
rxproect t5o kho\n% v:i proect /F chu}n bD cho cCn% viJc tiSp theo n:*.cli
v> cCn% viJc tiSp theo *2u c0u ph\i c. zD cKa proect /F c. thF th2m id n:* t8i
viJc t5o m8i i##ue {$.
"iSp theo ch3n% ta c0n /i;u chvnh link# tron% view Oile i##ue :
protecteddview#di##uedinde'.php. Nh>n phVa tr2n /0u Oile b5n # thI* nHi link#
/?Rc /Dnh n%ha )ua m\n% menu item#:
thi#ymenuxarra*e
array('IabeI'=>'Create Issue', 'urI'=>array('create')),
arra*ewlabelwxywYana%e z##uew< wurlwxyarra*ewadminwff<
f
1F th2m chuii tham #= tru* vIn e)uer* #trin% paramterf t8i link# ch3n% ta
/Hn %i\n th2m tr?-n% namexyvalue tron% ph0n /Dnh n%ha m\n% tham #=
url< ch3n% ta th_ l:m v8i proect idx < /i;u chvnh dsn% areate z##ue:
arra*ewlabelwxywareate z##uew< wurlwxyarra*ewcreatew< wpidwxyff<
c@* %i- khi b5n 'em tran% i##ue li#tin% < b5n # nh>n thI* areate z##ue v8i
link url nh? #au: http:ddlocalho#tddev*iidinde'.phporxi##uedcreatepidx
1@* l: chuii tham #= tru* vIn cho phNp lEc thu6c tVnh thiSt l9p proect
conte't< l3c n:* khi b5n click v:o link http:ddlocalho#tddev*iidinde'.phpo
rxi##uedcreate # hiJn lii 0! v: chv c. link tr2n m8i hiFn thD
zzz.. 1i;u chvnh chi tiSt tran% proect
MiJc th2m proect+id t8i url cho viJc t5o m8i i##ue eareate new i##uef link l:
thLc #L c0n thiSt cho %iai /o5n /0u /F chBc chBn rjn% b6 lEc /r l:m viJc
hiJu )u\. "u* nhi2n ch3n% ta luCn luCn nh>n thI* link url khi t5o m8i i##ue
luCn v8i proect id l: < tIt nhi2n khCn% ph\i /i;u ch3n% ta mu=n. NhAn% %>
ch3n% ta mu=n l:m l: c. m6t menu option cho viJc t5o m8i i##ue trl th:nh
m6t ph0n cKa tran% chi tiSt proect. M8i c4ch n:*<b5n c. thF lLa chEn m6t
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
proect t^ danh #4ch proect < proect conte't /bc biJt # /?Rc hiFn thD v:
ch3n% ta* th2m /6n% proect id /F t5o m8i link# i##ue< hr* l:m n. tha* /Qi.
Yl protecteddview#dproectdview.php. Nh>n l tr2n /0u Oile b5n # thI* m\n%
menu thi#ymenuxarra*eP.f. ah3n% ta c0n th2m link kh4c /F t5o m8i i##ue
vD trV kSt th3c danh #4ch menu
thi#ymenuxarra*e
arra*ewlabelwxyw$i#t Proectw< wurlwxyarra*ewinde'wff<
arra*ewlabelwxywareate Proectw< wurlwxyarra*ewcreatewff<
arra*ewlabelwxywpdate Proectw< wurlwxyarra*ewupdatew<
widwxymodelyidff<
arra*ewlabelwxywDelete Proectw< wurlwxyww< wlinkmption#wxyarra*ew#
ubmitwxyarra*ewdeletew<widwxymodelyidf<wconOirmwxywAre *ou #ure *ou
want to delete thi# itemowff<
arra*ewlabelwxywYana%e Proectw< wurlwxyarra*ewadminwff<
array('IabeI'=>'Create Issue', 'urI'=>array('issue/create',
'pid'=>$modeI->id)),
f
M:o tr>nh du*Jt %G http:ddlocalho#tddev*iidinde'.phporxproectdviewidx
c5n # nh>n thI* cu=i menu l: link t5o i##ue<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
c@* %i- ch3n% ta /r c. thu6c tVnh proect conte't /?Rc thiSt l9p khi t5o m8i
i##ue< ch3n% ta c. thF remove tr?-n% Oorm proect cn% nh? tr?-n% Oorm u#er
input. Yl protecteddview#di##ued+Oorm.php '.a b[ /o5n #au:
div cla##xrowy
ophp echo OormylabelE'emodel<wproect+idwf oy
ophp echo Oormyte'tnieldemodel<wproect+idwf oy
ophp echo Oormyerroremodel<wproect+idwf oy
ddivy
"u* nhi2n d?-n% nh? proect+id # khCn% /?Rc #ubmit t^ Oorm<ch3n% ta #
c0n thiSt l9p tham #= proect+id t^ b6 lEc eOilterf m: ch3n% ta m8i thLc thi.
(au /. ch3n% ta /r biSt /?Rc proect+id li2n )uan<hr* thiSt l9p %i4 trD cKa
z##ue::proect+id t^ %i4 trD cKa id b\n% proect /?Rc c:i /bt bli Oilter tr?8c /..
M> v9* ch3n% ta c0n /i;u chvnh z##ue#aontroller::actionareateef method:
public Ounction actionareateef

modelxnew z##ue
$modeI->project_id = $this->_project->id;
c5n t5o m8i i##ue v8i proect+id x 2 v: # c. kSt )u\ :
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zzz..& "rl l5i v8i dropdown owner re/'ester
au=i c]n% < ch3n% ta trl l5i v8i nhAn% %> ch3n% ta thiSt l9p tr?8c /.<v8i #L
tha* /Qi owner v: re)ue#ter khi lLa chEn dropdown hRp lJ khi lLa chEn
proect.
"ron% thLc /Hn n:* ch3n% ta c0n m6t #= u#er li2n )uan t8i proect< #au /.
)u\n trD u#er tron% Oocu# cKa ch?Hn% #au<ch3n% ta # l:m nhi;u hHn viJc
th2m /?-n% dkn li2n )uan t8i databa#e v8i /?-n% dkn #)l ch3n% ta c0n th2m
2 u#er v:o m*#)l:
zN(E{" zN"m tbl+u#er eemail< u#ername< pa##wordf MA$E( ewte#t,
notanaddre##.comw<w#er mnew< YDewte#twff< ewte#t2,notanaddre##.
comw<w#er "wow< YDewte#t2wff
~hi b5n ch5* Zn% d`n% m8i n:*< b5n # t5o 2 u#er m8i tron% hJ th=n% v8i id
l: v: 2. ah3n% ta c0n a##i%n e/7n% kTf hai u#er n:* t8i proect
zN(E{" zN"m tbl+proect+u#er+a##i%nment eproect+id< u#er+idf
MA$E( e<f< e<2f
Y6t /i;u tu*Jt v-i cKa )uan hJ tron% Active {ecord v8i Yii l: kh\ n7n% tru*
c9p hRp lJ th:nh vi2n cKa Zn% d`n% /. v8i i##ue v8i )uan hJ thu6c v;
ebelon% tof<tham chiSu t^ i##ue model khli t5o.(au khi ch3n% ta #_ d`n% uii
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
tool khli t5o i##ue model cla##< ch3n% ta /r chBc chBn tVch v:o check bo'
tr?-n% BuiId ReIations th?-n% l: mbc /Dnh /r tVch rhi.1i;u n:* cho phNp
ch3n% ta '@* dLn% m=i )uan hJ %iAa c4c b\n%. ah3n% ta c. thF nh>n h:m
relation#ef t^ protecteddmodel#dz##ue.php
(au /. ch3n% ta t5o cla## #au khi th2m m=i )u5n hJ tron% databa#e :
public Ounction relation#ef

ddNm"E: *ou ma* need to adu#t the relation name and the related
dd cla## name Oor the relation# automaticall* %enerated below.
return arra*e
wre)ue#terw xy arra*e#elO::cE$mNu(+"m< w#erw< wre)ue#ter+idwf<
wownerw xy arra*e#elO::cE$mNu(+"m< w#erw< wowner+idwf<
wproectw xy arra*e#elO::cE$mNu(+"m< wProectw< wproect+idwf<
f

l /@* )uan hJ re)ue#ter thu6c v; u#er.re)ue#ter+id bli i##ue.re)ue#ter /r


tham chiSu t8i b\n% tbl+u#er< t?Hn% tL v8i 2 tr?-n% csn l5i.
1@* l: cIu h>nh /?Rc khli t5o t^ A{ model< v8i )uan hJ n:* ch3n% ta c. thF
tru* c9p v:o A{ cla## li2n )uan m6t c4ch dt d:n%.cho vV v`< ch3n% ta mu=n
tru* c9p m6t proect t^ m6t i##ue li2n )uan:
ddcreate the model in#tance b* primar* ke*:
i##ue x z##ue::modelefyOindc*Pkef
ddacce## the a##ociated Proect A{ in#tance
proect x i##ueyproect
~hi ch3n% ta t5o Proect model cla## v8i nhAn% b\n% kh4c /?Rc /Dnh n%ha
c. m=i )uan hJ erela#hion#hipf< tu* nhi2n b@* %i- ch3n% ta c. m6t #= )uan hJ
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
/?Rc /Dnh n%ha< ch3n% ta c0n th2m Proect::relation#ef. Yl
dprotecteddmodel#dProect.php v: tha* thS n6i dun% tron% h:m relation#ef:
public Ounction relation#ef

return arra*e
wi##ue#w xy arra*e#elO::HA(+YANY< wz##uew< wproect+idwf<
wu#er#w xy arra*e#elO::YANY+YANY< w#erw<
wtbl+proect+u#er+a##i%nmenteproect+id< u#er+idfwf<
f

z##ue#: ddm6t i##ue c. thF c. m6t hobc nhi;u tron% dL 4n


#er#: ddm6t u#er c. thF m6t hobc nhi;u tron% m6t hobc nhi;u dL 4n
M8i #L tha* thS tr2n ch3n% ta dt d:n% tru* c9p tIt c\ i##ue hobc u#er li2n
)uan t8i proect /. m6t c4ch dt d:n%.
proect x Proect::modelefyOindc*Pkef
allProectz##ue# x proectyi##ue#
all#er# x proectyu#er#
ownermOnir#tz##ue x proectyi##ue#0yowner
"hCn% th?-n% ch3n% ta c. thF viSt lJnh (W$ tr5n% th4i n=i /F tru* c9p t8i dA
liJu li2n )uan<#_ d`n% )uan hJ tron% A{ Yii %i3p ch3n% ta dt d:n% v: nhanh
%En hHn.
ah3n% ta c. thF tru* c9p )uan hJ dt d:n% dt /Ec v: dt hiFu.
III.1.19 Generating the data to populate the dropdowns
"heo kS ho5ch ch3n% ta c0n dropdown dA liJu cKa stat's v: type . ah3n% ta
# th2m h:m %et#ermption#ef t8i Proect model cla##:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Yl protecteddmodel#dProect.php v: th2m /o5n #au v:o d?8i cla##:
d||
| ,return arra* oO valid u#er# Oor thi# proect< inde'ed b* u#er zD#
|d
public Ounction %et#ermption#ef

u#er#Arra* x aHtml::li#tDataethi#yu#er#< widw< wu#ernamewf


return u#er#Arra*

/@* ch3n% ta #_ d`n% Yii# aHtml cla## %i3p ch3n% ta t5o m\n%
idxyu#ername t^ c4c u#er li2n )uan t8i proect. Nh8 rjn% thu6c tVnh u#er#
e/Dnh n%ha tron% method relation#eff tron% proect cla## khi #_ d`n% A{
model. aHtml::li#tDataef l: ph?Hn% thZc c. thF tiSn h:nh tron% danh #4ch v:
#_ d`n% m\n% hRp lJ t^ aactivenorm::dropDown$i#tef
c@* %i- ch3n% ta c. h:m %et#ermption#ef tr\ v; dA liJu ch3n% ta c0n< ch3n%
ta thLc thi dropdown /F hiFn thD dA liJu tr\ v;<ch3n% ta # #_ d`n% Oilter /F
thiSt l9p proect id li2n )uan t^ re)ue#t +uE"<v: ch3n% ta #_ d`n% %i4 trD /F
thiSt l9p proect+id khi t5o m8i i##ue l3c bBt /0u thLc hiJn
z##ueaontroller::actionareateef method< b@* %i- b5n c. c\m thI* A{ thLc #L
dt d:n% v: nhanh %En khCn% o
ah3n% ta hr* tha* /Qi tron% Oorm i##ue:
Yl protecteddview#di##ued+Oorm.php v: t>m hai tr?-n% te'tinput ownerYid v:
re/'esterYid v: tha* thS n. v8i /o5n #au:
ophp echo Oormyte'tnieldemodel<wowner+idwf oy
"rl th:nh
ophp echo OormydropDown$i#temodel<wowner+idw< modelyproect
y%et#ermption#eff oy
M: tha* thS : ophp echo Oormyte'tnieldemodel<wre)ue#ter+idwf oy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"h:nh : ophp echo OormydropDown$i#temodel<wre)ue#ter+idw<
modelyproecty%et#ermption#eff oy
M:o tr>nh du*Jt %G : http:ddlocalho#tddev*iidinde'.phporxi##uedcreatepidx
NSu khCn% v:o /?Rc v: b4o lii vui lsn% v:o uii< click menu : ModeI
generator #au /. t5i tr?-n% table preOi' : tbl+ < t5i tr?-n% table name :
tbl+u#er < t5i tr?-n% model cla## : #er #au /. previewy%enerated.
NSu vkn ch?a v:o /?Rc vui lsn% v:o uii< click menu : ModeI generator #au
/. t5i tr?-n% table preOi' : tbl+ < t5i tr?-n% table name :
tbl+proect+u#er+a##i%nment < t5i tr?-n% model cla## :
Proect#erA##i%nment #au /. previewy%enerated.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zzz..20 $:m l0n tha* /Qi cu=i c]n%
(au khi ch3n% ta bBt /0u t5o i##ue Oorm <hr* l:m nhanh m6t l0n tha* /Qi cu=i
c]n%<MiJc t5o time v: 'ser cn% nh? time 'pdate v: 'ser 'pdate Oield /.
ch3n% ta c. tron% mii b\n %hi l: cH #l hi#tor* elDch #_f v: /4nh dIu n%?-i
d]n% n:o c9p nh9t b\n %hi. (au /. ch3n% ta # /i;u chvnh Zn% d`n% lo%ic m6t
c4ch tL /6n% phQ biSn v8i Oield '_ lT in#ert hobc update. c@* %i- < hr* '.a /i
Oield input tron% Oorm cKa ch3n%:
M:o protecteddview#di##ued+Oorm.php '.a c4c /o5n #au:
div cla##xrowy
ophp echo OormylabelE'emodel<wcreate+timewf oy
ophp echo Oormyte'tnieldemodel<wcreate+timewf oy
ophp echo Oormyerroremodel<wcreate+timewf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wcreate+u#er+idwf oy
ophp echo Oormyte'tnieldemodel<wcreate+u#er+idwf oy
ophp echo Oormyerroremodel<wcreate+u#er+idwf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wupdate+timewf oy
ophp echo Oormyte'tnieldemodel<wupdate+timewf oy
ophp echo Oormyerroremodel<wupdate+timewf oy
ddivy
div cla##xrowy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp echo OormylabelE'emodel<wupdate+u#er+idwf oy
ophp echo Oormyte'tnieldemodel<wupdate+u#er+idwf oy
ophp echo Oormyerroremodel<wupdate+u#er+idwf oy
ddivy
(au khi thLc hiJn 'on% Oorm cKa ch3n% ta chv csn nh? #au:
zzz..2 X\ hHi v8i a{D
Ph0n /Vch cKa ch?Hn% n:* l: thLc thi tIt c\ a{D operation cho
z##ue. ah3n% ta c. kSt th3c viJc t5o ra c4c h:m< nh?n% ch3n% ta vkn c0n
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ho:n th:nh ph0n chi tiSt ereadf< c9p nh9t eupdatef< v: '.a edeletef cho z##ue<
"h9t ma* mBn h0u hSt ti2u chu}n n:* /r thLc thi tron% uii a{D khi
%enerated c4c h:m. "u* nhi2n #au /. ch3n% ta mu=n )u\n lT tIt c\ z##ue v8i
khCn% chv v7n c\nh cKa proect< ch3n% ta c0n l:m m6t #= /i;u chvnh #ao
cho ch3n% ta c. thF tru* c9p v:o c4c h:m chZc n7n%.
zzz..22 Danh #4ch v8i z##ue
Ybc d] ch3n% ta /r t^n% n%h v; actionznde'ef method tron%
z##ueaontroller cla## n. hiFn thD danh #4ch tIt c\ i##ue tron% a(D$< ch3n% ta
khCn% mu=n /i;u n:* tron% h:m chZc n7n%.D?-n% nh? khCn% th9t #L t=t khi
m: tIt c\ danh #4ch c4c i##ue tron% a(D$ /?Rc hiFn thD ra<M> thS n2n ch3n%
ta # /i;u chvnh Zn% d`n% /F hiFn thD danh #4ch cKa m6t ph0n i##ue li2n )uan
t8i proect tron% chi tiSt tran%. $3c /. ch3n% ta tiSn h:nh tha* /Qi )uan hJ A{
model tron% Yii< n. # l:m biFu /h tha* /Qi.
zzz..2! 1i;u chvnh proect controller
"r?8c ti2n ch3n% ta # /i;u chvnh actionMiewef method tron%
Proectaontroller cla##. (au /. ch3n% ta mu=n hiFn thD danh #4ch c4c i##ue
li2n )uan v8i c4c dL 4n /bc biJt m: ch3n% ta chEn< ch3n% ta c. thF l:m /i;u
n:* v8i c]n% m6t tran% chi tiSt proect. Yethod actionMiewef l: method /?Rc
hiFn thD tron% chi tiSt proect.
ahvnh l5i method #au: protecteddcontrollerdProectaontroller.php
public Ounction actionMieweidf

i##ueDataProviderxnew aActiveDataProviderewz##uew<
arra*ewcriteriawxyarra*e
wconditionwxywproect+idx:proectzdw<
wparam#wxyarra*ew:proectzdwxythi#yloadYodeleidfyidf<
f<
wpa%inationwxyarra*ewpa%e(iewxy<f<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ff
thi#yrenderewvieww<arra*ewmodelwxythi#yloadYodeleidf<
wi##ueDataProviderwxyi##ueDataProvider<
ff

"5i /@* ch3n% ta #_ d`n% aactiveDataProvider e~Vch ho5t nh: cun% cIp
dA liJu do Yii t5o raf Oramework cla## /F cun% cIp dA liJu #_ d`n% /=i
t?Rn% cKa aactive {ecord ekVch ho5t b\n %hif< n. # #_ d`n% c4c )uan hJ
A{ model li2n )uan /F trVch lEc dA liJu t^ a(D$ tron% /. v: ch3n% ta # #_
d`n% n. m6t c4ch dt d:n%. cjn% viJc '@* dLn% th:nh ph0n danh #4ch %Ei l:
ali#tMiew.
ah3n% ta #_ d`n% th:nh ph0n n:* /F hiFn thD danh #4ch cKa i##ue
tron% view Oile. ah3n% ta #_ d`n% thu6c tVnh ti2u chu}n /F )u*St /Dnh /i;u
kiJn /. # chv lEc danh #4ch i##ue li2n )uan t8i proect /an% /?Rc hiFn thD<
ch3n% ta cn% #_ d`n% thu6c tVnh ph@n tran% epa%inationf /F %i8i h5n b\n %hi
tron% danh #4ch i##ue /?Rc lEc ra.
"hZ cu=i c]n% ch3n% ta l:m th2m dA liJu /?Rc cun% cIp n:* v:o
m\n% /Dnh n%ha /?Rc %Ei l: renderef< /F l:m n. hRp lJ khi chu*Fn #an% view
Oile v8i biSn Xiss'ezataroide.
zzz..2! ahvnh l5i Oile Miew cKa proect
Nh? ch3n% tCi /r /; n%hD <ch3n% ta # #_ d`n% th:nh ph0n tron%
Oramework %Ei t8i ali#tMiew /F hiFn thD n%o:i danh #4ch c4c i##ue tron%
proect chi tiSt tran%. Yl protecteddview#dproectdview.php v: th2m /o5n #au
d?8i c]n% Oile:
br dy
hyProect z##ue#dhy
ophp thi#ywid%etewii.wid%et#.a$i#tMieww< arra*e
wdataProviderwxyi##ueDataProvider<
witemMiewwxywdi##ued+vieww<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ff oy
M9* l: ch3n% ta /r thiSt l9p thu6c tVnh dataProvider tron% ali#tMiew t^ i##ue
dataprovider m: ch3n% ta t5o tron% controller.M: #au /. ch3n% ta cIu h>nh n.
#_ d`n% t^ protecteddview#di##ued+view.php Oile nh? m6t template# cho
render mii item tron% data provider. nile n:* /r /?Rc t5o cho ch3n% ta bli
uii tool khi ch3n% ta %enerated a{D cho z##ue.ah3n% ta #_ d`n% n. t5i /@*
/F hiFn thD i##ue tron% proect chi tiSt tran%.
ah3n% ta cn% c0n l:m #L tha* /Qi protected/iews/iss'e/Yiew.php Oile /.
ch3n% ta /7 /i;u chvnh /bc biJt nh? m6t la*out template# cho mii i##ue.
ahvnh l5i n6i dun% thLc thF cKa Oile nh? #au:
div cla##xviewy
byophp echo aHtml::encodeedatay%etAttribute$abelewnamewff oy:
dby
ophp echo aHtml::linkeaHtml::encodeedataynamef< arra*ewi##ued
vieww< widwxydatayidff oy
br dy
byophp echo aHtml::encodeedatay%etAttribute$abelewde#cripti
onwff oy:dby
ophp echo aHtml::encodeedatayde#criptionf oy
br dy
byophp echo aHtml::encodeedatay%etAttribute$abelewt*pe+idwff
oy:dby
ophp echo aHtml::encodeedatayt*pe+idf oy
br dy
byophp echo aHtml::encodeedatay%etAttribute$abelew#tatu#+idwff
oy:dby
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp echo aHtml::encodeedatay#tatu#+idf oy
ddivy
mk< b5n v:o tr>nh du*Jt %G : http:ddlocalho#tddev*iidinde'.phpo
rxproectdviewidx2 'em kSt )u\:
III.1.!6 2i<n h=nh status v= type te"t '>
hi>n th
"r?8c /. ch3n% ta v^a th2m public method z##ue A{ cla##</F trVch
lEc #tatu# v: t*pe option t8i dropdown i##ue Oorm ch3n% ta c0n th2m m6t #=
method tron% A{ cla## /F tr\ v; te't cho #tatu# v: t*pe /bc biJt hobc t*pe id.
M:o protecteddmodel#dz##ue.php :
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
public Ounction %et(tatu#"e'tef

#tatu#mption#xthi#y#tatu#mption#
return i##ete#tatu#mption#thi#y#tatu#+idf o
#tatu#mption#thi#y#tatu#+id : unknown #tatu# ethi#y#tatu#+
idf

d||
| ,return #trin% the t*pe te't di#pla* Oor the current i##ue
|d
public Ounction %et"*pe"e'tef

t*pemption#xthi#yt*pemption#
return i##etet*pemption#thi#yt*pe+idf
o t*pemption#thi#yt*pe+id : unknown t*pe ethi#yt*pe+idf

"5i /@* # tr\ v; c4c tr5n% th4i value eahua #an #an%<(an #an%<ket thucf
v: thu6c tVnh te't ebu%<Oeature<ta#kf /F c:i /bt cho i##ue.
c5n v:o protecteddmodeldi##ue.php th2m /o5n khai b4o #au:
con#t ("A"(+Nm"YE"x0
con#t ("A"(+YE"x
con#t ("A"(+nzNA$x2
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
public Ounction %et(tatu#mption#ef

return arra*e
#elO::("A"(+Nm"YE"xywahua (an (an%w<
#elO::("A"(+YE"xyw(an% (an%w<
#elO::("A"(+nzNA$xywHoan "hanhw<
f

public #tatic Ounction %etAllowed(tatu#{an%eef

return arra*e
#elO::("A"(+Nm"YE"<
#elO::("A"(+YE"<
#elO::("A"(+nzNA$<
f

"h2m /o5n #au v:o arra* tron% Ounction z##ue::rule#ef


arra*ew#tatu#+idw<winw<wran%ewxy#elO::%etAllowed(tatu#{an%eeff<
"ron% z##ue::relation#ef b5n th2m /o5n #au:
wu#er#wxyarra*e#elO::YANY+YANY<w#erw<wtbl+proect+u#er+a##i%nmenteproect
+id<u#er+idfwf<
public Ounction %et#ermption#ef
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

u#er#Arra*xaHtml::li#tDataethi#yu#er#<widw<wu#ernamewf
return u#er#Arra*

M:o protecteddview#di##ued+Oorm.php chvnh l5i nh? #au:


div cla##xrowy
ophp echo OormylabelE'emodel<w#tatu#+idwf oy
ophp echo OormydropDown$i#temodel<w#tatu#+idw<model
y%et(tatu#mption#eff oy
ophp echo Oormyerroremodel<w#tatu#+idwf oy
ddivy
zzz..2 "h2m /o5n hiFn thD te't t8i Oorm
M:o protecteddview#di##ued+view.php
"ha* thS thi# ophp echo aHtml::encodeedatayt*pe+idf oy th:nh:
ophp echo aHtml::encodeedatay%et"*pe"e'teff oy
M: ophp echo aHtml::encodeedatay#tatu#+idf oy th:nh
ophp echo aHtml::encodeedatay%et(tatu#"e'teff oy
ech: 7 mKi to +ong stat's ! type th& bn ph0i !o link men' manage v:
click v:o h>nh edit tron% view li#t rhi chEn c4c tr?-n% /F t5o ra c4c id cho 2
tr?-n% n:*< nSu khCn% kSt )u\ hiJn ra # l: unknow hobc # bD b4o lii khCn%
nh9n diJn /?Rc kSt )u\ tr\ v; v> khCn% /=i t?Rn% /?Rc khli t5o f
M: /@* l: th:nh )u\:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zzz..2 "ha* /Qi chi tiSt khun% nh>n z##ue
ah3n% ta hr* v:o http:ddlocalho#tddev*iidinde'.phporxi##uedviewidx v: thI*
c4c tr5n% th4i vkn csn njm l d5n% #= 0 < < P /i;u /. c. n%ha ch3n% ta vkn
ch?a tha* /Qi v: /@* l: cCn% viJc ch3n% ta ph\i l:m
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"5i v> ch3n% ta #_ d`n% view Oile mbc /Dnh n2n ch?a cIu h>nh hSt</F /i;u
chvnh ch3n% ta c0n v:o protecteddview#di##uedview.php
ah3n% ta # #_ d`n% Uii /?Rc kS th^a t^ wid%et adetailMiew< c4i m: ch3n%
ta /r nh>n thI* tr?8c /. l Oile view proect m: ch3n% ta th2m ali#tMiew<
M: ch3n% ta c. thF nh>n thI* /o5n #au tron% protecteddview#di##uedview#.php
ophp thi#ywid%etewii.wid%et#.aDetailMieww< arra*e
wdatawxymodel<
wattribute#wxyarra*e
widw<
wnamew<
wde#criptionw<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wproect+idw<
wt*pe+idw<
w#tatu#+idw<
wowner+idw<
wre)ue#ter+idw<
wcreate+timew<
wcreate+u#er+idw<
wupdate+timew<
wupdate+u#er+idw<
f<
ff oy
"5i /@* ch3n% ta thiSt l9p dA liJu cho model adetail Miew wid%et t^ z##ue
Yodel cla## /?Rc khli t5o e/. l: khli t5o /bc biJt khi ch3n% ta mu=n hiFn thD
chi tiStf v: #au khi thiSt l9p danh #4ch cKa ph0n t_ khi model /?Rc khli t5o
/F hiFn thD tron% render chi tiSt view#< m6t ph0n t_ c. thF /bc tr?n% %i=n%
nh? chuii /Dnh d5n% : t2n : thu6c tVnh: nhrn< v8i c\ hai /=i t?Hn% type v:
label hobc arra*< tron% tr?-n% hRp n:* ch3n% ta chv #_ d`n% t2n cKa ph0n t_.
NSu ch3n% ta thI* m\n% ph0n t_ n:*< ch3n% ta c. thF cu#tom viJc hiFn thD t=t
hHn t^ viJc /Dnh n%ha l5i c4c *Su t=. ah3n% ta # tiSn h:nh kS ho5ch tron%
thLc /Hn /bc biJt cKa model cla## method z##ue::%et"*pe"e'tef v:
z##ue::%et(tatu#"e'tef /?Rc #_ d`n% hiFn thD %i4 trD te't cho type v: stat's
field.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Hr* tha* /Qi tron% adetailMiew nh? #au:
ophp thi#ywid%etewii.wid%et#.aDetailMieww< arra*e
wdatawxymodel<
wattribute#wxyarra*e
widw<
wnamew<
wde#criptionw<
array(
'name'=>'type_id',
'vaIue'=>CHtmI::encode($modeI->getTypeText())
),
array(
'name'=>'status_id',
'vaIue'=>CHtmI::encode($modeI->getStatusText())
),
wowner+idw<
wre)ue#ter+idw<
f<
ff oy
(au khi thLc hiJn 'on% v:o http:ddlocalho#tddev*iidinde'.phpo
rxi##uedviewidx 'em kSt )u\:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zzz..2 HiFn thD t2n owner ! re/'ester
"^ \nh tr2n b5n cn% c. thF nh>n thI*< b5n chv m8i nh>n thI* id cKa u#er t5o
l2n hobc u#er *2u c0u n.< cCn% viJc cKa b5n b@* %i- l: tha* thS n. bjn% t2n
cKa u#er tron% b\n% u#er.
(_ d`n% Wuan hJ A{
(au khi z##ue# v: u#er# /?Rc tr>nh ditn l5i %i=n% nh? b\n% dA liJu v: m=i
)uan hJ bli kh.a n%o:i eOorei%n ke*f ch3n% ta c. thF tru* c9p /?Rc v:o
owner ! re/'ester u#ername t^ model tron% view Oile nh- cCn% c` m5nh
cKa Yii A{ < b@* %i- cCn% viJc cKa ch3n% ta l: /Dnh n%ha l5i m=i )uan hJ
tron% z##ue::relation#ef
'owner' => array(seIf::BELONGS_TO, 'User', 'owner_id'),
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
// on ny s dng Iin kt thuc v,bn thn mt user nm trong bng
user
'requester' => array(seIf::BELONGS_TO, 'User', 'requester_id'),
// on ny s dng Iin kt thuc v,ngi yu cu nm trong bng
user
1o5n n:* /r /?Rc khli t5o rhi v: b5n khCn% c0n ph\i v:o /. /i;u chvnh l5i<
"ha* v:o /. b5n c0n )ua* l5i Oile protecteddview#di##ue#dview.php /F l:m n=t
#L tha* /Qi
ophp thi#ywid%etewii.wid%et#.aDetailMieww< arra*e
wdatawxymodel<
wattribute#wxyarra*e
widw<
wnamew<
wde#criptionw<
arra*e
wnamewxywt*pe+idw<
wvaluewxyaHtml::encodeemodely%et"*pe"e'teff
f<
arra*e
wnamewxyw#tatu#+idw<
wvaluewxyaHtml::encodeemodely%et(tatu#"e'teff
f<
array(
'name'=>'owner_id',
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
'vaIue'=>isset($modeI->owner)?CHtmI::encode($modeI->owner-
>username):"unknown"
),
array(
'name'=>'requester_id',
'vaIue'=>isset($modeI->requester)?CHtmI::encode($modeI-
>requester->username):"unknown" ),
f<
ff oy
M: /@* l: kSt )u\: http:ddlocalho#tddev*iidinde'.phporxi##uedviewidx
zzz..2& (L tha* /Qi cu=i c]n% v8i navi%ation
Yl protecteddview#di##uedview.php v: tha* /Qi menu conOi% nh? #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
thi#ymenuxarra*e
array('IabeI'=>'List Issues', 'urI'=>
array('index', 'pid'=>$modeI->project->id)),
array('IabeI'=>'Create Issue', 'urI'=>
array('create', 'pid'=>$modeI->project->id)),
arra*ewlabelwxywpdate z##uew< wurlwxy
arra*ewupdatew< widwxymodelyidff<
arra*ewlabelwxywDelete z##uew< wurlwxyww< wlinkmption#wxy
arra*ew#ubmitwxyarra*ewdeletew<widwxymodelyidf<
wconOirmwxywAre *ou #ure *ou want to delete thi# itemowff<
array('IabeI'=>'Manage Issues', 'urI'=>
array('admin', 'pid'=>$modeI->project->id)),
f
"iSp theo ch3n% ta c0n tha* /Qi z##ueaontroller::Oilter#ef
public Ounction Oilter#ef

return arra*e
wacce##aontrolw< dd perOorm acce## control Oor a{D operation#
'projectContext + create index admin', //perform a check to
//ensure vaIid project context
f

"iSp theo ta chvnh l5i z##ueaontrolller:actionznde'ef:


public Ounction actionznde'ef
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

$dataProvider=new CActiveDataProvider('Issue', array(


'criteria'=>array(
'condition'=>'project_id=:projectId',
'params'=>array(':projectId'=>$this->_project->id),
),
));
thi#yrenderewinde'w<arra*ewdataProviderwxydataProvider<ff

"iSp theo b5n c0n v:o protecteddview#di##uedadmin.php


(_ d`n% kSt )u\ cKa model cla## z##ue#::#earchef method /?Rc cun% cIp
danh #4ch i##ue#.c5n v:o z##ueaontroller::actionAdminef:
public Ounction actionAdminef

modelxnew z##ueew#earchwf
iOei##ete+uE"wz##uewff
modelyattribute#x+uE"wz##uew
$modeI->project_id = $this->_project->id;
thi#yrenderewadminw<arra*ewmodelwxymodelff

"iSp theo b5n tha* /Qi tron% z##ue#::#earchef


public Ounction #earchef

dd Warnin%: Plea#e modiO* the Oollowin% code to remove attribute# that


L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
dd #hould not be #earched.
criteriaxnew aDbariteria
criteriaycompareewidw<thi#yidf
criteriaycompareewnamew<thi#yname<truef
criteriaycompareewde#criptionw<thi#yde#cription<truef
criteriaycompareewt*pe+idw<thi#yt*pe+idf
criteriaycompareew#tatu#+idw<thi#y#tatu#+idf
criteriaycompareewowner+idw<thi#yowner+idf
criteriaycompareewre)ue#ter+idw<thi#yre)ue#ter+idf
criteriaycompareewcreate+timew<thi#ycreate+time<truef
criteriaycompareewcreate+u#er+idw<thi#ycreate+u#er+idf
criteriaycompareewupdate+timew<thi#yupdate+time<truef
criteriaycompareewupdate+u#er+idw<thi#yupdate+u#er+idf
$criteria->condition='project_id=:projectID';
$criteria->params=array(':projectID'=>$this->project_id);
return new aActiveDataProvidere%et+cla##ethi#f< arra*e
wcriteriawxycriteria<
ff

/@* ch3n% ta v^a '.a criteriaycompareef #_ d`n% proect+id v8i criteria


yconditionef /F #o #4nh v8i proect+id tron% proect conte't.v8i nhAn% tha* /Qi
n:* danh #4ch i##ue# thLc #L bD chi ph=i bli proect
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ah?Hn% : Wu\n lT n%?-i d]n% v: )u4
tr>nh '4c thLc
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
e#er Yana%ement and Authenticationf
~S ho5ch 'a hHn
~hi ch3n% ta #_ d`n% dsn% lJnh *ii /F t5o ra Zn% d`n% n:*< n. cho phNp
ch3n% ta /7n% nh9p bli 2 account l: demo demo v: adminadmin <ch3n% ta
c. thF thLc hiJn l5i )u4 tr>nh '4c thLc t^ a{D v: ta c. kS ho5ch #au:
"5o controller cla## cho phNp:
. "5o m8i u#er
2. $Ec danh #4ch u#er thn t5i tron% c#dl
!. a9p nh9tdchvnh #_a b\n %hi
. X.a u#er thn t5i
"5o view Oile v: thLc hiJn tr>nh b:* :
. HiFn thD Oorm cho phNp n%?-i d]n% t5o m8i
2. HiFn thD danh #4ch u#er thn t5i
!. HiFn thD Oorm cho phNp chvnh #_a u#er
. "h2m hobc '.a u#er
ahvnh l5i )u4 tr>nh '4c thLc #_ d`n% databa#e /F kiFm tra )u4 tr>nh
lo%in
zM. #er a{D
"r?8c ti2n b5n v:o uii : http:ddlocalho#tddev*iidinde'.phpo
rx%iiddeOaultdinde'
ahEn ModeI Generated v: /i;n %i=n% nh? h>nh #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
alick Preview y uenerated.
(au khi ho:n th:nh b5n v:o protecteddmodel#d /F kiFm tra Oile m8i t5o
b5n # thI* #er.php
"iSp theo b5n v:o uii chEn CRUD Generator < t5i Yodel cla## : /i;n
v:o : }ser < t5i aontroller zD /i;n v:o : }ser, #au /. chEn preview y
%enerated.
"iSp theo b5n v:o http:ddlocalho#tddev*iidinde'.phporxu#er kiFm tra b5n
/r thI* 2 u#er /?Rc t5o #n ban /0u
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

ah3n% ta cn% c. thF t5o m8i u#er n%a* b@* %i- t^ link menu create
user: http:ddlocalho#tddev*iidinde'.phporxu#erdcreate
NSu hiJn t5i b5n khCn% /7n% nh9p b5n # c. router lo%in #_ d`n% v8i 2
t:i kho\n l: demoddemo v: admindadmin.
I?.! 2h=nh ph@n Aehavior
cehavior tron% Yii cla## /?Rc thLc thi %iao diJn cKa zbehavior< v:
ph?Hn% thZc cKa n. c. thF kS th^a t^ h:m th:nh ph0n bli viJc kVch
ho5t component. $T do ch3n% ta #_ d`n% component n:* bli v> #er<
Proect< z##ue cn% c. c]n% cIu tr3c< ta tha* thS h:m behavior c. #n
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
v: kVch ho5t n. t^ model cla##<n. cho phNp ch3n% ta thiSt l9p c4c
tr?-n% dA liJu tron% A{ cla## .
"hLc tS tron% th? viJn Uii< %.i nh[ tron% Yii Oramework /r thn t5i
behavior cho viJc c9p nh9t th-i %ian create+time v: update+time.
cehavior n:* c. t2n atime#tampcehavior.
c5n v:o protecteddmodel#d#er.php th2m h:m #au:
public Ounction behavior#ef

return arra*ewa"ime#tampcehaviorw xy arra*e


wcla##w xy wii.behavior#.a"ime#tampcehaviorw<
wcreateAttributew xy wcreate+timew<
wupdateAttributew xy wupdate+timew<
w#etpdatemnareatew xy true<
f<
f


/@* ch3n% ta v^a kVch ho5t th? viJn Uii atime#tampcehavior t8i
cla## #er model. ah3n% ta c. thF nh>n thI* #L kh4c biJt cKa 2 ph0n
t_ createYtime v: 'pdateYtime /?Rc cIu h>nh tron% behavior /F thiSt
l9p th-i %ian cho mii b\n %hi /?Rc t5o hobc c9p nh9t.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
(au n:* khi b5n /7n% nh9p v:o u#er m8i th> b5n # thI* 2 ph0n t_ #
/?Rc tL c9p nh9t:
1i;u n:* th9t tu*Jt ch3n% ta c0n lbp l5i n. tron% model cla##< tu* nhi2n
ch3n% ta c. thF thLc hiJn kS ho5ch kh4c t=t hHn nAa < /. l: tiSn h:nh
#_ d`n% Active record ekVch ho5t b\n %hif bli #L kiJn before[ae
Ntr2Kc khi l2'O, v: thiSt l9p nhAn% %> c0n thiSt cho tr?-n% dA liJu. v:
ch3n% ta c. thF thLc hiJn c4c lJnh kSt n=i v8i A{ model cla##.c5n #
lLa chEn kS ho5ch n:* t=t hHn cho Zn% d`n% Yii c Ka b5n.
1F l:m viJc ch3n% ta c0n t5o m8i A{ model cla##< tr?8c ti2n '.a b[
h:m behaviors() -hBng ta v*a t3o trong Cser.php mo%el.
"iSp theo b5n v:o protecteddmodel#d b5n t5o Oile
DevYiiActiveRecord.php vi ni dung sau:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp
ab#tract cla## DevYiiActive{ecord e'tend# aActive{ecord

protected Ounction beOore(aveef

iOenull xx Yii::appefyu#erf
idxYii::appefyu#eryid
el#e
idx
iOethi#yi#New{ecordf
thi#ycreate+u#er+idxid
thi#yupdate+u#er+idxid
return parent::beOore(aveef

public Ounction behavior#ef

return arra*e
wa"ime#tampcehaviorw xy arra*e
wcla##w xy wii.behavior#.a"ime#tampcehaviorw<
wcreateAttributew xy wcreate+timew<
wupdateAttributew xy wupdate+timew<
w#etpdatemnareatew xy true<
f<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f

"5i /@* ch3n% ta /r t\i ph?Hn% thZc aactive{ecord::beOore(aveef< /@*


l: m6t tron% nhi;u even m: aactive{ecord '@* dLn% /F cho phNp
cu#tom tron% )u4 tr>nh thLc hiJn proect. eahi tiSt vui lsn% v:o )u*Fn
Yii cookbook nhN tCi /r dDch rhi v:o Yiivn.com m: t>m tron% ph0n
ebookf< c. hai ph?Hn% thZc hi trR cho phNp ch3n% ta chu*Fn dA liJu
b\n %hi tr?8c khi ch3n% ta thLc hiJn nhAn% %> c0n thiSt tr?8c khi hobc
#au khi kVch ho5t b\n %hi %hm 2 h:m : before[aeNO ! after[aeNO
NTr2Kc khi l2' ! sa' khi l2'O.
"ron% tr?-n% hRp n:* ch3n% ta )u*St /Dnh l?u t5m 2 biSn time tr?8c
khi tiSn h:nh l?u v:o a(D$.
ah3n% ta )u*St /Dnh viJc khCn% chv %i\i )u*St viJc t5o m8i b\n %hi ha*
c9p nh9t b\n %hi #_ d`n% thu6c tVnh thi#yi#New{ecord v: thiSt l9p
dA liJu. ~hi ch3n% ta chBc chBn khli t5o tr\ v; parent**before[aeNO l!
ch:ng ta "4 thGc hi(n +ong mPi th? "25c l!m trong h!m. Ch:ng ta
thGc hi(n Dii**appNOM'ser " + l7 nh<ng khVi to n@i ngo!i ?ng d>ng
hn ch. tr'y c3p.
ah3n% ta c. thF di chu*Fn behavior#ef method /F l:m cla## cH b\n cho
c4c A{ model kS th^a n. v8i viJc kVch ho5t behavior#
"a th_ /i;u n:*< ch3n% ta c0n chvnh l5i mii ! A{ cla## model l: Proect<
u#er< z##ue /F kS th^a h:m khli t5o t^ aactive{ecord. MV d`:
cla## #er e'tend# aActive{ecord

P
ah3n% ta c0n tha* bjn%
cla## #er e'tend# DevYiiActive{ecord

P
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
$:m t?Hn% tL v8i cla## Proect v: z##ue model cla##.
"iSp theo ch3n% ta c0n v:o protecteddview#dproectd+Oorm.php v:
protecteddview#di##ued+Oorm.php v: protecteddview#du#erd+Oorm.php
t>m v: '.a nhAn% /o5n #au:
div cla##xrowy
ophp echo OormylabelE'emodel<wcreate+timewf oy
ophp echo Oormyte'tnieldemodel<wcreate+timewf oy
ophp echo Oormyerroremodel<wcreate+timewf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wcreate+u#er+idwf oy
ophp echo Oormyte'tnieldemodel<wcreate+u#er+idwf oy
ophp echo Oormyerroremodel<wcreate+u#er+idwf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wupdate+timewf oy
ophp echo Oormyte'tnieldemodel<wupdate+timewf oy
ophp echo Oormyerroremodel<wupdate+timewf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wupdate+u#er+idwf oy
ophp echo Oormyte'tnieldemodel<wupdate+u#er+idwf oy
ophp echo Oormyerroremodel<wupdate+u#er+idwf oy
ddivy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
M: t^ Oorm u#er: protecteddview#du#erd+Oorm.php ta '.a th2m ph0n t_
la#t+lo%in+time
div cla##xrowy
ophp echo OormylabelE'emodel<wla#t+lo%in+timewf oy
ophp echo Oormyte'tnieldemodel<wla#t+lo%in+timewf oy
ophp echo Oormyerroremodel<wla#t+lo%in+timewf oy
ddivy
"iSp theo v:o model cKa ! cla## z##ue< Proect< #er# tr2n '.a c4c
ph0n t_ tr2n tron% h:m rule# v: h:m attribute#label v: h:m #earch.
"iSp theo ta v:o model #er.php :: rule#ef v: tha* /Qi nh? #au:
public Ounction rule#ef

return arra*e
arra*ewemailw< wre)uiredwf<
arra*ewemail< u#ername< pa##wordw< wlen%thw< wma'wxy2<
arra*ewemail< u#ernamew< wuni)uewf<
arra*ewemailw< wemailwf<
dd "he Oollowin% rule i# u#ed b* #earchef.
dd Plea#e remove tho#e attribute# that #hould not be #earched.
arra*ewid< email< u#ername< pa##wordw< w#aOew<wonwxyw#earchwf<
f

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zM.! "h2m tr?-n% uG l5i pa##word
ah3n% ta khai b4o tr2n /0u cla## #er.php model tron%
protecteddmodel#d#er.php:
public pa##word+repeat
"h2m #er::rule#ef dsn% #au v:o m\n% arra*:
arra*ewpa##wordw< wcomparew< wcompareAttributewxywpa##word+repeatwf<
arra*ewpa##word+repeatw< w#aOewf<
M: tha* /Qi
arra*ewemail< u#ername< pa##word< pa##word+repeatw< wre)uiredwf<
"h2m /o5n #au v:o protecteddview#du#erd+Oorm.php #au pa##word Oield:
div cla##xrowy
ophp echo OormylabelE'emodel<wpa##word+repeatwf oy
ophp
echo Oormypa##wordnieldemodel<wpa##word+repeatw<
arra*ew#iewxy0<wma'len%thwxy2ff
oy
ophp echo Oormyerroremodel<wpa##word+repeatwf oy
ddivy
M: /@* l: kSt )u\:
http:ddlocalho#tddev*iidinde'.phporxu#erdcreate
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zM. Ha#h ec7mf m9t kh}u
$0n tha* /Qi cu=i c]n% ch3n% ta c. thF l:m tr?8c khi di chu*Fn t8i viJc
t5o m8i u#er /. l: t5o h:m b7m cho m9t kh}u cKa n%?-i d]n%< khi
ch3n% ta nhNt n. v:o databa#e.
ah3n% ta # th2m lo%ic v:o #er.php nh- viJc /i;u khiFn
aactive{ecord cho phNp ch3n% ta cu#tom mbc /Dnh kVch ho5t b\n %hi <
/i;u n:* /?Rc t\i v8i ph?Hn% thZc after`alidateNO #au khi '4c thLc
c4c tr?-n% dA liJu /0u v:o hRp lJ<
M:o model #er.php protecteddmodel#d#er.php th2m /o5n #au d?8i
cla##:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
protected Ounction aOterMalidateef

parent::aOterMalidateef
iOethi#yha#Error#eff
thi#ypa##word x thi#yha#hPa##wordethi#ypa##wordf

d||
| uenerate# the pa##word ha#h.
| ,param #trin% pa##word
| ,return #trin% ha#h
|d
public Ounction ha#hPa##wordepa##wordf

return mdepa##wordf

c5n hr* t5o m8i t:i kho\n t2n admin< pa##word admin v: # thI* kSt
)u\ #au: e c?8c n:* m: khCn% t5o th> /o5n #au kh[i ph\i v:o hJ th=n%
luCn +f
http:ddlocalho#tddev*iidinde'.phporxu#erdviewidx!
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
zM. X4c thLc #er kSt n=i t8i Databa#e
Nh? ch3n% ta biSt< Oorm /7n% nh9p cH b\n n%?-i d]n% '4c thLc tru*
c9p /?Rc t5o /Hn %i\n bli lJnh Yii khi t5o m8i Zn% d`n%< ah3n% ta tru*
nh9p v:o web#ite nh- hai t:i kho\n l: demoddemo v: admindadmin< cCn% viJc
cKa ch3n% ta b@* %i- l: chu*Fn to:n b6 t:i kho\n '4c thLc tron% a(D$ m:
b5n v^a t5o v:o )u4 tr>nh '4c thLc hJ th=n% Yii.
ui8i thiJu Yii authentication model eX4c thLc mC h>nh a(D$f
"run% t@m t8i '4c thLc u#er cKa Yii l: m6t th:nh ph0n Zn% d`n% %Ei l:
u#er< n. l: m6t /=i t?Rn% /?Rc thLc thi t^ %iao diJn zweb#er< cla## /bc biJt
n:* /?Rc #_ d`n% bli viJc thLc thi aweb#er< th:nh ph0n u#er n:* l: thCn%
tin tIt c\ /Dnh danh u#er hiJn t5i c. tron% Zn% d`n%. "h:nh ph0n n:* /?Rc
cIu h>nh cho ch3n% ta nh? m6t ph0n tL /6n% tron% hJ th=n% khi Zn% d`n%
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
/?Rc t5o bli *ii tool< 1F cIu h>nh n. b5n v:o protecteddconOi%dmain.php v:
nh>n /o5n n:*:
wu#erwxyarra*e
dd enable cookieba#ed authentication
wallowAuto$o%inwxytrue<
f<
$3c n:* n. /?Rc cIu h>nh nh? m6t th:nh ph0n Zn% d`n%<v8i t2n u#er<
ch3n% ta c. thF tru* c9p bIt kq nHi n:o tron% Zn% d`n% bli viJc #_ d`n%:
Yii::appefyu#er.
Yii tL /6n% /Dnh n%ha thLc thF thLc hiJn viJc '4c thLc cH b\n< n.
/?Rc %Ei l: identity cIass v: n. /?Rc thF hiJn bli zu#eridentit* interOace<
vai trs chVnh cKa cla## n:* l: '4c thLc v: ph@n )u*;n u#er.
c5n ml protecteddcomponent#d#erzdentit*.php /@* chVnh l: n. v: b5n
c. thF nh>n #H )ua n. :
ophp
cla## #erzdentit* e'tend# a#erzdentit*

public Ounction authenticateef

u#er#xarra*e
wdemowxywdemow<
wadminwxywadminw<
f
iOei##eteu#er#thi#yu#ernameff
thi#yerroraodex#elO::E{{m{+(E{NAYE+zNMA$zD
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
el#eiOeu#er#thi#yu#ernamexxthi#ypa##wordf
thi#yerroraodex#elO::E{{m{+PA((Wm{D+zNMA$zD
el#e
thi#yerroraodex#elO::E{{m{+NmNE
return thi#yerroraode


"o:n b6 cCn% viJc cKa n. njm tron% Ounction authenticateef v: b5n c.
thF nh>n thI* m\n% chZa 2 ph0n t_ demoddemo v: admindadmin chVnh l: 2
u#er ch3n% ta c. thF /7n% nh9p v:o web#ite
1F hiFu rG hHn v; )u* tr>nh cKa n. b5n hr* 'em #H /h #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Wu* tr>nh bBt /0u v8i viJc thiSt l9p cla## tron% Oorm model
$o%innorm.php eprotecteddmodel#d$o%innorm.php f < v: khi Oorm /?Rc #ubmit<
n. # #_ d`n% h:m $o%innormyvalidateef /F '4c thLc c4i input t^ Oorm
nh9p v:o /?Rc /Dnh n%ha tron% rule#ef
public Ounction rule#ef

return arra*e
arra*ewu#ername< pa##wordw< wre)uiredwf<
arra*ewrememberYew< wbooleanwf<
arra*ewpa##wordw< wauthenticatewf<
f

~St th3c viJc '4c thLc chVnh l: viJc #_ d`n% cu#tom method
authenticateef:
public Ounction authenticateeattribute<param#f

thi#y+identit*xnew #erzdentit*ethi#yu#ername<thi#ypa##wordf
iOethi#y+identit*yauthenticateeff
thi#yaddErrorewpa##wordw<wzncorrect u#ername or pa##word.wf

(au khi thLc hiJn 'on% viJc '4c thLc cKa n. n. %Ei t8i h:m khli t5o
/Dnh danh tron% protecteddcomponent#d#erzdentit*.php v: %Ei t8i h:m
authenticateef
1@* chVnh l: l3c u#er /?Rc ph@n )u*;n v: '4c thLc u#er tron% hJ
th=n%<mbc /Dnh /an% c. 2 u#er l: demo v: admin n. # tr\ v; true.(au khi
th:nh cCn% n. # trl v; (iteaontroller::$o%inef method<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
public Ounction lo%inef

iOethi#y+identit*xxxnullf

thi#y+identit*xnew #erzdentit*ethi#yu#ername<thi#ypa##wordf
thi#y+identit*yauthenticateef

iOethi#y+identit*yerroraodexxx#erzdentit*::E{{m{+NmNEf

durationxthi#yrememberYe o !00|2|!0 : 0 dd !0 da*#


Yii::appefyu#erylo%inethi#y+identit*<durationf
return true

el#e
return Oal#e

(au khi ho:n th:nh n. tr\ v; true hobc Oal#e v: chu*Fn h?8n% t8i {$
/?Rc /Dnh danh #n.
zM.. "ha* /Qi thLc thi '4c nh9n
c@* %i- ch3n% ta /i v:o ph0n tha* /Qi #ao cho n. nh9n t:i kho\n m9t
kh}u n%?-i d]n% tron% b\n% tbl+u#er.
10u ti2n hr* v:o protecteddcomponent#d#erzdentit*.php v: tha* /Qi to:n b6
n6i dun% cKa n. th:nh nh? #au:
ophp
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
cla## #erzdentit* e'tend# a#erzdentit*

private +id
public Ounction authenticateef

u#erx#er::modelefyOindew$mWE{eu#ernamefxow<
arra*e#trtolowerethi#yu#ernamefff
iOeu#erxxxnullf
thi#yerroraodex#elO::E{{m{+(E{NAYE+zNMA$zD
el#e iOeu#eryvalidatePa##wordethi#ypa##wordff
thi#yerroraodex#elO::E{{m{+PA((Wm{D+zNMA$zD
el#e

thi#y+idxu#eryid
thi#yu#ernamexu#eryu#ername
thi#y#et(tateewla#t$o%inw< dateemddd* %:i A<
#trtotimeeu#eryla#t+lo%in+timefff
u#ery#aveAttribute#earra*ewla#t+lo%in+timewxydateeYmd H:i:#<
timeeff<
ff
thi#yerroraodex#elO::E{{m{+NmNE

return thi#yerroraodexx#elO::E{{m{+NmNE

public Ounction %etzdef

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
return thi#y+id

M: #au /. ch3n% ta /i t8i protecteddmodel#d#er.php l:m pa##word validation


< ch3n% ta cn% c0n th2m method cho #er model :
public Ounction validatePa##wordepa##wordf

return thi#yha#hPa##wordepa##wordfxxxthi#ypa##word

zM. kS th^a ph0n t_ #er


M8i viJc thiSt l9p tron% #erzdentit* :
thi#y#et(tateewla#t$o%inw< dateemddd* %:i A<
timeeu#eryla#t+lo%in+timefff
u#ery#aveAttribute#earra*e
wla#t+lo%in+timewxydateeYmd H:i:#< timeeff<ff
(au n:* ta c. thF d]n% : Yii::appefyu#eryla#t$o%in
zM.& "r>nh b:* la#t lo%in tron% home pa%e
M:o protecteddview#d#itedinde'.php th2m v:o /o5n #au:
hyWelcome to iy
ophp echo aHtml::encodeeYii::appefynamef oy
diydhy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp iOeYii::appefyu#eryi#uue#tf:oy
py
You la#t lo%%ed in on ophp echo Yii::appefyu#eryla#t$o%in oy.
dpy
ophp endiOoy
M:o http:ddlocalho#tddev*iidinde'.phporx#itedlo%in /7n% nh9p v8i t:i
kho\n m8i t5o admindadmin< /o5n tr?8c ai ch?a t5o th> kh[i ph\i hEc tiSp< /i
n%K luCn /i.
1@* l: kSt )u\:
aHNu : 1i;u khiFn tru* c9p
n%?-i d]n%
eAa #er Acce## aontrolf
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
M. aIu h>nh )u\n lT tru* c9p
M:o protecteddconOi%dmain.php v: th2m /o5n #au v:o m\n%
component:
wcomponent#wxyarra*e
P
wauthYana%erwxyarra*e
wcla##wxywaDbAuthYana%erw<
wconnectionzDwxywdbw<
f<
M.2 "5o b\n% c#dl {cAa
M:o phpm*admin v: t5o ra c4c b\n% #au v8i c4c )uer* :
To bng auth_item
create table tbl+auth+itemename varchare2f not null<t*pe intef not
null<de#cription te't<birule te't<data te't<primar*
ke*enameffEn%inexznnoDc
To bng auth_item_chiId
create table tbl+auth+item+childeparent varchare2f not null<
child varchare2f not null<P{zYA{Y ~EY eparent<childffENuzNExznnoDc
"5o kh.a n%o:i tham chiSu v8i Oield name
alter table tbl+auth+item+child add con#traint Ok+auth+item+child+parent
Oorei%n ke*eparentf reOerence# tbl+auth+itemenamef on update ca#cade on
delete ca#cade
alter table tbl+auth+item+child add con#traint Ok+auth+item+child+child Oorei%n
ke*echildf reOerence# tbl+auth+itemenamef on update ca#cade on delete
ca#cade
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
To bng auth assignment
create table tbl+auth+a##i%nmenteitemname varchare2f not null<u#erid
intef not null<birule te't<data te't<primar* ke*eitemname<u#eridff
en%inexinnodb
To kha ngoi Iin kt bng vi fieId itemname
alter table tbl+auth+a##i%nment add Oorei%n ke*eitemnamef reOerence#
tbl+auth+itemenamef on update ca#cade on delete ca#cade
To kha ngoi Iin kt vi userid
alter table tbl+auth+a##i%nment add con#traint Ok+auth+a##i%nment+u#erid
Oorei%n ke*eu#eridf reOerence# tbl+u#ereidf on update ca#cade on delete
ca#cade
"iSp theo v:o protecteddconOi%dmain.php v: #_a l5i n6i dun% tron% component
arra*:
wcomponent#wxyarra*e
wauthYana%erwxyarra*e
wcla##wxywaDbAuthYana%erw<
wconnectionzDwxywdbw<
witem"ablew xywtbl+auth+itemw<
witemahild"ablew xywtbl+auth+item+childw<
wa##i%nment"ablew xywtbl+auth+a##i%nmentw<
f<
M.! "5o biFu /h /Dnh danh {cAa
(au khi t5o c4c b\n% c#dl rhi ch3n% ta c0n th2m c4c vai trs erole#f v: ph0n
)u*;n epermi##ionf.ah3n% ta # #_ d`n% APz /?Rc cun% cIp bli
a'thmanager component. c@* %i- ch3n% ta hr* nh>n 'em )ua /Dnh n%ha c4c
b\n% v8i #H /h #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
/@* ta c. thF nh>n thI* cIp cao nhIt chVnh l: b\n th@n ch3n% ta eadminf v8i
viJc t5o u#er< c9p nh9t u#er< '.a u#er< t5o proect< c9p nh9t proect v: '.a
proect< n. /?Rc ph@n )u*;n \nh h?ln% t^ c\ hai member eth:nh vi2nf v:
readereN%?-i /Ecf
."h:nh vi2n chv /?Rc phNp c9p nh9t #_a '.a dA liJu cKa b\n th@n v: c4c
)u*;n lRi cKa n%?-i /Ec /. l: 'em th:nh vi2n< /Ec proect< /Ec i##ue.
N%?-i /Ec eanon*mou#f l: n%?-i kh4ch vrn% lai tru* c9p v:o web hE /?Rc
phNp /Ec thCn% tin u#er< /6c thCn% tin c Ka proect v: i##ue
M. "hLc thi proect A{ method m8i
"r?8c ti2n v:o phpm*admin th2m cCt m8i cho tbl+proect+u#er+a##i%nment
alter table tbl+proect+u#er+a##i%nment add column role varchare2f not null
M:o protecteddmodel#dProect.php v: th2m v:o h:m #au:
public Ounction a##i%n#ereu#erzd< rolef
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

command x Yii::appefydbycreateaommandef
commandyin#ertewtbl+proect+u#er+a##i%nmentw< arra*e
wrolewxyrole<
wu#er+idwxyu#erzd<
wproect+idwxythi#yid<
ff

"h2m h:m '.a #er:


public Ounction remove#ereu#erzdf

command x Yii::appefydbycreateaommandef
commandydeletee
wtbl+proect+u#er+a##i%nmentw<
wu#er+idx:u#erzd AND proect+idx:proectzdw<
arra*ew:u#erzdwxyu#erzd<w:proectzdwxythi#yidf
f

M. "h2m #er t8i Proect


. "h2m h:m tnh e#taticf %et#er{olemption#ef t^ Proect A{ tr\
v; danh #4ch hRp lJ v8i role option #_ d`n% %et{ole#ef method
tron% mana%er. ah3n% ta # cho u#er lLa chEn dropdown
2. "h2m h:m m8i %Ei l: i##erznProecteu#erf t^ Proect A{
)u*St /Dnh u#er c. li2n )uan t8i proect
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ah3n% ta #_ d`n% rule# validation /F th2m u#er t8i proect
!. "h2m Oorm m8i t2n l: Proect#ernorm kS th^a t^ aOormYodel
/F t5o input Oorm model. "h2m v:o Oorm n:* ! ph0n t_ l: role<proect<
u#er.
. "h2m Oile m8i tron% protecteddview#dproect %Ei l: addu#er.php
/F hiFn thD Oorm m8i t5o t^ u#er t8i proect.
. "h2m controller action method m8i t2n actionAddu#eref t^
Proectaontroller.php v: /i;u chvnh c\ acce##{ule#ef ph?Hn% thZc tru*
c9p /Dnh danh t^ u#er.
c@* %i- ch3n% ta bBt ta* v:o cCn% viJc
M.. ahvnh l5i Proect model cla##
"^ Proect Yodel ch3n% ta t5o th2m 2 h:m m8i:
d|| {eturn# an arra* oO available role# in which a u#er can be placed
|when bein% added to a proect
|d
public #tatic Ounction %et#er{olemption#ef

return aHtml::li#tDataeYii::appefyauthYana%ery%et{ole#ef< wnamew<


wnamewf

d|
| Determine# whether or not a u#er i# alread* part oO a proect
|d
public Ounction i##erznProecteu#erf

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
#)l x (E$Ea" u#er+id n{mY tbl+proect+u#er+a##i%nment WHE{E
proect+idx:proectzd AND u#er+idx:u#erzd
command x Yii::appefydbycreateaommande#)lf
commandybindMaluee:proectzd< thi#yid< PDm::PA{AY+zN"f
commandybindMaluee:u#erzd< u#eryid< PDm::PA{AY+zN"f
return commandye'ecuteefxx

M..2 "h2m model norm cla##


M:o protecteddmode# t5o m8i Oile t2n Proect#ernorm.php v8i n6i dun% #au:
ophp
cla## Proect#ernorm e'tend# anormYodel

public u#ername
public role
public proect
private +u#er
public Ounction rule#ef

return arra*e
arra*ewu#ername< rolew< wre)uiredwf<
arra*ewu#ernamew< we'i#tw< wcla##Namewxyw#erwf<
arra*ewu#ernamew< wveriO*wf<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f

public Ounction veriO*eattribute<param#f

iOethi#yha#Error#eff

u#er x #er::modelefyOindc*Attribute#
earra*ewu#ernamewxythi#yu#ernameff
iOethi#yproectyi##erznProecteu#erff

thi#yaddErrorewu#ernamew<w"hi# u#er ha# alread*


been added to the proect.wf
el#e

thi#y+u#er x u#er

public Ounction a##i%nef

iOethi#y+u#er in#tanceoO #erf

thi#yproectya##i%n#erethi#y+u#eryid< thi#yrolef
auth x Yii::appefyauthYana%er
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
bi{ulexwreturn i##eteparam#proectf param#proect
yallowaurrent#erew.thi#yrole.wfw
authya##i%nethi#yrole<thi#y+u#eryid< bi{ulef
return true

el#e

thi#yaddErrorewu#ernamew<wError when attemptin% to a##i%n thi#


u#er to the proect.wf
return Oal#e

public Ounction create#ername$i#tef

#)l x (E$Ea" u#ername n{mY tbl+u#er


command x Yii::appefydbycreateaommande#)lf
row# x commandy)uer*Allef
u#ername# x arra*ef
Ooreacherow# a# rowf

u#ername#xrowwu#ernamew

return u#ername#

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

M..! "h2m action t8i Proectaontroller


public Ounction actionAddu#ereidf

proect x thi#yloadYodeleidf
iOeYii::appefyu#erycheckAcce##ewcreate#erw<
arra*ewproectwxyproectfff

throw new aHttpE'ceptione0!<wYou are not authoried to perOorm


thi# action.wf

Oormxnew Proect#ernorm
iOei##ete+Pm("wProect#ernormwff

Oormyattribute#x+Pm("wProect#ernormw
Oormyproect x proect
iOeOormyvalidateeff

iOeOormya##i%neff

Yii::appefyu#ery#etnla#hew#ucce##w<Oormyu#ername .
ha# been added to the proect. f
Oormyun#etAttribute#ef
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
OormyclearError#ef

Oormyproect x proect
thi#yrenderewaddu#erw<arra*ewmodelwxyOormff

ahvnh l5i acce##{ule#ef Ounction:


public Ounction acce##{ule#ef

return arra*e
arra*ewalloww< dd allow all u#er# to perOorm winde'w and wvieww action#
wcontroller#wxyarra*ewi##uew<wproectw<wu#erwf<
waction#wxyarra*ewinde'w<wvieww<'addussser'f<
wu#er#wxyarra*ew,wf<
f<
"iSp theo v:o protecteddview#dproect t5o view Oile m8i t2n addu#er.php
ophp
thi#ypa%e"itlexYii::appefyname . w Add #er "o Proectw
thi#ybreadcrumb#xarra*e
modelyproectynamexyarra*ewvieww<widwxymodelyproectyidf<
wAdd #erw<
f
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
thi#ymenuxarra*e
arra*ewlabelwxywcack "o Proectw< wurlwxyarra*ewvieww<widwxymodel
yproectyidff<
f
oy
hyAdd #er "o ophp echo modelyproectyname oydhy
<?php if(Yii::app()->user->hasFIash('success')):?>
<div cIass="successMessage">
<?php echo Yii::app()->user->getFIash('success'); ?>
</div>
<?php endif; ?>
div cla##xOormy
ophp Oormxthi#ybe%inWid%etewaActivenormwf oy
p cla##xnoteynield# with #pan cla##xre)uiredy|d#pany are
re)uired.dpy
div cla##xrowy
ophp echo OormylabelE'emodel<wu#ernamewf oy
ophp
thi#ywid%etewii.wid%et#.ui.auiAutoaompletew< arra*e
wnamewxywu#ernamew<
w#ourcewxymodelycreate#ername$i#tef<
wmodelwxymodel<
wattributewxywu#ernamew<
woption#wxyarra*e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wmin$en%thwxyw2w<
f<
whtmlmption#wxyarra*e
w#t*lewxywhei%ht:20p'w
f<
ff
oy
ophp echo Oormyerroremodel<wu#ernamewf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wrolewf oy
ophp
echo OormydropDown$i#temodel<wrolew<
Proect::%et#er{olemption#eff oy
ophp echo Oormyerroremodel<wrolewf oy
ddivy
div cla##xrow button#y
ophp echo aHtml::#ubmitcuttonewAdd #erwf oy
ddivy
ophp thi#yendWid%etef oy
ddivy
"iSp theo v:o protecteddview#dproectdview.php th2m /o5n #au v:o menu:
thi#ymenuxarra*e
P
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
arra*ewlabelwxywAdd #er "o Proectw< wurlwxyarra*ewproectdaddu#erw<
widwxymodelyidff<
f
"iSp theo b5n v:o phpm*admin th2m m8i b\n %hi tron% tbl+auth+item v8i
name: member< t*pe: < data: member
(au /. %G v:o tr>nh du*Jt : http:ddlocalho#tddev*iidinde'.phpo
rxproectdaddu#eridx2
aHNu "HY (E{ amYYEN"
~S Ho5ch:
. "hiSt kS v: t5o b\n% a(D$ hi trR comment
2. "5o Yii A{ cla## li2n )uan v8i commnt n%o:i b\n%
!. "h2m Oorm t8i i##ue cho phNp u#er #ubmit comment
. HiFn thD tIt c\ danh #4ch comment li2n )uan t8i i##ue tron% tran% chi
tiSt
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
. "iSn h:nh ti2u chu}n cKa Yii wid%et /F hiFn thD danh #4ch cKa
comment %0n/@* tron% tran% danh #4ch proect.
Mz. "5o model
"r?8c ti2n ta # c0n ph\i t5o b\n% comment tron% c#dl < v:o
phpm*admin v: thLc hiJn c4c )uer* #au:
To bng tbI_comment
create table tbl+commente
id intef not null primar* ke* auto+increment<
content te't not null<
i##ue+id intef Nm" N$$<
create+time datetime DEnA$" N$$<
create+u#er+id intef DEnA$" N$$<
update+time datetime DEnA$" N$$<
update+u#er+id intef DEnA$" N$$
fENuzNExznnoDc
"5o kh.a n%o:i v8i tbl+i##ue
alter table tbl+comment add con#traint Ok+comment+i##ue Oorei%n
ke*ei##ue+idf reOerence# tbl+i##ueeidf on delete ca#cade on update
re#trict
"5o kh.a n%o:i v8i tbl+u#er create+u#er
alter table tbl+comment add con#traint Ok+comment+owner Oorei%n
ke*ecreate+u#er+idfreOerence# tbl+u#ereidf on update re#trict on delete
re#trict
"5o kh.a n%o:i v8i tbl+u#er update+u#er
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
alter table tbl+comment add con#traint Ok+comment+update+u#er
Oorei%n ke*eupdate+u#er+idf reOerence# tbl+u#ereidf on update re#trict
on delete re#trict
"iSp theo v:o uii http:ddlocalho#tddev*iidinde'.phporx%iiddeOaultdinde'
alick v:o menu : ModeI Generator v: t5o aomment
(au /. nhIn Preview y uenerated.
1F kiFm tra v:o protecteddmodel#daomment.php ch3n% ta c. thF nh>n thI*
m=i )uan hJ /?Rc khli t5o tron% relation:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
d||
| ,return arra* relational rule#.
|d
public Ounction relation#ef

dd Nm"E: *ou ma* need to adu#t the relation name and the related
dd cla## name Oor the relation# automaticall* %enerated below.
return arra*e
wupdate#erw xy arra*e#elO::cE$mNu(+"m< w#erw< wupdate+u#er+
idwf<
wi##uew xy arra*e#elO::cE$mNu(+"m< wz##uew< wi##ue+idwf<
wcreate#erw xy arra*e#elO::cE$mNu(+"m< w#erw< wcreate+u#er+
idwf<
f

ah3n% ta c. thF nh>n thI* )uan hJ /bc biJt cKa comment thu6c v; i##ue.
Nh?n% ch3n% ta cn% c0n /Dnh n%ha )uan hJ onetoman* em6t nhi;uf
%iAa m6t i##ue v: nhAn% comment< m6t i##ue c. thF c. nhi;u comment<hr*
tha* /Qi z##ue cla## model:
public Ounction relation#ef

return arra*e
wre)ue#terw xy arra*e#elO::cE$mNu(+"m< w#erw< wre)ue#ter+idwf<
wownerw xy arra*e#elO::cE$mNu(+"m< w#erw< wowner+idwf<
wproectw xy arra*e#elO::cE$mNu(+"m< wProectw< wproect+idwf<
'comments' => array(seIf::HAS_MANY, 'Comment',
'issue_id'),
'commentCount' => array(seIf::STAT, 'Comment', 'issue_id'),
f


ah3n% ta cn% c0n tha* /Qi kS th^a l /0u t2n cla## aomment
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp
d||
| "hi# i# the model cla## Oor table tbl+comment.
|d
cla## aomment e'tend# DevYiiActive{ecord
P
Mz.2 "5o comment a{D
c@* %i- ch3n% ta c. A{ model < tiSp theo v:o uii t5o CRUD generator
v8i t2n Comment. M reiew M ~enerated
c5n c. thF v:o http:ddlocalho#tddev*iidinde'.phporxcommentdcreate /F
'em c4c Oield dA liJu /r /?Rc %enerated #n< v: v:o
protecteddcontroller#daommentaontroller.php /F 'em )ua cIu tr3c cKa
n..
Mz.! "h2m m6t comment
"heo h?8n% dkn #n<ch3n% ta /r c. c4c khli t5o ra comment< ch3n%
ta c0n th2m method tron% z##ue A{ cla## eprotecteddmodeldz##ue.phpf :
public Ounction addaommentecommentf

commentyi##ue+idxthi#yid
return commenty#aveef

Ph?Hn% thZc n:* # tr\ v; %i4 trD cKa i##ueyid tr?8c khi m6t
comment m8i /?Rc l?u v:o c#dl.
"iSp theo ch3n% ta c0n v:o
protecteddcontrollerdz##ueaontroller.php::actionMiewef method ch3n% ta
# c0n th2m comment A{ /F chu*Fn #an% view:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
public Ounction actionMieweidf

i##uexthi#yloadYodeleidf
commentxthi#ycreateaommentei##uef
thi#yrenderewvieww<arra*e
wmodelwxyi##ue<
wcommentwxycomment<
ff

Mz. HiFn thD norm


"a thI* comment l: m6t th:nh ph0n njm l %0n nh? tIt c\ mEi nHi
tron% Zn% d`n% cKa b5n v> v9* n2n m6t comment khCn% n2n chv njm l
m6t Oile view ri2n% biJt< b5n n2n t5o m6t Oile view chun% /F c. thF t4i #_
d`n% n. nh- partial iews. (_ d`n% renderPartialef # chv render ra n6i
dun% chZa /Ln% tron% view# Oile.
1F thLc hiJn ta v:o tron% protecteddview#di##ued t5o Oile m8i c. t2n :
Ycomments.php v8i n6i dun% #au /@*:
ophp Ooreachecomment# a# commentf: oy
div cla##xcommenty
div cla##xauthory
ophp echo aHtml::encodeecommentyauthoryu#ernamef oy:
ddivy
div cla##xtimey
on ophp echo dateewn < Y gagt h:i aw<#trtotimeecommentycreate+
timeff oy
ddivy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
div cla##xcontenty
ophp echo nl2breaHtml::encodeecommentycontentff oy
ddivy
hry
ddivy comment y
ophp endOoreach oy
nile n:* chIp nh9n m\n% arra* comment /?Rc khli t5o tron% input
Oorm v: hiFn thD ch3n%. ah3n% ta c0n /i;u chvnh view# Oile cho i##ue
detail khi #_ d`n% Oile m8i. ah3n% ta v:o
protecteddview#di##uedview.php v: th2m /o5n #au v:o d?8i c]n% cKa
Oile:
div idxcomment#y
ophp iOemodelycommentaountyxf: oy
h!y
ophp echo modelycommentaounty o modelycommentaount . w
comment#w : wmne commentw oy
dh!y
ophp thi#yrenderPartialew+comment#w<arra*e
wcomment#wxymodelycomment#<
ff oy
ophp endiO oy
h!y$eave a aommentdh!y
ophp iOeYii::appefyu#eryha#nla#hewcomment(ubmittedwff: oy
div cla##xOla#h#ucce##y
ophp echo Yii::appefyu#ery%etnla#hewcomment(ubmittedwf oy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ddivy
ophp el#e: oy
ophp thi#yrenderPartialewdcommentd+Oormw<arra*e
wmodelwxycomment<
ff oy
ophp endiO oy
ddivy
$0n tha* /Qi cu=i c]n% ch3n% ta l:m v8i comment input Oorm . norm
/?Rc t5o # chZa nhAn% Oield input /?Rc /Dnh n%ha tron% table
tbl+comment.ah3n% ta # l:m m6t Oorm /Hn %i\n bjn% viJc v:o
Protecteddview#dcommentd+Oorm.php v: /i;u chvnh n. nh? #au:
div cla##xOormy
ophp Oormxthi#ybe%inWid%etewaActivenormw< arra*e
widwxywcommentOormw<
wenableAa'MalidationwxyOal#e<
ff oy
p cla##xnoteynield# with #pan cla##xre)uiredy|d#pany are
re)uired.dpy
ophp echo Oormyerror(ummar*emodelf oy
div cla##xrowy
ophp echo OormylabelE'emodel<wcontentwf oy
ophp echo Oormyte'tAreaemodel<wcontentw<arra*ewrow#wxy<
wcol#wxy0ff oy
ophp echo Oormyerroremodel<wcontentwf oy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ddivy
div cla##xrow button#y
ophp echo aHtml::#ubmitcuttonemodelyi#New{ecord o wareatew :
w(avewf oy
ddivy
ophp thi#yendWid%etef oy
ddivy
M:o tr>nh du*Jt http:ddlocalho#tddev*iidinde'.phporxproect
ahEn m6t id bIt kq rhi nh>n v:o menu b2n ph\i chEn Create Issue
nh>n v:o menu b2n ph\i chEn Manage Issue #au /. click v:o chi tiSt
m6t tran% # nh>n thI* kSt )u\ #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Mz. "5o wid%et comment %0n /@*
Mz.. Name# #cope#
1F bBt /0u t5o wid%et < ch3n% ta # chvnh l5i aomment A{ emodelf tr\ v;
comment %0n /@*</F l:m /i;u n:* ch3n% ta # tiSn h:nh chvnh l5i ti2u chu}n
Yii model cla## kh4c c. t2n l: D-opes.
(cope# cho phNp ch3n% ta #_ d`n% t2n t^ kh.a ri2n%<n. cun% cIp c4ch /Dnh
n%ha c4c /i;u kiJn c@u lJnh (W$ /F trVch lEc danh #4ch tron% A{ model.
Name #cope# /?Rc /Dnh n%ha bli aactive{ecord::#cope#ef < vV d` nSu b5n
mu=n /Dnh n%ha name #cope %Ei tr\ v; comment %0n /@* ch3n% ta c.
aomment::#cope#ef:
cla## aomment e'tend# DevYiiActive{ecord

...
public Ounction #cope#ef

return arra*e
wrecentwxyarra*e
worderwxywcreate+time DE(aw<
wlimitwxy<
f<
f

...

(au /. ch3n% ta dt d:n% lEc danh #4ch tron% A{ model:


L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
comment#xaomment::modelefyrecentefyOindAllef
Hobc /Hn %i\n hHn nAa ch3n% ta chv thLc hiJn nh? #au:
public Ounction recentelimitxf

thi#y%etDbariteriaefymer%eWithe
arra*e
worderwxywt.create+time DE(aw<
wlimitwxylimit<
f
f
return thi#

Mz..2 Nhi;u hHn v; )uan hJ A{ )uer* tron% Yii


V i nh ng " ph?Hn% thZc tr2n ta c. thF #_ d`n% dt d:n% v8i name#cope /F kSt
n=i v8i A{ model< cho vV d` ch3n% ta c. thF 'em )uer* #au:
comment# x aomment::modelefywithearra*ewi##uewxyarra*ewconditionwxyw
proect+idxwfffyrecente0fyOindAllef
c@* %i- ch3n% ta )ua* l5i b:i hEc cKa ch3n% ta< v:o
protecteddcontrollerdz##ueaontroller.php v: chvnh l5i tron% Ounction loadYodelef
l5i nh? #au:
public Ounction loadYodeleid< withaomment#xOal#ef

iOewithaomment#f
model x z##ue::modelefywithearra*ewcomment#wxyarra*ewwithwxywa
uthorwfffyOindc*Pkeidf
el#e
modelxz##ue::modelefyOindc*Pkeidf
iOemodelxxxnullf
throw new aHttpE'ceptione0<w"he re)ue#ted pa%e doe# not
e'i#t.wf
return model
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

"ron% actionMiewef th2m v:o tr'e tron% loadYodelef:


public Ounction actionMieweidf

i##uexthi#yloadYodeleid< truef
....

Mz..! "5o wid%et


M:o protecteddmodel#daomment.php th2m h:m #au:
public Ounction Oind{ecentaomment#elimitf

return #elO::modelefywithewi##uewf
yOindAllearra*eworderwxywt.create+time DE(aw<wlimitwxylimit<ff

M:o protecteddcomponent#d t5o m8i Oile {ecentaomment#.php v8i n6i dun%


#au:
ophp
cla## {ecentaomment# e'tend# aWid%et

private +comment#
public di#pla*$imit x
public proectzd x null
public Ounction initef

thi#y+comment# x aomment::modelef
yOind{ecentaomment#ethi#ydi#pla*$imit< thi#yproectzdf

public Ounction %et{ecentaomment#ef

return thi#y+comment#

public Ounction runef

dd thi# method i# called b* aaontroller::endWid%etef
thi#yrenderewrecentaomment#wf

oy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"iSp theo v:o protecteddcomponent#d t5o view#drecentaomment#.php :
et5o m8i Oilef:
uly
ophp Ooreachethi#y%et{ecentaomment#ef a# commentf: oy
div cla##xauthory
ophp echo commentycreate#eryu#ername oy added a comment.
ddivy
div cla##xi##uey
ophp echo aHtml::linkeaHtml::encodeecommentyi##ueynamef<
arra*ewi##uedvieww< widwxycommentyi##ueyidff oy
ddivy
ophp endOoreach oy
duly
au=i c]n% v:o protecteddview#dproectdinde'.php th2m /o5n #au v:o cu=i Oile:
ophp thi#ywid%etew{ecentaomment#wf oy
~St )u\ #au: http:ddlocalho#tddev*iidinde'.phporxproect
M9* l: n%on l:nh c:nh /:o< tiSp t`c ta # th2m /0u v: cu=i wid%et v8i
CportIet: tha* thS /o5n tr2n tron% protecteddview#dproectdinde'.php bjn%
/o5n #au:
ophp
thi#ybe%inWid%etewii.wid%et#.aPortletw< arra*ewtitlewxyw{ecentaomment#w<ff
thi#ywid%etew{ecentaomment#wf
thi#yendWid%etef
oy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
~St )u\:

Mz.. "h2m wid%et v:o m6t tran% kh4c


"iSp theo ch3n% ta # th2m comment %0n /@* v:o
protecteddview#dproectdview.php v:o cu=i tran%:
ophp
thi#ybe%inWid%etewii.wid%et#.aPortletw< arra*e
wtitlewxyw{ecent aomment# mn "hi# Proectw<
ff
thi#ywid%etew{ecentaomment#w< arra*ewproectzdwxymodelyidff
thi#yendWid%etef oy
M: /@* l: kSt )u\< http:ddlocalho#tddev*iidinde'.phporxproectdviewidx
M8i c4ch n:* ta c. thF dt d:n% nh3n% m6t wid%et t8i bIt k> vD trV n:o tron%
Zn% d`n% m6t c4ch hiJu )u\.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
aHNu "HY {(( WEc nEED
~ HmaH
. "\i c:i /bt Uendnramework v:o Zn% d`n% Yii
2. "5o m8i action tron% controller cla## '_ lT /Dnh d5n% need
!. ahvnh l5i {$ #ao cho dt #_ d`n%
. "h2m viJc t5o m8i Oeed t8i c\ danh #4ch proect lkn tran% chi tiSt
proect
Mzz. a:i /bt Uend nramework
M:o tran% chK cKa Uend v: chEn b\n Uendnramework .2 minimal e$?u T
#_ d`n% Uend < k #_ d`n% Uend 2 nSu k h9u )u\ r4n% chDuf
aIu tr3c th? m`c cKa n. nh? #au:
zN("A$$.t't
$zaEN(E.t't
{EADYE.t't
bind
librar*d
M:o protected t5o m8i th? m`c vendor#.
c5n v:o th? m`c librar* cop* Oolder Uend v:o protecteddvendor#< v: th?
m`c cKa n. # l: protecteddvendor#dUend
$?u T< n%o:i viJc #_ d`n% Uend need t^ vV d` n:* b5n cn% c. thF #_
d`n% Uend mail /F %_i mail t8i kh4ch h:n% tron% c4c Zn% d`n%< bli v9*
n2n hr* ch3 T.
Mzz.2 (_ d`n% Uend+need
Uend+Oeed l: th:nh ph0n nh[ tron% Uend component c. chZc n7n%
t5o web Oeed dt d:n% v: nhanh ch.n%< ch3n% ta # #_ d`n% Uend+Oeed tron%
Yii m6t c4ch dt d:n% <
M:o protecteddcontroller#daommentaontroller.php t5o m6t action t2n
actionneedef.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
public Ounction actionneedef

iOei##ete+uE"wpidwff

comment# x aomment::modelefywithearra*e
wi##uewxyarra*e
wconditionwxywproect+idx:proectzdw<
wparam#wxyarra*ew:proectzdwxyintvale+uE"wpidwff<
fffyrecente20fyOindAllef

el#e
comment# x aomment::modelefyrecente20fyOindAllef
entrie#xarra*ef
Ooreachecomment# a# commentf

entrie#xarra*e
wtitlewxycommentyi##ueyname<
wlinkwxyaHtml::encodeethi#ycreateAb#oluterlewi##ued
vieww<arra*ewidwxycommentyi##ueyidfff<
wde#criptionwxy commenty create#eryu#ername . w#a*#:bryw .
commentycontent<
wla#tpdatewxy#trtotimeecommentycreate+timef<
wauthorwxyaHtml::encodeecommentycreate#eryu#ernamef<
f

L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ddnow u#e the Uend need cla## to %enerate the need
dd %enerate and render {(( Oeed
OeedxUend+need::importArra*earra*e
wtitlew xy wDevelopment Yii aomment# needw<
wlinkw xy thi#ycreateAb#oluterlewwf<
wchar#etw xy w"n&w<
wentrie#w xy entrie#<
f< wr##wf
Oeedy#endef

"iSp theo ta c0n l:m m6t #= tha* /Qi tron% controller aommentaontroller /F
cla## tron% end c. thF l:m viJc < l /0u tran% cla## b5n th2m /o5n #au:
Yii::importewapplication.vendor#.|wf
re)uire+onceewUenddneed.phpwf
re)uire+onceewUenddneedd{##.phpwf
"iSp theo aommentaontroller::acce##{ule#ef method cho phNp u#er 'em
/o5n Oeed /.
public Ounction acce##{ule#ef

return arra*e
arra*ewalloww< dd allow all u#er# to perOorm winde'w and wvieww
action#
'actions'=>array('index','view','feed'),
wu#er#wxyarra*ew|wf<
f<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
1@* l: kSt )u\: http:ddlocalho#tddev*iidinde'.phporxcommentdOeed
Mzz.! "5o {$ "h@n thiJn v8i #er
Ybc /Dnh cKa Yii b5n th?-n% nh>n thI* tham biSn chVnh l: r v: tiSp theo /.
l: controller+id v: action+id< vV d` v8i url cKa Oeed
http:ddlocalho#tddev*iidinde'.phporxcommentdOeed n. thLc #L khCn% /p v:
khCn% t=t cho )u4 tr>nh (Em ph4t triFn< ta c0n c. m6t url /p hHn nAa nh?
kiFu: http:ddlocalho#tddev*iidcommentOeed hobc
http:ddlocalho#tddev*iidcommentOeed.'ml
1F l:m /?Rc /i;u n:* ch3n% ta c0n ph\i nh- #L trR %i3p cKa Yii# {$
Yana%ement /F chvnh l5i /Dnh d5n% cKa {$.
Mzz.!. (_ d`n% {$ Yana%er
1F '@* dLn% cIu h>nh l5i {$ b5n c0n v:o protecteddconOi%dmain.php Oile /F
/Dnh n%ha url mana%er:
wurlYana%erwxyarra*e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wurlnormatwxywpathw<
f<
Ybc /Dnh th> n. /r njm tron% arra* component v: /r bD dsn% comment d|
|d /F b[ kVch ho5t n..
aCn% viJc cKa b5n l: b[ 2 dsn% comment /. /F kVch ho5t urlYana%er.
wurlYana%erwxyarra*e
wurlnormatwxywpathw<
wrule#wxyarra*e
wcontroller:gwydid:gdywxywcontrollerydvieww<
wcontroller:gwydaction:gwydid:gdywxywcontroller
ydactionyw<
wcontroller:gwydaction:gwywxywcontrollerydactio
nyw<
f<
f<
c@* %i- th> b5n /r c. thF v:o /?Rc url :
http:ddlocalho#tddev*iidinde'.phpdcommentdOeed m6t c4ch dt d:n%< v: t^ na*
khi v:o c4c cCn% c` b5n vui lsn% b[ /o5n rx /i khi v:o uii vV d`:
http:ddlocalho#tddev*iidinde'.phpd%iiddeOaultdinde'
Hobc v:o proect : http:ddlocalho#tddev*iidinde'.phpdproect . /F b[ n=t c\
inde'.php b5n c0n ph\i /Dnh n%ha l5i rl v: thiSt l9p controllerdaction tr[ /Sn
url /Dnh danh /.< /i;u n:* /r /?Rc viSt tron% Yii deveIopment Cookbook
ebook tCi /r dDch rhi< b5n vui lsn% v:o 'em l5i nhN
Mzz.!.2 aIu h>nh l5i {outin% rule# eWu* tBc route#f
{outer l: %> : $: m6t %.i url tr\ v; m: tr>nh du*Jt *2u c0u hJ th=n% '_ lT< n.
chZa c\ ph0n #= v: ph0n chA< vV d` nh? c4c biSn +uE"< +Pm("< hobc /Hn
%i\n l: c4c ti2u /; n6i dun% cKa m6t b:i n:o /. m: tr>nh du*Jt *2u c0u hJ
th=n% tr\ l-i v8i %.i /.< /F thLc hiJn cCn% viJc n:* Yii #_ d`n% rl mana%er
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
/F /Dnh n%ha c4c khai b4o tru* nh9p router /F )u*St /Dnh 'em controller n:o
v: action n:o /?Rc thLc hiJn .
M8i nhAn% )u* tBc {$< thiSt l9p aurlYana%er Oile # tr\ v; c4c thu6c tVnh )u*
tBc theo m\n% arra* v8i /Dnh d5n% patternxyroute.
M: /@* l: vV d` )ua v; m6t )u* tBc: "h2m /o5n #au v:o urlYana%er:
ddkhai b4o /?-n% dkn i##ue tr\ v; controllerdaction ei##uedinde'f
ddkhai b4o /?-n% dkn chi tiSt i##ue theo d5n% i##ue#did ed5n% #=f tr\ v;
ei##uedviewf.
wurlYana%erwxyarra*e
wurlnormatwxywpathw<
wrule#wxyarra*e
wi##uehellowxywi##uedinde'w<
wi##uedid:gdyd|wxywi##uedvieww<
f<
f
M8i khai b4o i##uehello b5n c. thF v:o tr>nh du*Jt 'em kSt )u\:
http:ddlocalho#tddev*iidinde'.phpdi##uehello n. /r nh9n /3n% khCn% +. 1hn%
th-i v8i c4ch n:* b5n c. thF v:o /?Rc
http:ddlocalho#tddev*iidinde'.phpdi##ued m: khCn% c0n ph\i %G
http:ddlocalho#tddev*iidinde'.phpdi##uedviewd m6t /?-n% dkn rIt 'Iu.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"iSp theo nSu ch3n% ta mu=n c. /?-n% dkn nh? #au:
http:ddlocalho#tddev*iidinde'.phpdcommentneed.'ml
"h9t /Hn %i\n ta v:o url Yana%er khai b4o nh? #au:
wurlYana%erwxyarra*e
wurlnormatwxywpathw<
'ruIes'=>array( 'commentfeed'=>array('comment/feed',
'urISuffix'=>'.xmI', 'caseSensitive'=>faIse),
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f<
f<
"ron% /. commentOeed l: url alia# v: url#uOOi' l: h9u t= #au url alia# <
ca#e(en#itive chVnh l: tr?-n% hRp ?u ti2n chEn router l /@* l: mbc /Dnh ek
?u ti2nf v: b5n v:o tr>nh du*Jt /F th?ln% thZc th:nh )u\.
http:ddlocalho#tddev*iidinde'.phpdcommentOeed.'ml
Mzz.!.! $o5i b[ kDch b\n inde'.php t^ {$
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
c@* %i- ch3n% ta # '.a th:nh ph0n inde'.php tr?8c url< /i;u n:* c0n l:m
theo 2 b?8c:
. ahvnh l5i web#erver cIu h>nh router tIt c\ c4c re)ue#t # khCn% '_ lT
Oile thn t5i hobc /?-n% dkn li2n )uan t8i inde'.php
2. "hiSt l9p url mana%er component v: chEn thu6c tVnh:
#how(criptName l: Oal#e.
~hi ch3n% ta #_ d`n% Apache H""P #erver ch3n% ta c. thF thLc hiJn %iai
/o5n /0u ti2n v8i Oile .htacce## tron% th? m`c %=c cKa Zn% d`n% v:
ch3n% ta t5o Oile .htacce## nh? #au:
"urnin% on the rewrite en%ine i# nece##ar* Oor the Oollowin% rule#
and Oeature#.
nollow(*m$ink# mu#t be enabled Oor thi# to work.
zOYodule mod+rewrite.cy
mption# nollow(*mlink#
{ewriteEn%ine mn
dzOYoduley
nle## an e'plicit Oile or director* e'i#t#< redirect all re)ue#t to
###Yii entr* #cript
zOYodule mod+rewrite.cy
{ewriteaond {EWE("+nz$ENAYE O
{ewriteaond {EWE("+nz$ENAYE d
{ewrite{ule . inde'.php
dzOYoduley
c@* %i- ch3n% ta v:o http:ddlocalho#tddev*iidcommentOeed.'ml +
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
http:ddlocalho#tddev*iidproect + c. /p l2n hn khCn% 5 o
c?8c thZ 2 /Hn %i\n rhi chvnh l5i urlYana%er v8i dsn% #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wurlYana%erwxyarra*e
wurlnormatwxywpathw<
wrule#wxyarra*e
wcommentOeedwxyarra*ewcommentdOeedw< wurl(uOOi'wxyw.'mlw<
wca#e(en#itivewxyOal#ef<
f<
ddtBt hn inde'.php
'showScriptName'=>faIse,
f<
qu, th I Ii c framework na xi ri hehe
1=i v8i nhAn% url /i;u khiFn v8i chv #= zD nh? Proect zD< ch3n% ta c. thF l:m
theo c4ch #au:
wurlYana%erwxyarra*e
wurlnormatwxywpathw<
wrule#wxyarra*e
'<pid:\d+>/commentfeed'=>array('comment/feed',
'urISuffix'=>'.xmI', 'caseSensitive'=>faIse),
wcommentOeedwxyarra*ewcommentdOeedw< wurl(uOOi'wxyw.'mlw<
wca#e(en#itivewxyOal#ef<
f<
w#how(criptNamewxyOal#e<
f<
M:o tr>nh du*Jt : http:ddlocalho#tddev*iid2dcommentOeed.'ml 'em kSt )u\:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Mzz.!. "h2m need $ink#
c@* %i- ch3n% ta # t5o url Oeed cIu tr3c th@n thiJn hHn cho n%?-i
d]n%<ch3n% ta c0n th2m #ub#cribe Oeed<v:o proectaontroller::actionznde'ef
ahvnh l5i nh? #au:
public Ounction actionznde'ef

dataProviderxnew aActiveDataProviderewProectwf
Yii::app()->cIientScript->registerLinkTag(
'aIternate',
'appIication/rss+xmI',
$this->createUrI('comment/feed'));
thi#yrenderewinde'w<arra*e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wdataProviderwxydataProvider<
ff

c5n # thI* /o5n #au l /0u th head:


link relxalternate t*pexapplicationdr##'ml hreOxdcommentOeed.
'ml dy
Mzz.!. u_i mail t8i u#er v8i Uend+Yailef
ah3n% ta # l:m vV d` thLc hiJn viJc %_i email t8i u#er bjn% c4ch #_ d`n%
Uend+mail t5i aommentaontroller.php ::actionYailef
c5n v:o protecteddcontrollerdaommentaontroller th2m /o5n #au v:o /0u
cla##:
re)uire+onceewUenddYail.phpwf
re)uire+onceewUenddYaild"ran#portd(mtp.phpwf
re)uire+onceewUenddYaildProtocold(mtpdAuthd$o%in.phpwf
"5i Ounction acce##{ule#ef

array('aIIow',
// aIIow aII users to perform 'index' and 'view' actions
'actions'=>array('index','view','feed','maiI'),
'users'=>array('*'),
),

X@* dLn% h:m mail:


public Ounction actionYailef
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

contentxwi love *ouw


addtoxwm*love,%mail.comw
#ubectxwDear lovew
emailOromxwlon%t&',%mail.comw
tr x new Uend+Yail+"ran#port+(mtp ew#mtp.%mail.comw<
arra*ewauthw xy wlo%inw< wu#ernamew xy wlon%t&',%mail.comw< wpa##wordw xy
wpa##word u#erw< w##lw xy w##lw< wportw xy ff
Uend+Yail::#etDeOault"ran#portetrf
mail x new Uend+Yailew"n&wf
maily#etcod*Htmleecontentff
mailyadd"oeaddtof
maily#et(ubecte#ubectf
maily#etnrom eemailOromf
maily#end ef

M:o tr>nh du*Jt 'em th:nh )u\ : http:ddlocalho#tddev*iidcommentdmail


Do khCn% c. ho#t n2n nSu b5n %G v:o # b4o lii khCn% thF ml %.i tr2n v>
u#er name v: pa##word khCn% chVnh '4c< v: b5n ph\i c. ho#t thLc tr2n m5n%
m8i c. thF %_i #ocket.
"u* nhi2n /Sn %iai /o5n n:* l: b5n /r ho:n to:n c. thF %_i mail online /?Rc
khi b5n ch5* tr2n web#ite thLc.
Mzz.!. "5o nile PDn M8i Uend+PDn
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
c5n v:o th? m`c %=c dev*ii t5o m6t Oolder c. t2n p%E n%an% h:n% v8i
protected /F chZa Oile t5o pdO
c5n v:o protecteddcontrollerdaommentaontroller.php th2m /o5n #au v:o /0u
cla##:
re)uire+onceewUenddPdO.phpwf
re)uire+onceewUenddPdOdPa%e.phpwf
re)uire+onceewUenddPdOdnont.phpwf
(_a l5i Ounction acce##{ule#ef th2m v:o actionPdO:
public Ounction acce##{ule#ef

return arra*e
array('aIIow', // aIIow aII users to perform 'index' and 'view'
actions
'actions'=>array('index','view','feed','maiI','pdf'),
'users'=>array('*'),
f<
"5o aommentaontroller:: actionPdOef:
public Ounction actionPdOef

pdO x new Uend+PdOef


pa%e x new Uend+PdO+Pa%eeUend+PdO+Pa%e::(zUE+Af

dd deOine Oont re#ource
Oont x
Uend+PdO+nont::OontWithNameeUend+PdO+nont::nmN"+am{zE{f
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

dd thiet lap Oont < kich co Oont
dd viet noi dun% pdO
pa%ey#etnonteOont< 2f ydraw"e'tewz mi## *ou<w< 2< 20f
ydraw"e'tewcecau#e z love Youw< 2< 20f

pdOypa%e# x pa%e

dd luu Oile vao Oolder pdO
pdOy#aveewpdOdmi##.pdOwf

M:o tr>nh du*Jt %G : http:ddlocalho#tddev*iidcommentdpdO


M:o Oolder pdO m8i t5o # thI* m6t Oile t2n mi##.pdO v: ml ra # c. h>nh #au:
M8i c4ch n:* b5n c. thF t5o ra m6t Oile pdO v: t5o /?-n% link t8i Oile /. /F
u#er c. thF download /?Rc m6t #= tran% theo kiFu h.a /Hn thanh to4n.
aHNu & : $Y Nu D-Nu 1P HN
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
~ HmaH
"5o theme m8i cho Zn% d`n% bjn% viJc t5o la*out m8i<a(( v: Oolder
v: c4c t:i #\n kh4c c0n /?Rc cun% cIp tron% Zn% d`n% v8i thiSt kS
Orontend m8i.
(_ d`n% ti2u chu}n n%Cn n%A v]n% mi;n cKa Yii /F chu*Fn dDch n%Cn
n%A m8i tron% Zn% d`n%.
c@* %i- ch3n% ta # /i v:o thLc hiJn
Mzzz. "hiSt kS la*out
Y6t la*out tron% Yii l: m6t Oile view# /bc biJt n. #_ d`n% c4c view# Oile
kh4c< /bc tVnh la*out chZa /Ln% %iao diJn n%?-i d]n% v: c4c th:nh ph0n< mr
lJnh cKa nhi;u view# Oile<khi #_ d`n% la*out /F chu*Fn t8i m6t view# Oile< Yii
nh3n% Oile view# /. v:o la*out.
Mzzz..2 "5o m6t la*out
a. hai c4ch chVnh c. thF t5o ra la*out v: khai b4o la*out m8i< m6t l:
%Ei thu6c tVnh la*out cKa awebApplication< mbc /Dnh t^
protecteddview#dla*out#dmain.php .an% nh? viJc lLa chEn v8i to:n Zn% d`n%
n. c. thF t\i t^ cIu h>nh main tron% protecteddconOi%dmain.php
MV d` ch3n% ta c0n t5o la*out m8i tron%
protecteddview#dla*out#dnew#la*out.php b5n mu=n #_ d`n% n. cho to:n Zn%
d`n% b5n c0n v:o protecteddconOi%dmain.php khai b4o nh? #au v:o /0u m\n%
arra*:
return arra*e
...
'Iayout'=>'newIayout',
$a*out n:* /?Rc kS th^a la*out Path thu6c tVnh cKa awebApplication
v8i mbc /Dnh protecteddview#dla*out#.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Y6t nHi kh4c cn% thiSt l9p thu6c tVnh la*out /. l: tron% controller cla##.
1@* l: c4ch n. t5o la*out /bc biJt khi Zn% d`n% /?Rc khli t5o<#_ d`n%
Yii tool t5o tL /6n% ra /?Rc protecteddcomponent#daontroller.php < khi b5n
ml Oile n:* ra b5n # thI* c6t khai b4o la*out /?Rc thiSt l9p la*out c. t2n
col'mnb. a4c thiSt l9p n:* /?Rc /i;u khiFn bli thiSt l9p t^ awebapplication
cla##.
Mzzz..! (_ d`n% la*out
~hi #_ d`n% la*out Oile ta c0n %Ei acontroller::renderef method< khi b5n
l:m viJc %Ei renderef method /F %Ei m6t Oile view#< Yii # nh3n% n6i dun% cKa
Oile view# /. v:o la*out cKa b5n cn% nh? controller cla## hobc mZc /6 Zn%
d`n%.c5n t r4nh th_ bIt k> la*out n:o /?Rc render view# Oile khi %Ei bli
ph?Hn% thZc acontroller::renderPartialef.
ah3n% ta v:o protecteddview#dla*outdnewla*out.php m: ch3n% ta m8i
t5o v: th2m v:o n6i dun% #au:
Dma"YPE htmly
htmly
heady
titley"itle oO the documentdtitley
dheady
bod*y
div idxheadery
(ome Header aontent Here
ddivy
div idxcontenty
ophp echo content oy
ddivy
div idxOootery
(ome nooter aontent Here
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ddivy
dbod*y
dhtmly
c@* %i- ch3n% ta th_ /bt n. v:o #ite controller /F #_ d`n% la*out m8i<
ml protecteddcontrollerd(iteaontroller.php v: t\i la*out v:o cla## nh? #au:
cla## (iteaontroller e'tend# aontroller

pubIic $Iayout='newIayout';
c@* %i- ch3n% ta v:o tr>nh du*Jt %G :
http:ddlocalho#tddev*iid#itedlo%in
Mzzz.. ui8i thiJu cluePrint a(( nramework
cluePrint c## Oramework /?Rc th2m v:o Zn% d`n% khi b5n khli t5o Zn%
d`n% t^ dsn% lJnh Yii tool<ch3n% trR %i3p ti2u chu}n a(( cho b5n< cun% cIp
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ti2u chu}n /Dnh n%ha H"Y$ %i3p cho ph0n /Dnh n%ha a(( %bp Vt lii< n. t8i
v8i nhi;u #creen print Oriendl* la*out<
1F nh3n% n. v:o tran% b5n # c0n #_ d`n% /o5n lJnh #au:
blueprint a(( Oramework y
link relx#t*le#heet t*pexte'tdc## hreOxophp echo Yii::appef
yre)ue#tyba#erl oydc##d#creen.c## mediax#creen< proection dy
link relx#t*le#heet t*pexte'tdc## hreOxophp echo Yii::appef
yre)ue#tyba#erl oydc##dprint.c## mediaxprint dy
iO lt zE &y
link relx#t*le#heet t*pexte'tdc## hreOxophp echo Yii::appef
yre)ue#tyba#erl oydc##die.c## mediax#creen< proection dy
endiOy
Mzzz.. HiFu v; thiSt l9p c## cluePrint
clueprint /r njm #n tron% webrootdc## v: n. c. ! Oile chVnh l::
ze.c##
Print.c##
(creen.c##
NhAn% thiSt l9p cH b\n /;u /r chZa nhAn% Oile n:* rhi<v: ch3n% ta #
#_ d`n% c## bjn% viJc t5o th2m m6t Oile m8i cho Zn% d`n% cKa b5n<
c5n khCn% n2n viSt th2m c## v:o ! Oile n:*.
Mzzz.. "hiSt l9p ti2u /; tran%
1F thiSt l9p ti2u /; tran% b5n chv c0n #_ d`n% /o5n code #au:
titleyophp echo aHtml::encodeethi#ypa%e"itlef oydtitley
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
a0n nh8 l: view# Oile # tr\ v; khli t5o cho controller# cla## thu6c tVnh
/?Rc thiSt l9p tron% view#<pa%e"itle l: ph0n t_ /Dnh n%ha tron% acontroller
# chZa t2n action l:m mbc /Dnh
Mzzz.. 1Dnh n%ha header
Y6t lLa chEn th?-n% l: n6i dun% header th?-n% lbp l5i tron% nhi;u
tran%<v> thS n2n ta c0n /Dnh n%ha header tron% la*out /F n. t4i #_ d`n%:
bod*y
div cla##xcontainer idxpa%ey
di id-header
di id-logo,php echo Citml**encodeNDii**appNOMnameO] ,
/di
/diyMM header MM
1F /Qi t2n proect b5n v:o protecteddconOi%dmain.php v: tha* thS nh?
#au:
wnamewxywY* Web Applicationw< trl th:nh
wnamewxywDevelopment Yii Proectw<
~St )u\: http:ddlocalho#tddev*iid#itedlo%in
Mzzz..& HiFn thD menu navi%ation
"hanh /i;u h?8n% th ?-n% 'uIt hiJn lbp l5i l nhi;u tran% v> thS n2n n.
th?-n% njm tron% la*out b5n c. thF nh>n /o5n code #au:
div idxmainmenuy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp thi#ywid%etewii.wid%et#.aYenuw<arra*e
witem#wxyarra*e
arra*ewlabelwxywHomew< wurlwxyarra*ewd#itedinde'wff<
arra*ewlabelwxywAboutw< wurlwxyarra*ewd#itedpa%ew<
wviewwxywaboutwff<
arra*ewlabelwxywaontactw< wurlwxyarra*ewd#itedcontactwff<
arra*ewlabelwxyw$o%inw< wurlwxyarra*ewd#itedlo%inwf<
wvi#iblewxyYii::appefyu#eryi#uue#tf<
arra*ewlabelwxyw$o%out ew.Yii::appefyu#eryname.wfw<
wurlwxyarra*ewd#itedlo%outwf< wvi#iblewxyYii::appefyu#eryi#uue#tf
f<
ff oy
ddivy mainmenu y
"5i /@* ch3n% ta /r #_ d`n% wid%et amenu /?Rc %Ei t^ Uii component.
Mzzz.. "5o breadcrumb# navi%ation
ah3n% ta t8i protecteddview#dla*out#dmain.php /F 'em cIu tr3c code
v: b5n # nh>n thI* c. ! dsn% /Dnh n%ha breadcrumb bli Uii component v8i
abreadcrumb#:
ophp iOei##etethi#ybreadcrumb#ff:oy
,php XthisMwidgetN_ii.widgets.CQreadcr'mbs_, arrayN
_links_-XthisMbreadcr'mbs,
OO] ,yMM breadcr'mbs MM
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp endiOoy
1@* l: wid%et kh4c cKa Uii th?-n% /?Rc #_ d`n% /F hiFn thD link /i;u
h?8n% t8i tran%< vV d` : proectyyproect yyEdit
1F #_ d`n% wid%et n:* b5n c0n ph\i cIu h>nh thu6c tVnh links < c4i m:
b5n mu=n hiFn thD. "hu6c tVnh n:* /?Rc tr\ v; t^ m\n% arra* /?Rc /Dnh
n%ha tron% ph0n breadcrumb#< ch3n% ta # #_ d`n% vV d` v8i breadcrumb
proectyproect yyEdit:
arra*e
wProect#wxyarra*ewproectdinde'wf<
wProect wxyarra*ewproectdvieww<widwxyf<
wEditw<
f
M; mbc /Dnh Yii # tL /6n% t5o breadcrumb# dLa v:o t2n proect v:
action proect.
NSu b5n mu=n cu#tom breadcrumb# dLa theo T cKa b\n th@n hr* v:o
Protecteddview#dproectdupdate.php v: 'em cIu tr3c Oile b5n # t>m thI*
dsn% /Dnh n%ha breadcrumb# #au:
thi#ybreadcrumb#xarra*e
wProect#wxyarra*ewinde'wf<
modelynamexyarra*ewvieww<widwxymodelyidf<
wpdatew<
f
"5i /@* b5n /r khai b4o cIu tr3c breadcrumb# nh? #au:
ProectyyProect Name yypdate
c5n v:o : http:ddlocalho#tddev*iidproectdupdatedidd2
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Mzzz..0 1Dnh n%ha Oooter
c5n c. thF v:o protecteddview#dla*out#dmain.php v: 'em khai b4o l
cu=i /o5n :
div idxOootery
aop*ri%ht cop* ophp echo dateewYwf oy b* Y* aompan*.brdy
All {i%ht# {e#erved.brdy
ophp echo Yii::poweredef oy
ddivy Oooter y
Mzzz.. Ph@n chia la*out#
"h?-n% th> khi v:o web#ite Zn% d`n% # nh9n la*out mbc /Dnh t^
protecteddview#dla*out#dmain.php < nh?n% /. chv l: m6t ph0n< khi Zn% d`n%
/?Rc khli t5o< tIt c\ controller /?Rc t5o # kS th^a t^
protecteddcomponent#daontroller.php
NSu ch3n% ta ml Oile n:* ra ch3n% ta # thI* ph0n /Dnh n%ha la*out c. khai
b4o la*out cho controller l: : la*outxwddla*out#dcol'mnbw nh?n% n. khCn% ph\i
l: la*out chVnh d?-n% nh? la*out col'mnb l: la*out Oile mbc /Dnh cho tIt c\
c4c cla## con.c5n c. thF v:o protecteddview#dla*out#d v: # nh>n thI* ! Oile
la*out #au:
aolumn.php
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
aolumn2.php
Yain.php
M: t^ /@* ta c. thF thI* c4c controller t\i la*out l: column.php :
,php / ar Xthis Controller / ,
,php XthisMbeginContentN_//layo'ts/main_O] ,
di id-content
,php echo Xcontent] ,
/diyMM content MM
,php XthisMendContentNO] ,
"5i Oile n:* b5n c. thF thI* n. t\i la*out main.php v: to:n b6 n6i dun% cKa
c4c controller # njm tron% ph0n content cKa main.php
92' 7* Trong code ta c# nh&n thy "on khai bo layo't c# th%m tiEn th A//B ,
trong tr2Hng h5p n!y file layo't trong iews s t&m tKi th!nh ph;n iews chWnh
Nprotected/iews/O "25c kWch hot bVi mod'le hi(n ti, cch n!y s khi.n h(
thhng s d>ng "2Hng dln iews chWnh trong ?ng d>ng h@n i(c s d>ng
"2Hng dln tC mod'les. ah3n% ta # hEc v; module# tron% ch?Hn% t8i /@*.
ah3n% ta hr* c]n% 'em vV d` c4ch t\i view# Oile v8i
(iteaontroller::action$o%inef:
thi#yrenderelo%inf
"5i /@* hJ th=n% # thLc hiJn:
"\i tIt c\ n6i dun% tron% Oile protecteddview#d#itedlo%in.php v: l:m
content chIp nh9n %i4 trD content t^ la*out Oile tron% controller < /. l:
col'mnb.pphp
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
(au /. col'mnb.php # %Ei t8i la*out chVnh l: main.php< n6i dun% njm
%iAa be%inaontentef v: endaontentef # chu*Fn %i4 trD v; la*out
main.php v8i biSn content.
$a*out Oile main.php # t\i l2n v: tr\ v; %iao diJn t8i n%?-i d]n%< nh?
v9* ta c. thF nh>n thI* th:nh ph0n controller nh9n c\ Oile main.php !
col'mnb.php chZ khCn% ph\i du* nhIt la*out chVnh nh? b5n n%h.
Y6t la*out kh4c # tL /6n% /?Rc t\i khi t5o m8i Zn% d`n% /. l: la*out
col'mn6.php < khi b5n /7n% nh9p admin 'on% b5n v:o tron% )u\n trD proect
b5n # thI* c6t menu navi%ation njm b2n ta* ph\i /. l: cIu h>nh cKa la*out
column2.php < b5n v:o protecteddcontrollerdProectaontroller.php # thI* dsn%
khai b4o #au:
public la*outxwddla*out#dcolumn2w
N6i dun% cKa Oile column2.php
ophp d| ,var thi# aontroller |d oy
ophp thi#ybe%inaontentewddla*out#dmainwf oy
div cla##x#pany
div idxcontenty
ophp echo content oy
ddivy content y
ddivy
div cla##x#pan la#ty
div idx#idebary
ophp
thi#ybe%inWid%etewii.wid%et#.aPortletw< arra*e
wtitlewxywmperation#w<
ff
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
thi#ywid%etewii.wid%et#.aYenuw< arra*e
witem#wxythi#ymenu<
whtmlmption#wxyarra*ewcla##wxywoperation#wf<
ff
thi#yendWid%etef
oy
ddivy #idebar y
ddivy
ophp thi#yendaontentef oy
c5n c. thF nh>n thI* n%o:i viJc t\i la*out chVnh main.php ra n. csn th2m
ph0n b2n ph\i t5o ra m6t wid%et chZa menu )u\n trD v8i danh #4ch item#
/?Rc lEc t^ Uii component :: aportlet
(au n:* khi b5n /Dnh n%ha t^n% th:nh ph0n tron% la*out b5n cn% # l:m
t?Hn% tL<< th2m nhAn% th:nh ph0n c0n thiSt 'un% )uanh content nh- viJc #_
d`n% c4c wid%et cKa Yii
Mzzz..2 X@* dLn% theme Yii
"heme /?Rc #_ d`n% /F cu#tom c4c thiSt kS la*out tron% m6t Zn%
d`n% web<m6t tron% nhi;u Zn% d`n% n. tron% mC h>nh kiSn tr3c YMa l: tr>nh
ditn c4c thCn% tin b\n %hi tron% )u\n trD ebackendf. M: mbc /Dnh tIt c\
theme# /?Rc /Dnh n%ha c0n ph\i njm tron% webroot#dtheme# eOolder theme#
n%an% h:n% v8i protectedf . "It nhi2n n. /?Rc lLa chEn tron% cIu h>nh Zn%
d`n% v: n. /?Rc /i;u chvnh bli thu6c tVnh ba#ePath v: ba#erl tron%
themeYana%er component
"It c\ c4c Oile view# # ph\i njm tron% th? m`c dview#d< la*out # ph\i
njm tron% view#dla*out#d v: Oile hJ th=n% ph\i njm tron% view#d#*#temd.
MV d` nSu b5n t5o theme %Ei l: c'stom v: tha* thS view# update cKa
proect b5n c0n t5o view Oile update.php v: l?u n. tron%
theme#dcu#tomdview#dproectdupdate.php
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Mzzz..! "5o m6t theme#
ah3n% ta # t5o m6t theme m8i c. t2n l: newtheme, tron% dtheme#
"5o tiSp Oolder t2n newtheme< t5o hai /?-n% dkn con l: c## v: view#
"ron% view# Oolder b5n # t5o m8i la*out# Oolder
aIu tr3c Oolder m8i nh? #au:
protected
theme#
"ron% Oolder theme#:
theme#gnewtheme
theme#gnewthemegc##
theme#gnewthemegview#
theme#gnewthemegview#gla*out#
c@* %i- ch3n% ta # tiSn h:nh tha* /Qi< cop* to:n b6 Oile tron% dc##
chu*Fn #an% /?-n% dkn tron% theme m8i l: dtheme#dnewthemedc##d
"iSp theo cop* dprotecteddview#dla*out#dmain.php t8i /?-n% dkn theme
m8i: dtheme#dnewthemedview#dla*out#dmain.php
M:o protecteddconOi%dmain.php th2m /o5n khai b4o theme:
wnamewxywDevelopment Yii Proectw<
'theme'=>'newtheme'<
c5n v:o dtheme#dnewthemedview#dla*out#dmain.php v: chvnh l5i /o5n
khai b4o /?-n% dkn c## v: # l5i nh? #au:
yMM bl'eprint C[[ framework MM
link rel-stylesheet type-te+t/css href-,php echo Dii**appNOMre/'estM
base}rl] ,/themes/newtheme/css/screen.css media-screen, proLection /
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
link rel-stylesheet type-te+t/css href-,php echo Dii**appNOMre/'estM
base}rl] ,/themes/newtheme/css/print.css media-print /
yMMif lt {
link rel-stylesheet type-te+t/css href-,php echo Dii**appNOMre/'estM
base}rl] ,/ssthemes/newtheme/css/ie.css media-screen, proLection /
yendifMM
link rel-stylesheet type-te+t/css href-,php echo Dii**appNOMre/'estM
base}rl] ,/themes/newtheme/css/main.css /
link rel-stylesheet type-te+t/css href-,php echo Dii**appNOMre/'estM
base}rl] ,/themes/newtheme/css/form.css /
c5n v:o Oile main.php tron% theme#dnewthemedc##dmain.c## v: chvnh l5i
thu6c tVnh back%round tron% bod* l: m:u /[:
bod*

mar%in: 0
paddin%: 0
color:
Oont: normal 0pt Arial<Helvetica<#an##eriO
back%round: red

M:o http:ddlocalho#tddev*iid#itedlo%in 'em kSt )u\


L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Mzzz.. ahu*Fn web#ite #an% n%Cn n%A kh4c
1Dnh n%ha $ocale v: lan%ua%e
$ocale ev]n% mi;nf /?Rc thiSt l9p t^ /Dnh n%ha u#er lan%ua%e < )u=c
%ia<v: bIt kq %iao diJn n%?-i d]n% hiFn thD n6i dun% v7n c\nh cKa v]n% mi;n
/.< n%?-i ta th?-n% %Ei l: $ocale id vV d` : en+( l: nhAn% v]n% mi;n ti2u
chu}n cho n%?-i Yp n.i tiSn% Anh enited (tate#f < vi+MN th?-n% chv /Dnh
cho n%?-i viJt.
"ron% Yii dA liJu v]n% mi;n tr>nh ditn tron% alocale cla##< n. cun% cIp
v]n% mi;n ri2n% /?Rc th2m v:o hJ th=n% t^ n%?-i d]n%< nh? c4c kT tL< #=<
n%:* th4n%<time< /F tr>nh ditn v]n% mi;n ri2n% ta #_ d`n%
alocale::%etzn#tanceelocalezDf . (au /@* l: vV d` #_ d`n% v]n% mi;n en+(
tron% th:nh ph0n Zn% d`n%:
Yii::appefy%et$ocaleewen+u#wf
Yii c. thF l:m viJc v8i h0u hSt v8i v]n% mi;n n%Cn n%A kh4c nhau< /F
'em n%Cn n%A cKa b5n /?Rc hi trR ha* khCn% b5n v:o Orameworkdi&nddata
b5n # nh>n thI* Oile vi+vn.php d:nh cho viJt nam.
"rl l5i vV d` tr?8c nSu b5n mu=n 'em n%:* th4n% bjn% tiSn% Anh
enited (tate#f b5n chv c0n l:m nh? #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
locale x Yii::appefy%et$ocaleewen+u#wf
print+relocaleymonthName#f
~St )u\ l::
M8i ztalia:
locale x Yii::appefy%et$ocaleewitwf
print+relocaleymonthName#f
"It nhi2n tCi /?a ra vV d` cho b5n /F c4c b5n 'em chZ khCn% c0n
ph\i te#t th9t v> n. )u4 /Hn %i\n< nSu b5n mu=n te#t b5n chv c0n v:o Oile
view# bIt k> v: %G dsn% lJnh tr2n rhi v:o tr>nh du*Jt kiFm tra. "u* nhi2n b:i
t8i /@* th> tIt nhi2n l: b5n ph\i /6n% ta* ch@n
Mzzz.. "hLc hiJn chu*Fn dDch n%Cn n%A
"ron% /o5n tr?8c %i8i thiJu i&n ta c. thF thI* hE thiSt kS cun% cIp %0n nh?
/0* /K < Yii cun% cIp c\ me##a%e tran#lation echu*Fn dDch tin nhBnf theo kiFu
n%Bn %En dt d]n%< /Dnh d5n% te't theo kiFu tin nhBn< v: cu=i c]n% l: chu*Fn
dDch n%Cn n%A t^ Oile me##a%e lan%ua%e /..
Y6t me##a%e /?Rc khli t5o t^ ame##a%e#ource v: n. cun% cIp c4c thu6c
tVnh #au:
aphpYe##a%e(ource: 1@* l: me##a%e #ource mbc /Dnh< l-i nhBn c0n
chu*Fn dDch /?Rc l?u d?8i d5n% ke*xyvalue tron% m\n% php <mii cbp /i km
# /hn% th-i km theo #L chu*Fn dDch cKa n. theo v]n% mi;n c0n chu*Fn
a%ette't Ye##a%e(ource: $-i nhBn chu*Fn dDch /?Rc l?u d?8i d5n% uN
uette't nile
adbYe##a%e(ource: 1?Rc l?u tron% b\n% databa#e.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Y6t me##a%e # /?Rc load v:o th:nh ph0n Zn% d`n%<Yii # /Dnh n%ha n.
tron% th:nh ph0n Zn% d`n% v: tr\ v; me##a%e m: n. c0n /?Rc tr\ v;.cli
mbc /Dnh ch3n% ta #_ d`n% aphpYe##a%e(ource v: th:nh ph0n cH b\n #
ph\i njm tron% protecteddme##a%e#.
ah3n% ta # l:m m6t vV d` #au: chu*Fn dDch n%Cn n%A cKa Oorm Oield $o%in
#an% tiSn% Anh v: tiSn% MiJt.
ah3n% ta # 'em )ua nhrn elabelf thCn% tin cKa Oorm nh? #au:
En%li#h "iSn% MiJt
#ername "2n /7n% nh9p
Pa##word Y9t kh}u
{emember me ne't time Nh8 tCi l0n /7n% nh9p t8i
ah3n% ta # #_ d`n% aphpYe##a%e(ource /F chu*Fn dDch tin nhBn tr2n.
aCn% viJc /0u ti2n b5n c0n l:m l: t5o Oile PHP chZa /Ln% n6i dun% c0n
chu*Fn dDch. "Ci # l:m $ocalezD v8i t2n l: vi < ch3n% ta # %Ei danh m`c
mbc /Dnh cKa n. b@* %i-.
ah3n% ta # t5o Oile m8i d?8i th? m`c protecteddme##a%e#d theo cIu tr3c
d$ocalezddaate%or*Name.php
c5n v:o protecteddme##a%e#dviddeOault.php v: thLc hiJn /o5n #au:
ophp
return arra*e
w#ernamew xy w"2n /7n% nh9pw<
wPa##wordw xy wY9t kh}uw<
w{emember me ne't timew xy Nh8 tCi l0n /7n% nh9p t8i<
f
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ah3 T b5n /F mr #o5n th\o l: "n& nSu khCn% #au khi thLc hiJn # mr h.a
to:n kT tL l5.
"iSp theo ch3n% ta c0n thiSt l9p n%Cn n%A mbc /Dnh l: MiJt Nam< b5n v:o
protecteddconOi%dmain.php v: tha* /Qi /0u /o5n
iOe2xx0f

lan%ua%exwenw
el#e
lan%ua%exwviw
return arra*e
wla*outwxywnewla*outw<
wba#ePathwxydirnamee++nz$E++f.Dz{Ea"m{Y+(EPA{A"m{.w..w<
wnamewxywDevelopment Yii Proectw<
wlan%ua%ewxylan%ua%e<
c@* %i- cCn% viJc cu=i c]n% b5n c0n l:m l: %Ei h:m Yii::tef etef y"ran#latef
tron% Oorm Oield label $o%in< b5n hr* v:o protecteddmodel#d$o%innorm.php
">m h:m attribute$abel#ef tha* thS l5i nh? #au:
d||
| Declare# attribute label#.
|d
public Ounction attribute$abel#ef

return arra*e
wrememberYewxyYii::tewdeOaultw<w{emember me ne't timewf<
wu#ernamewxyYii::tewdeOaultw< w#ernamewf<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wpa##wordwxyYii::tewdeOaultw< wPa##wordwf<
f

M: /@* l: kSt )u\:


http:ddlocalho#tddev*iid#itedlo%in
a@u h[i tiSp theo l: l:m thS n:o khi b5n click v:o biFu t?Rn% c- En%li#h th>
Oile tL /6n% chu*Fn #an% tiSn% anh v: c- viJt nam th> n. # tr\ v; tiSn% viJt
nh? h>nh tr2n /3n% khCn% o
c5n chv c0n t5o th2m tron% protecteddme##a%e#d m6t Oolder en v: m6t Oile
php l: : deOault.php v: thLc hiJn * hJt nh? b?8c t5o Oile vi< khi c4c b5n t5o
hai l4 c- chu*Fn dDch b5n # t5o th2m m6t tr?-n% /F '4c /Dnh /@u l: en v:
/@u l: vi #au /. b5n tha* thS tron% protecteddconOi%dmain.php c. /o5n
2xx0 < nh? v9* l: to:n b6 n6i dun% # chu*Fn dDch.
aHNu ( D-Nu Yzz YmD$E
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
~ HmaH
"5o module m8i t^ admin
"5o kh\ n7n% admin c. thF th2m tin nhBn ml r6n% t^ hJ th=n% cho
Zn% d`n% n%?-i d]n%< hiFn thD t5i tran% danh #4ch proect
"h_ theme m8i t8i module
"5o b\n% c#dl m8i l?u c4c tin nhBn cKa hJ th=n%
"5o tIt c\ arud cho tin nhBn hJ th=n%
H5n chS tru* c9p tIt c\ c4c h:m kF c\ module m8i t^ admin
HiFn thD tin nhBn hJ th=n% tron% danh #4ch proect
zX. $:m viJc v8i module
Y6t module Yii # chZa rIt nhi;u miniZn% d`n% tron% /.<n. chZa /Ln% to:n
b6 cIu tr3c view#< model< controller< v: c\ c4c th:nh ph0n wid%et c0n hi trR<
"u* nhi2n module khCn% thF triFn khai b\n th@n ch3n% nh? m6t Zn% d`n%.
1F t5o m6t module vC c]n% /Hn %i\n ch3n% ta # #_ d`n% uii
http:ddlocalho#tddev*iid%iiddeOaultdinde'
c5n click v:o Yodule uenerator:
"5i tr?-n% Yodule zD b5n %G admin y previewy%enerated
c5n v:o protecteddmodule#d # thI* Oolder m8i hiJn ra c. t2n admin< /F #_
d`n% b5n cn% c0n ph\i v:o protecteddconOi%dmain.php v: khai b4o n. :
wmodule#wxyarra*e
w%iiwxyarra*e
wcla##wxyw#*#tem.%ii.uiiYodulew<
wpa##wordwxyww<
dd zO removed< uii deOault# to localho#t onl*. Edit careOull* to
ta#te.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wipnilter#wxyarra*ew2.0.0.w<w::wf<
f<
'admin',
f<
c@* %i- b5n c. thF v:o tr>nh du*Jt 'em kSt )u\:
http:ddlocalho#tddev*iidadminddeOaultdinde'
zX.2 Yodule la*out
Y6t thZ ch3n% ta # ph\i nh>n l5i l ch?Hn% tr?8c /. l: newtheme, n. cn%
c0n /?Rc 4p d`n% v:o module<lT do cho viJc n:* l: n%o:i module controller
cla## /?Rc kS th^a t^ protecteddcomponent#daontroller.php< nhAn% la*out
m8i nh? :la*outxddla*out#dcolumn.
Wuan trEn% l: dIu dd l phVa tr?8c la*out n:*< n. )u* /Dnh c` thF viJc ch3n%
ta #_ d`n% /?-n% dkn Zn% d`n% chVnh hHn #o v8i /?-n% dkn cKa m6t
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
module c` thF khi t>m kiSm t9p tin< v> thS ch3n% ta c0n 4p d`n% cho nhAn%
la*out n%o:i module /F Zn% d`n% cKa ch3n% ta dt l:m viJc hHn.
NSu ch3n% ta chv thLc hiJn m6t dIu %5ch chNo d tr?8c ph0n /Dnh n%ha t2n
la*out c0n t>m< ta # thI* module admin # khCn% /?Rc 4p d`n% tron% tIt c\
mEi nHi<lT do /i;u n:* l: do dIu d< khi b= trV dla*out#dcolumn n. # t>m
kiSm c4c t9p tin tron% module chZ khCn% ph\i njm tron% Zn% d`n% chVnh.
ah3n% ta # tha* /Qi /i;u n:* v8i viJc #_ d`n% module:
c5n c. thF cIu h>nh h0u hSt mEi thZ tron% module < vV d` th2m th:nh ph0n
la*out Oile< mbc /Dnh la*out cho module # njm tron% protecteddmodule#d
modulezddview#dla*out#< modulezd l /@* l: admin chn% h5n
ah3n% ta c. thF nh>n thI* l3c khli t5o # khCn% c. Oile n:* v> thS n2n module
# nh9n la*out mbc /Dnh t^ Zn% d`n%
zX.! "h_ m6t la*out
"r?8c ti2n b5n c0n v:o protecteddview#dla*out#dcolumn v: /Qi l5i nh? #au:
ophp thi#ybe%inaontentewddla*out#dmainwf oy
1Qi th:nh
ophp thi#ybe%inaontentewdla*out#dmainwf oy
10u ti2n hr* thiSt l9p la*out mbc /Dnh cho module< ch3n% ta # thLc hiJn bli
h:m initef tron% protecteddmodule#dadmindAdminYodule#.php
cla## AdminYodule e'tend# aWebYodule

public Ounction initef

dd thi# method i# called when the module i# bein% created


dd *ou ma* place code here to cu#tomie the module or the
application
dd import the modulelevel model# and component#
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
thi#y#etzmportearra*e
wadmin.model#.|w<
wadmin.component#.|w<
ff
$this->Iayout = 'main';

M8i c4ch n:* b5n khCn% c0n lo t8i la*out Oile mbc /Dnh tron% module nAa< m:
module # nh9n Oile mbc /Dnh %i=n% nh? cla## controller.php< csn nSu b5n
mu=n tha* /Qi b5n chv c0n t5o Oile m8i v: /Dnh n%ha la*out cho n. v: th2m
la*out /. tron% protecteddmodule#dadmindview#dla*out#.
"iSp theo b5n cop* 2 Oile protecteddview#dla*out#dmain.php v:
protecteddview#dla*out#dcolumn.php t^ Zn% d`n% chVnh< v: vZt n. v:o
protecteddmodule#dadmindview#dla*out#
(au /. b5n chvnh l5i column.php tron%
protecteddmodule#dadmindview#dla*out#dcolumn.php
X.a ph0n tham chiSu t8i main.php Oile chv csn:
<?php $this->beginContent(); ?>
div idxcontenty
ophp echo content oy
ddivy content y
ophp thi#yendaontentef oy
"iSp theo b5n v:o protecteddmodule#dadmindview#dla*out#dmain.php v: th2m
nhAn% /o5n in /9m #au
...
div cla##xcontainer idxpa%ey
div idxheadery
<div id="Iogo"><?php echo CHtmI::encode(Yii::app()->name) . "
Admin ConsoIe"; ?></div>
ddivy header y
div idxmainmenuy
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
ophp thi#ywid%etewii.wid%et#.aYenuw<arra*e
witem#wxyarra*e
array('IabeI'=>'Back To Main Site', 'urI'=>array('/project')),
array('IabeI'=>'Admin', 'urI'=>array('/admin/defauIt/index')),
arra*ewlabelwxyw$o%inw< wurlwxyarra*ewd#itedlo%inwf<
wvi#iblewxyYii::appefyu#eryi#uue#tf<
arra*ewlabelwxyw$o%out ew.Yii::appefyu#eryname.wfw<
wurlwxyarra*ewd#itedlo%outwf< wvi#iblewxyYii::appefyu#eryi#uue#tf
f<
ff oy
ddivy mainmenu y
M:o tr>nh du*Jt tL #?8n%:
http:ddlocalho#tddev*iidadminddeOaultdinde'
zX. H5n chS tru* c9p admin
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Y6t vIn /; b5n c. thF nh>n thI* l: bIt k> ai cn% c. thF tru* c9p /?Rc v:o
admin module<ch3n% ta # '@* dLn% #ao cho chv nhAn% u#er /?Rc cho phNp
m8i c. kh\ n7n% v:o admin module
c5n v:o protecteddmodule#dadmindAdminYodule::beOoreaontrollerActionef
method v: chvnh l5i nh? #au:
public Ounction beOoreaontrollerActionecontroller< actionf

iOeparent::beOoreaontrollerActionecontroller< actionff

dd thi# method i# called beOore an* module controller action i#


perOormed
dd *ou ma* place cu#tomied code here
if( !Yii::app()->user->checkAccess("admin") )
{
throw new CHttpException(403,Yii::t('appIication','You are
not authorized to perform this action.'));
}
return true;

el#e
return Oal#e

H9u )u\ l: : http:ddlocalho#tddev*iidadminddeOaultdinde'


L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"iSp theo ta # v:o dtheme#dnewthemedview#dla*out#dmain.php v: chvnh l5i
wid%et menu nh? #au:
div idxmainmenuy
ophp thi#ywid%etewii.wid%et#.aYenuw<arra*e
witem#wxyarra*e
arra*ewlabelwxywProect#w< wurlwxyarra*ewdproectwff<
arra*ewlabelwxywAboutw< wurlwxyarra*ewd#itedpa%ew<
wviewwxywaboutwff<
arra*ewlabelwxywaontactw< wurlwxyarra*ewd#itedcontactwff<
array('IabeI'=>'Admin', 'urI'=>array('/admin/defauIt/index'),
'visibIe'=>Yii::app()->user->checkAccess("admin")),
arra*ewlabelwxyw$o%inw< wurlwxyarra*ewd#itedlo%inwf<
wvi#iblewxyYii::appefyu#eryi#uue#tf<
arra*ewlabelwxyw$o%out ew.Yii::appefyu#eryname.wfw<
wurlwxyarra*ewd#itedlo%outwf< wvi#iblewxyYii::appefyu#eryi#uue#tf
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f<
ff oy
ddivy mainmenu y
{G r:n% khi b5n ch?a /7n% nh9p # khCn% hiJn link menu admin, nh?n% khi
b5n /7n% nh9p hJ th=n% th> hJ )u\ nh? #au:
http:ddlocalho#tddev*iidproect
zX. "h2m m6t tin nhBn ml r6n% t^ hJ th=n%
Y6t module c. thF chZa /Ln% nhAn% miniZn% d`n% v: b\n th@n n. cn% #
tru* nh9p t8i nhAn% h:m main application< ta hr* th2m m6t #= h:m m8i cho
admini#trator< ch3n% ta # th2m kh\ n7n% )u\n lT tin nhBn ml r6n% t^ hJ
th=n% khi hE m8i /7n% nh9p v:o web#ite.
zX.. "5o b\n% tron% a(D$
a{EA"E "Ac$E tbl+#*#+me##a%e
e
id zN"EuE{ Nm" N$$ P{zYA{Y ~EY A"m+zNa{EYEN"<
me##a%e "EX" Nm" N$$<
create+time DA"E"zYE<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
create+u#er+id zN"EuE{<
update+time DA"E"zYE<
update+u#er+id zN"EuE{
f
aop* v: pa#te cho nhanh< nh?n% nh8 c4c tr?-n% dA liJu.
zX..2 "5o a{D v: model cho b\n% m8i
http:ddlocalho#tddev*iid%iiddeOaultdinde' chEn ModeI Generator link< tiSp
theo t5i tr?-n% "able preOi' chEn tblY< t5i tr?-n% "able Name chEn :
tbl+#*#+me##a%e< t5i tr?-n% model cla## chEn : (*#Ye##a%e < t5i Yodel
Path chEn : application.module#.admin.model# ech3 T khCn% /F mbc /Dnhf
#au /. y chEn preview
y%enerated.
"iSp theo nhIn v:o a{D uenerator <t5i tr?-n% Yodelcla## chEn :
Application.module#.admin.model#.(*#Ye##a%e<
"5i controller+id : chEn admind(*#Ye##a%e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
(au /. nhIn previewy%enerated.
zX..! "h2m m6t link# t8i h:m chZc n7n% m8i
Hr* th2m menu item m8i v:o la*out main tron% module admin ph0n
navi%ation mainmenu. Yl Oile
protecteddmodule#dadmindview#dla*out#dmain.php
arra*ewlabelwxyw(*#tem Ye##a%e#w< wurlwxyarra*ewdadmind#*#Ye##a%ed
inde'wff<
(au /. b5n v:o tr>nh du*Jt 'em kSt )u\:
http:ddlocalho#tddev*iidadmind#*#Ye##a%edcreate
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
NSu hJ th=n% b4o lii khCn% hiJn ra b\n% n:* b5n v:o
module#dadmindcontroller#d#*#Ye##a%eaontroller# v: #_a khai b4o:
cla## (*#Ye##a%eaontroller e'tend# aontroller# th:nh
class [ysMessageController e+tends Controller
M: nh>n 'u=n% d?8i ph0n /Dnh n%ha la*out ta thI* :
la*outxddla*out#dcolumn2
ahVnh bli /i;u n:* m: Oront cKa admin vkn #_ d`n% la*out cKa h:m chVnh
%hm 2 c6t l: c6t chZa n6i dun% controller v: m6t c6t l: m6t wid%et menu
b2n ph\i< ch3n% ta # c]n% chvnh l5i la*out /F biSn n6i d`n% tron% controller
t^ m6t c6t th:nh hai c6t.a4ch thLc hiJn nh? #au:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
. aop* t^ protecteddview#dla*out#dcolumn2.php t8i
protecteddmodule#dadmindview#dla*out#dcolumn2.php
2. X.a ddla*out#dmain tron% ph0n /0u v:o cKa h:m be%inaontentef tron%
column2.php m8i cop* v;.
!. ahvnh l5i #*#Ye##a%e model kS th^a t^ DevYiiActive{ecord l /0u
/o5n khai b4o cla##
. ahvnh l5i (*#Ye##a%eaontroller #_ d`n% column2.php la*out Oile v8i
viJc /Dnh n%ha l5i khai b4o la*out l: :la*outxdla*out#dcolumn2
. (au khi ch3n% ta kS th^a DevYiiActive{ecord ch3n% ta # '.a b[ m6t
#= tr?-n% dA liJu khCn% c0n thiSt khi /?Rc tL /6n% t5o ra< vV d` '.a
/o5n #au tron% module#dadmindview#d#*#Ye##a%ed+Oorm.php
div cla##xrowy
ophp echo OormylabelE'emodel<wcreate+timewf oy
ophp echo Oormyte'tnieldemodel<wcreate+timewf oy
ophp echo Oormyerroremodel<wcreate+timewf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wcreate+u#er+idwf oy
ophp echo Oormyte'tnieldemodel<wcreate+u#er+idwf oy
ophp echo Oormyerroremodel<wcreate+u#er+idwf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wupdate+timewf oy
ophp echo Oormyte'tnieldemodel<wupdate+timewf oy
ophp echo Oormyerroremodel<wupdate+timewf oy
ddivy
div cla##xrowy
ophp echo OormylabelE'emodel<wupdate+u#er+idwf oy
ophp echo Oormyte'tnieldemodel<wupdate+u#er+idwf oy
ophp echo Oormyerroremodel<wupdate+u#er+idwf oy
ddivy
(au /. '.a b[ )u* tBc tron% (*#Ye##a%e::rule#ef A{:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
arra*ewcreate+u#er< update+u#erw< wnumericalw<
winte%ermnl*wxytruef< and arra*ewcreate+time< update+timew<
w#aOewf<
$0n tha* /Qi cu=i c]n% b5n v:o (*#Ye##a%eaontroller::acce##{ule#ef
public Ounction acce##{ule#ef

return arra*e
arra*ewalloww< dd allow onl* u#er# in the wadminw role acce## to
ddour action#
waction#wxyarra*ewinde'w<wvieww< wcreatew< wupdatew< wadminw<
wdeletewf<
wrole#wxyarra*ewadminwf<
f<
arra*ewden*w< dd den* all u#er#
wu#er#wxyarra*ew|wf<
f<
f

(au khi t hLc hiJn 'on% b5n v:o :


http:ddlocalho#tddev*iidadmind#*#Ye##a%edcreate
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
{G r:n% Oorm /r trl l2n /p hHn rIt nhi;u< v: b5n c. thF t5o m6t #= tin nhBn
ri2n% cho b5n n%a* l3c n:* el?u T #_ d`n% /F chu}n bD dA liJu te#t b\n %hi
c4c ph0n #auf.
zX.. HiFn thD tin nhBn t8i n%?-i d]n%
c@* %i- ch3n% ta c. m6t tin nhBn t^ hJ th=n% < hr* hiFn thD n. t8i n%?-i d]n%
tron% tran% chK.
"h2m model cla## cho Zn% d`n% tru* c9p
M:o protecteddconOi%dmain.php v: th2m v: model cKa module admin:
dd autoloadin% model and component cla##e#
wimportwxyarra*e
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
wapplication.model#.|w<
wapplication.component#.|w<
'appIication.moduIes.admin.modeIs.*',
f<
ahEn tin nhBn %0n nhIt /?Rc c9p nh9t
ah3n% ta # chv tr\ v; m6t tin nhBn %0n nhIt t^ hJ th=n%< nh- tr?-n% dA liJu
'pdateYtime tron% b\n%< #au /. ta th2m n. v:o danh #4ch proect. ah3n% ta
# chvnh #_a l5i Proectaontroller::actionznde'ef:
M:o protecteddcontrollerdProectaontroller.php #_a l5i actionznde' nh? #au:
public Ounction actionznde'ef

dataProviderxnew aActiveDataProviderewProectwf
Yii::appefyclient(criptyre%i#ter$ink"a%e
walternatew<
wapplicationdr##'mlw<
thi#ycreaterlewcommentdOeedwff
//get the Iatest system message to dispIay based on the update_
time coIumn
$sysMessage = SysMessage::modeI()->find(array(
'order'=>'t.update_time DESC',
));
if($sysMessage !== nuII)
$message = $sysMessage->message;
eIse
$message = nuII;
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
thi#yrenderewinde'w<arra*e
wdataProviderwxydataProvider<
'sysMessage'=>$message,
ff

c@* %i- ch3n% ta c0n chvnh view# Oile < v:o protecteddview#dproectdinde'.php
v: th2m v:o tr2n /0u /o5n hyProectdhy tron% view Oile
ophp iOe#*#Ye##a%e xx nullf:oy
div cla##x#*#me##a%ey
ophp echo #*#Ye##a%e oy
ddivy
ophp
Yii::appefyclient(criptyre%i#ter(cripte
wOadeAndHideEOOectw<
we.#*#me##a%ef.animateeopacit*: .0< 000f.
Oademute#lowfw
f
endiO
oy
(au khi ho:n th:nh 'on% ch3n% ta v:o tr>nh du*Jt tL #?8n%:
http:ddlocalho#tddev*iidproect
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"h2m a(( cho tin nhBn
c5n v:o theme#dnewthemedc##dmain.c## v: th2m v:o /o5n c## #au:
div.#*#me##a%e

paddin%:.&em
mar%inbottom:em
border:!p' #olid ddd
back%round:EEnnn
color:nn!!0A
bordercolor:00&E

H9u )u\ l:: http:ddlocalho#tddev*iidproect


L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
c5n ch- %i@* n. # tL /6n% mIt nh- )uer* animate.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
aHNu 0 aachin%
DA liJu b6 nh8 /Jm l: ph?Hn% thZc rIt t=t %i3p cho viJc ph`c v` hiJu n7n%
#_ d`n% cKa m6t Zn% d`n% web#ite.NSu nhAn% n6i dun% /bc biJt /?Rc *2u
c0u bli nhi;u n%?-i d]n% c]n% m6t l3c<#_ d`n% b6 nh8 /Jm /F l?u trA v:
ph`c v` n6i dun% tron% m6t th-i /iFm nhIt /Dnh /F lEc dA liJu v: tru* c9p dA
liJu.
Yii cun% cIp h0u hSt c4c ti2u chu}n cachin%<b5n c. thF bBt /0u cIu h>nh
cache v:o Zn% d`n% cKa b5n< nhAn% th:nh ph0n /. /;u /?Rc kS th^a t^
acache tron% *ii<cla## cH b\n cKa cache v8i c4c thiSt bD cache kh4c /?Rc
thLc thi.
Yii cun% cIp nhi;u ti2u chu}n cache kh4c nhau nhjm l?u trA dA liJu< #au /@*
l: danh #4ch c4c cache m: Yii cun% cIp thLc thi:
. amemcache: (_ d`n% PHP Yemcache e'ten#ion#
2. aapcaache: (_ d`n% PHP APa e'ten#ion#
!. aXaache: (_ d`n% PHP Xcache e'ten#ion#
. aEAcceleratoraache: (_ d`n% PHP Eaccelerator e'ten#ion#
. adbaache: (_ d`n% b\n% cH #l dA liJu /F l?u trA cache<bli mbc /Dnh
n. # t5o v: #_ d`n% (W$ite! datab#e d?8i d5n% th? m`c.c5n c. thF
#_ d`n% c4ch thZc n:* thCn% )ua khai b4o kSt n=i connectionzD
. aendDataaache: (_ d`n% Uend Data aache nh? m6t b6 nh8 /Jm
v^a ph\i
. aOileaache: (_ d`n% Oile l?u %iA dA liJu cache</@* l: c4ch thZc /F l?u
trA dA liJu l8n v: ml r6n% e%i=n% nh? m6t tran%f
&. adumm*aache: ui8i thiJu %iao diJn cache<nh?n% khCn% luCn thLc
hiJn cachin%<lT do cho #L thLc thi n:* l: c4c nh: ph4t tri;n khCn% hi
trR n. thLc hiJn cachin%<b5n chv c. thF te#tcode v8i c4c cache hRp lJ
kh4c
9. awinaache: awincache /?Rc thLc thi t^ th:nh ph0n Zn% d`n% cache
cH b\n tron% Wincache<thCn% tin nhi;u hHn b5n v:o
http:ddwww.ii#.netddownload# /F theo dGi
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
"It c\ nhAn% lo5i cache tr2n /;u ph\i kS th^a t^ th:nh ph0n acache
X. aIu h>nh cache
c5n v:o protecteddconOi%dmain.php v: cIu h>nh cache b5n mu=n #_ d`n% vV
d` v8i amemcache e$?u T rjn% #_ d`n% memcache th> apache cKa b5n ph\i
tVch hRp memcache rhi nhN<v:o phpinOo v: 'em thCn% #= < c4c cache kh4c
cn% t?Hn% tL ph\i tVch hRp v:o apachef /@* l: thCn% #= cKa tCi #au khi cIu
h>nh 'on% memcache:
arra*e
......
wcomponent#wxyarra*e
......
wcachewxyarra*e
wcla##wxyw#*#tem.cachin%.aYemaachew<
w#erver#wxyarra*e
arra*ewho#twxyw#erverw< wportwxy2!< wwei%htwxy0f<
arra*ewho#twxyw#erver2w< wportwxy2!< wwei%htwxy0f<
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
f<
f<
f<
f
"u* nhi2n /F %iA )uan hJ /Hn %i\n cho n%?-i /Ec tron% Zn% d`n% DevYii
ch3n% ta # #_ d`n% aOileaache l:m vV d`.
aOileaache cun% cIp cachin% cH b\n< khi #_ d`n% lo5i cache n:* mii dA liJu
thLc thi # /?Rc l?u tron% m6t Oile< v: mbc /Dnh n. # tr\ Oile v; /?-n% dkn
protecteddruntimedcache eb5n vui lsn% t5o Oolder cache tron% Oolder runtimef
< /F tha* /Qi cIu h>nh thiSt l9p b5n v:o protecteddconOi%dmain.php v: thLc
hiJn nh? #au:
dd application component#
wcomponent#wxyarra*e
P
'cache'=>array(
'cIass'=>'system.caching.CFiIeCache',
),
P
f<
M8i #L tha* /Qi n:* l mEi nHi tron% Zn% d`n% b5n c. thF %Ei n. bjn% :
Yii::appefycache.
X.2 (_ d`n% cache Oile
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
c@* %i- ch3n% ta # #_ d`n% Oile cache tron% th:nh ph0n Zn% d`n%< hr*
v:o protecteddmodule#dadmindmodel#d(*#Ye##a%e.php A{ model cla## /F
lEc tin nhBn cu=i c]n% tron% b\n %hi.
ah3n% ta # th2m h:m #au v:o model cla##:
d||
| {etrieve# the mo#t recent #*#tem me##a%e.
| ,return (*#Ye##a%e the A{ in#tance repre#entin% the late#t #*#tem
|me##a%e.
|d
public #tatic Ounction %et$ate#tef

dd#ee iO it i# in the cache< iO #o< u#t return it


iOe ecachexYii::appefycachefxxnullf

ke*xwDevYii.Proect$i#tin%.(*#temYe##a%ew
iOee#*#Ye##a%excachey%eteke*ffxxOal#ef
return #*#Ye##a%e

dd"he #*#tem me##a%e wa# either not Oound in the cache< or


ddthere i# no cache component deOined Oor the application
ddretrieve the #*#tem me##a%e Orom the databa#e
#*#Ye##a%e x (*#Ye##a%e::modelefyOindearra*e
worderwxywt.update+time DE(aw<
ff
iOe#*#Ye##a%e x nullf
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]

dda valid me##a%e wa# Oound. (tore it in cache Oor Outure


retrieval#
iOei##eteke*ff
cachey#eteke*<#*#Ye##a%e<!00f
return #*#Ye##a%e

el#e
return null

(au /. b5n v:o Proectaontroller::actionznde'ef tha* /Qi :


#*#Ye##a%e x (*#Ye##a%e::modelefyOindearra*eworderwxywt.update+time
DE(aw<ff
"h:nh /o5n:
#*#Ye##a%e x (*#Ye##a%e::%et$ate#tef
M:o l5i web#ite: http:ddlocalho#tddev*iidproect #au /. v:o
protecteddruntimedcache b5n # thI* nhAn% Oile cache /?Rc t5o ra:
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
X.! aache Dependencie#
"ham #= ph` thu6c cho phNp tha* thS kh4c v: phZc t5p hHn nhi;u
c4ch tiSp c9n /F )u*St /Dnh c. hobc khCn% c. c4c dA liJu /?Rc l?u trA tron%
b6 nh8 cache l:m m8i. HHn l: tu*2n b= m6t kho\n% th-i %ian /Hn %i\n cho
hSt th-i h5n l?u trA dA liJu< chiSn l?Rc b6 nh8 /Jm cKa b5n c. thF *2u c0u
rjn% c4c dA liJu trl n2n khCn% hRp lJ dLa tr2n nhAn% thZ nh? n%?-i #_
d`n% c` thF l:m theo *2u c0u< chS /6 chun%< tr5n% th4i cKa c4c
Zn% d`n%< hobc 'em m6t t9p tin tr2n hJ th=n% t9p tin /r /?Rc c9p nh9t %0n
/@*. 1i;u n:* cho phNp b5n /F '4c /Dnh c4c )u* tBc '4c nh9n b6 nh8 cache
"ham #= ph` thu6c /?Rc khli t5o t^ acacheDependenc* <*ii chIp nh9n c4c
thu6c tVnh #au:
aOileaacheDependenc*: dA liJu tron% cache # l: m6t Oile hRp lJ tha* /Qi th-i
%ian v: tha* /Qi t^ khi cache th?c hiJn
adirector*aacheDependenc*: ui=n% nh? Oile tr?8c nh?n% kiFm tra tIt c\ c4c
Oile v: /?-n% dkn con t8i /?-n% dkn ri2n%
aDbaacheDependenc*: dA liJu tron% b6 nh8 cache # khCn% c. hiJu lLc nSu
kSt )u\ tru* vIn m6t c@u lJnh (W$ )u* /Dnh /?Rc tha* /Qi kF t^ khi tra cZu
b6 nh8 cache tr?8c /..
aulobal(tateaacheDependenc*: a4c dA liJu tron% b6 nh8 cache # khCn%
hRp lJ nSu %i4 trD cKa tr5n% th4i public )u* /Dnh /?Rc tha* /Qi. Y6t tr5n% th4i
public l: m6t biSn li2n t`c tr2n nhi;u *2u c0u v: nhi;u phi2n tron% m6t Zn%
d`n%.
N. /?Rc /Dnh n%ha thCn% )ua aApplication :: #etulobal(tate ef.
aahainedaacheDependenc*: 1i;u n:* cho phNp b5n chuii l5i v8i nhau
nhi;u ph` thu6c. a4c dA liJu tron% b6 nh8 cache # trl th:nh khCn% hRp lJ
nSu c. cKa c4c ph` thu6c v:o chuii /?Rc tha* /Qi.
aE'pre##ionDependenc*: dA liJu tron% b6 nh8 cache # khCn% c. hiJu lLc
nSu kSt )u\ cKa biFu thZc PHP )u* /Dnh /?Rc tha* /Qi.
ah3n% ta # chvnh l5i Ounction %et$ate#tef tron% #*#Ye##a%e.php:
cachey#eteke*< #*#Ye##a%e< 0< new aDbaacheDependenc*ew(E$Ea"
YAXeupdate+timef n{mY tbl+#*#+me##a%ewff
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
X. Wuer* aachin%
c6 nh8 /Jm tru* vIn c4ch tiSp c9n b6 nh8 /Jm kSt )u\ cH #l dA liJu nh?
v9* l: th?-n% 'u*2n c0n thiSt tron% c4c Zn% d`n% cH #l dA liJu theo /Dnh
h?8n% m: Yii cun% cIp dt d:n% hHn tru* vIn b6 nh8 /Jm."ru* vIn b6 nh8
/Jm l?u trA kSt )u\ cKa m6t tru* vIn cH #l dA liJu tron% b6 nh8 cache v: tiSt
kiJm th-i %ian thLc hiJn tru* vIn tr2n c4c *2u c0u tiSp theo v> /@* l: nhAn%
ph`c v` trLc tiSp t^ b6 nh8 cache. 1F kVch ho5t m6t tru* vIn< b5n c0n ph\i
/\m b\o t:i #\n )uer*aachezD cKa t:i #\n aDbaonnection /; c9p /Sn zD
thu6c tVnh cKa m6t th:nh ph0n b6 nh8 cache hRp lJ. N. /; c9p /Sn wcachew
theo mbc /Dnh< /. l: nhAn% %> ch3n% tCi /r c. cIu h>nh t^ vV d` b6 nh8 cache
tr?8c.
1F #_ d`n% b6 nh8 /Jm tru* vIn< ch3n% tai chv /Hn %i\n l: %Ei b6 nh8
cacheef t^ ph?Hn% thZc aDbaonnection.
Ph?Hn% thZc n:* mIt tron% m6t th-i %ian< /F '4c /Dnh #= %i@* tru* vIn /?Rc
/F vkn csn tron% b6 nh8 cache. NSu th-i %ian /?Rc thiSt l9p l: 0< b6 nh8 /Jm
/?Rc vC hiJu h.a. c5n cn% c. thF chv /Dnh m6t tr?-n% hRp
aaacheDependenc* nh? l: m6t /=i #= thZ hai< v: '4c /Dnh c. bao nhi2u
tru* vIn tiSp theo n2n /?Rc l?u trA nh? l: m6t /=i #= thZ ba. 1=i #= thZ ba
n:* mbc /Dnh l: < c. n%ha l: chv c. c4c tru* vIn (W$ tiSp theo # /?Rc l?u
trA.
1F #_ d`n% )uer* cache ta v:o (*#temYe##a%e::%et$ate#tef thLc hiJn tha*
/Qi to:n b6 h:m l5i nh? #au:
ddu#e the )uer* cachin% approach
dependenc* x new aDbaacheDependenc*ew(E$Ea" YAXeupdate+timef
n{mY tbl+#*#+me##a%ewf
#*#Ye##a%e x (*#Ye##a%e::modelefycachee&00< dependenc*f
yOindearra*eworderwxywt.update+time DE(aw<ff
return #*#Ye##a%e
X. Ph@n m\n% aachin%
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
a4c vV d` tr?8c /@* chZn% minh viJc #_ d`n% c4c dA liJu b6 nh8 /Jm. 1@* l:
nHi m: ch3n% ta t5o ra m6t ph@n m\nh du* nhIt cKa dA liJu v: l?u trA n.
tron% b6 nh8 cache. a. nhi;u ph?Hn% ph4p kh4c c. #n
tron% Yii /F l?u trA c4c ph@n m\nh cKa c4c tran% /?Rc t5o ra bli m6t ph0n
cKa m6t kDch b\n 'em ha* th9m chV l: to:n b6 tran% chVnh n..
nra%ment b6 nh8 /Jm l: b6 nh8 /Jm m6t /o5n cKa m6t tran%. ah3n% ta c.
thF t9n d`n% lRi thS cKa m\nh b6 nh8 /Jm b2n tron% c4c t9p tin view. 1F l:m
nh? v9*< ch3n% ta #_ d`n% aaontroller :: be%inaache ef v:
aaontroller :: endaache ef.hai ph?Hn% ph4p /?Rc #_ d`n% /F /4nh dIu #L
bBt /0u v: kSt th3c cKa c4c tran% kSt 'uIt n6i dun% /?Rc l?u trA tron% b6 nh8
cache. an% nh? l: tr?-n% hRp khi #_ d`n% m6t b6 nh8 /Jm dA liJu
ph?Hn% ph4p tiSp c9n< ch3n% ta c0n m6t ch>a kh.a du* nhIt /F '4c /Dnh c4c
n6i dun% /?Rc l?u trA. Nh>n chun%<c3 ph4p /F #_ d`n% b6 nh8 /Jm m\nh
b2n tron% m6t kDch b\n 'em l: nh? #au:
......
o php
iO e thi#y be%inaache e idff

d d ... N6i dun% b5n mu=n /F cache l /@*


thi#y endaache ef

y
......
ce%inaache ef tr\ v; Oal#e khi c. m6t phi2n b\n l?u trA c. #n<
v: n6i dun% /?Rc l?u tron% b6 nh8 cache # /?Rc tL /6n% chn v:o t5i nHi
/.< nSu khCn%<n6i dun% b2n tron% /?Rc thLc hiJn v: # /?Rc l?u trA khi
endaache ef /?Rc %Ei.
X. 1Dnh n%ha thCn% #= ph@n m\nh cachin%
~hi %Ei be%inaache ef< ch3n% ta c. thF cun% cIp m6t m\n% nh? l: tham #=
thZ hai bao %hm b6 nh8 /Jm t]* chEn /F t]* chvnh b6 nh8 /Jm m\nh. Nh?
m6t vIn /; cKa thLc tS< be%inaache ef v: endaache ef ph?Hn% ph4p l: m6t
wrapper thu9n tiJn cKa amutputaache b6 lEc d wid%et. M> v9*< c4c t]* chEn b6
nh8 /Jm c. thF l: c4c %i4 trD ban /0u cho bIt kq thu6c tVnh n:o cKa l8p
amutputaache.
N%?-i ta c. thF cho rjn% m6t tron% nhAn% lLa chEn phQ biSn nhIt )u* /Dnh
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
khi b6 nh8 /Jm dA liJu th-i %ian< chv /Dnh /6 d:i n6i dun% c. thF vkn c. %i4 trD
tron% b6 nh8 cache.
1. l: t?Hn% tL nh? c4c thCn% #= th-i %ian ch3n% tCi #_ d`n% khi b6 nh8 /Jm
thCn% b4o hJ th=n% cKa ch3n% tCi. "a c. thF chv /Dnh tham #= th-i %ian khi
%Ei be%inaache ef nh? #au:
thi#y be%inaache e ke*< arra* edurationwxy !00ff
a:i /bt mbc /Dnh cho c4ch tiSp c9n n:* ph@n m\nh b6 nh8 /Jm l: kh4c nhau
hHn #o v8i dA liJu b6 nh8 /Jm. NSu ch3n% ta khCn% thiSt l9p th-i %ian n.
mbc /Dnh l: 0 %i@*< c. n%ha l: n6i dun% /?Rc l?u tron% b6 nh8 cache # hSt
hiJu lLc #au 0 %i@*. a. rIt nhi;u lLa chEn kh4c b5n c. thF thiSt l9p khi #_
d`n% b6 nh8 /Jm. 1F biSt th2m thCn% tin< hr* tham kh\o c4c APz
t:i liJu h?8n% dkn cho amutputaache cn% nh? m\nh ph0n b6 nh8 /Jm cKa
c4c Yii http:ddwww.*iiOramework.comddocd%uided.dendcachin%.Ora%ment
X. (_ d`n% ph@n m\nh cache
Hr* thLc hiJn /i;u n:* tron% Zn% d`n% DevYii. ah3n% ta m6t l0n nAa # t9p
trun% v:o li#t proect. b5n c. thF nh8 l5i< phVa d?8i c]n% cKa tran% li#t proect
c. m6t li#t comment /r /?Rc n%?-i d]n% comment v8i vIn /; li2n )uan
t^n% dL 4n. Danh #4ch n:* chv cho thI* nhAn% n%?-i csn l5i m6t b>nh lu9n
v; vIn /; n:o. "ha* v> hhi ph`c danh #4ch n:* mii khi c. *2u c0u< ch3n% ta
hr* #_ d`n% m\nh b6 nh8 /Jm cache danh #4ch n:* hai ph3t. a4c Zn% d`n%
c. thF chDu /Ln% c4c dA liJu n:* hHi c< v: hai ph3t l: thLc #L khCn% ph\i l:
d:i ph\i ch- /Ri cho m6t danh #4ch b>nh lu9n /?Rc c9p nh9t.
M:o protecteddview#dproectdinde'.php:
ophp
ke*xDevYii.Proect$i#tin%.{ecentaomment#
iOethi#ybe%inaacheeke*<arra*ewdurationwxy20fff

thi#ybe%inWid%etewii.wid%et#.aPortletw< arra*ewtitlewxyw{ecentaomment#w<ff
thi#ywid%etew{ecentaomment#wf
thi#yendWid%etef
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
thi#yendaacheef

oy
X.& Pa%e aachin%
N%o:i b6 nh8 /Jm m\nh< Yii cun% cIp c4c t]* chEn /F cache c4c kSt )u\ cKa
*2u c0u to:n b6 tran%. a4ch tiSp c9n b6 nh8 /Jm tran% t?Hn% tL nh? c4c b6
nh8 /Jm m\nh. "u* nhi2n< bli v> n6i dun% cKa to:n b6 m6t tran% th?-n%
/?Rc t5o ra /F 'em< ch3n% ta c. thF khCn% chv /Hn %i\n %Ei be%inaache ef v:
endaache ef tron% Oile. $T do l: n. /?Rc 4p d`n% tron% cu6c %Ei
/F aaontroller :: render ef ph?Hn% ph4p #au khi 'em n6i dun%. M> v9*< ch3n%
ta # luCn luCn b[ l cH h6i /F lI* n6i dun% t^ b6 nh8 cache.
M> v9*< /F cache to:n b6 tran%< ch3n% ta ho:n to:n n2n b[ )ua viJc thLc hiJn
c4c h:nh /6n% t5o ra c4c n6i dun% tran%. 1F thLc hiJn /i;u n:*< ch3n% ta c.
thF #_ d`n% l8p aoutputaache nh? l: m6t b6 lEc h:nh /6n% tron% l8p /i;u
khiFn .
MV d`< ch3n% ta hr* #_ d`n% c4ch tiSp c9n b6 nh8 /Jm tran% b6 nh8 cache
c4c tran% kSt )u\ cho mii tran% chi tiSt proect . "ran% chi tiSt proect tron%
DevYii /?Rc tr\ l5i bjn% re)ue#t: {$ http:ddlocalho#tddev*iidproectdviewdidd
id< tron% /. id l: zD dL 4n c` thF ch3n% ta /an% *2u c0u chi tiSt . NhAn% %>
ch3n% ta mu=n l:m l: thiSt l9p m6t b6 lEc b6 nh8 /Jm # cache to:n b6 n6i
dun% cKa tran% n:* m6t c4ch ri2n% biJt cho mii zD *2u c0u. ah3n% ta c0n
ph\i kSt hRp zD dL 4n v:o %i4 trD b6 nh8 cache. 1. l:< ch3n% ta khCn% mu=n
l:m cho m6t *2u c0u cho c4c chi tiSt cKa dL 4n v: c. c4c Zn% d`n% tr\ v;
m6t kSt )u\ /?Rc l?u tron% b6 nh8 cache cho dL 4n 2. a4c b6 lEc
aoutputaache cho phNp ch3n% ta l:m /i;u /..
Yl protecteddcontroller#dProectaontroller.php v: chvnh l5i Oilter#ef
public Ounction Oilter#ef

return arra*e
wacce##aontrolw< dd perOorm acce## control Oor a{D operation#
array(
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
'COutputCache + view', //cache the entire output from the
actionView() method for 2 minutes
'duration'=>120,
'varyByParam'=>array('id'),
),
f

aIu h>nh b6 lEc n:* #_ d`n% c4c b6 lEc amutputaache cache to:n b6 #\n
l?Rn%
/?Rc t5o ra bli c4c Zn% d`n% t^ m6t cu6c %Ei /Sn Proectaontroller ::
ActionMiew ef.
"hCn% #= 'em th2m n%a* #au khi t- khai amutputaache< nh? b5n c. thF
%Ei l5i< l: c4ch ti2u chu}n ch3n% tCi bao %hm c4c ph?Hn% ph4p h:nh /6n% c`
thF m: m6t b6 lEc
n2n 4p d`n%. "ham #= th-i %ian )u* /Dnh ""$ l: 20 %i@* e2 ph3tf<
#au /. c4c n6i dun% tran% # /?Rc t4i #inh.
a4c cIu h>nh var*c*Param l: m6t lLa chEn thLc #L tu*Jt v-i m: ch3n% tCi
4m chv /Sn tr?8c.
"ha* v> /bt tr4ch nhiJm l2n b5n< c4c nh: ph4t triFn< /Sn v8i m6t
chiSn l?Rc )uan trEn% du* nhIt cho c4c n6i dun% /?Rc l?u trA< tVnh n7n% n:*
cho phNp #L tha* /Qi
/?Rc '_ lT tL /6n%. MV d`< tron% tr?-n% hRp n:*< bjn% c4ch '4c /Dnh m6t
danh #4ch t2n
t?Hn% Zn% /F uE" c4c thCn% #= tron% *2u c0u /0u v:o. M> ch3n% tCi l: b6
nh8 /Jm
tran% n6i dun% *2u c0u /=i v8i c4c dL 4n do proect+id< n. l:m cho c\m %i4c
ho:n h\o /F #_ d`n%
zD n:* nh? l: m6t ph0n cKa thS hJ ch>a kh.a du* nhIt cho b6 nh8 /Jm n6i
dun%. cjn% c4ch '4c /Dnh
wMar*c*Paramw xy arra* ewidwf< amutputaache thLc hiJn /i;u n:* cho ch3n% ta
dLa tr2n input
Wuer*(trin% id. a. nhi;u lLa chEn c. #n /F /5t /?Rc /i;u n:* lo5i
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Autoaontent #L tha* /Qi chiSn l?Rc khi #_ d`n% amutputaache b6 nh8 cache
dA liJu cKa ch3n% ta. Yii ..2 c. #n c4c tVnh n7n% #au
var*c*{oute: cjn% c4ch thiSt l9p t]* chEn n:* l: true< c4c re)ue#t route c`
thF
/?Rc /?a v:o /Dnh danh du* nhIt cho c4c dA liJu /?Rc l?u tron% b6 nh8
cache. M> v9*< b5n
c. thF #_ d`n% #L kSt hRp cKa b6 /i;u khiFn /?Rc *2u c0u v: h:nh /6n% /F
ph@n biJt
l?u trA n6i dun%.
var*c*(e##ion: cjn% c4ch thiSt l9p t]* chEn n:* /F th9t #L< m6t #e##ion zD
du* nhIt /?Rc #_ d`n%
/F ph@n biJt c4c n6i dun% tron% b6 nh8 cache. Yii phi2n n%?-i d]n% c. thF
'em #L kh4c nhau
n6i dun%< nh?n% tIt c\ c4c n6i dun% n:* vkn c. thF /?Rc ph`c v` t^ b6 nh8
cache.
var*c*Param: Nh? /r th\o lu9n tr?8c /.< /i;u n:* #_ d`n% c4c /0u v:o
uE" chuii tru* vIn
c4c thCn% #= /F ph@n biJt c4c n6i dun% tron% b6 nh8 cache.
var*c*E'pre##ion: cjn% c4ch thiSt l9p t]* chEn n:* /F m6t biFu thZc PHP<
ch3n% ta c. thF #_ d`n%
kSt )u\ cKa biFu thZc n:* /F ph@n biJt c4c n6i dun% tron% b6 nh8 cache.
M> v9*< v8i c4c b6 lEc tr2n cIu h>nh tron% l8p Proectaontroller cKa ch3n% tCi<
mii *2u c0u
cho m6t tran% c` thF chi tiSt dL 4n /?Rc l?u trA tron% b6 nh8 cache tron% hai
ph3t tr?8c khi /?Rc
t4i #inh v: m6t l0n nAa /?Rc l?u trA tron% b6 nh8 cache. c5n c. thF kiFm tra
/i;u n:* bjn% c4ch 'em dL 4n c` thF< #au /. c9p nh9t dL 4n. a9p nh9t cKa
b5n # khCn% /?Rc n%a* l9p tZc hiFn thD nSu /?Rc thLc hiJn tron% th-i %ian
th-i %ian b6 nh8 cache hai ph3t.
c6 nh8 /Jm kSt )u\ to:n b6 tran% l: m6t c4ch tu*Jt v-i /F c\i thiJn hiJu #uIt
tran% web< tu* nhi2n
chBc chBn n. khCn% c. T n%ha cho mii tran% tron% tIt c\ c4c Zn% d`n% /?Rc
l?u trA.
N%a* c\ tron% vV d` cKa ch3n% ta< b6 nh8 /Jm to:n b6 tran% cho tran% chi tiSt
dL 4n khCn%
cho phNp ch3n% ta #_ d`n% /3n% pa%ination thLc hiJn cho danh #4ch c4c
i##ue. ah3n% ta
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
#_ d`n% /i;u n:* nh? l: m6t vV d` nhanh ch.n% l:m thS n:o /F thLc hiJn b6
nh8 /Jm tran%< nh?n% n. khCn% ph\i l: c4ch tiSp c9n /3n% tron% mEi tr?-n%
hRp. Y6t #L kSt hRp cKa ba ph?Hn% ph4p tiSp c9n< dA liJu< m\nh< v: b6 nh8
/Jm tran%< cho phNp b5n /i;u chvnh chiSn l?Rc b6 nh8 /Jm cKa b5n /F /4p
Zn% *2u c0u Zn% d`n%. ah3n% ta /r thLc #L chv kh4m ph4 )ua tIt c\ c4c b6
nh8 /Jm t]* chEn c. #n tron% Yii. H* vEn% rjn% /i;u n:* /r kVch thVch #L
hEc h[i cKa b5n /F tiSp t`c
t>m hiFu th2m to:n b6 nh8 /Jm c. #n.
GeneraI performance tuning tips
A# *ou are preparin% *our application Oor production< there are a Oew other
thin%#
to take into con#ideration. "he Oollowin% #ection# brieOl* outline #ome other
area# oO
con#ideration when workin% to tweak the perOormance oO a Yiiba#ed web
application.
Using APC
Enablin% the PHP APa e'ten#ion i# perhap# the ea#ie#t wa* to improve the
overall perOormance oO an application. "he e'ten#ion cache# and optimie#
PHP
intermediate code and avoid# the time #pent in par#in% PHP #cript# Oor ever*
incomin% re)ue#t.
zt al#o provide# a ver* Oa#t #tora%e mechani#m Oor cached content. With APa
enabled< *ou can u#e the aApcaache implementation Oor cachin% content<
Ora%ment#<
and pa%e#.
DisabIing the debug mode
We di#cu##ed the debu% mode earlier in the chapter< but it wonwt hurt to hear it
a%ain.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
Di#ablin% debu% mode i# another ea#* wa* to improve perOormance and
#ecurit*. A
Yii application run# in debu% mode iO the con#tant Yzz+DEcu i# deOined a#
true in
the main inde'.php entr* #cript. Yan* component#< includin% tho#e down in
the
Oramework it#elO< incur e'tra overhead when runnin% in the debu% mode.
Al#o< a# wa# mentioned wa* back in Chapter 6< ~etting [tarted< when we Oir#t
created
a Yii application< mo#t oO *our Yii application Oile# do not need to be< nor
#hould the*
be< in the publicall* acce##ible web director*. A Yii application ha# u#t one
entr*
#cript< which i# oOten the onl* Oile that need# to be placed in the web director*.
mther
PHP #cript#< includin% all oO the Yii Oramework Oile#< #hould be protected. "hi#
i# wh*
the deOault name oO the primar* application director* i# called protectedd. "o
avoid
#ecurit* i##ue#< it i# recommended to keep it Orom bein% publicl* acce##ible.
Using yiiIite.php
When the PHP APa e'ten#ion i# enabled< one can replace *ii.php with a
diOOerent Yii boot#trap Oile named *iilite.php. "hi# can help to Ourther boo#t the
perOormance oO a Yiipowered application. "he Oile *iilite.php come# with ever*
Yii relea#e. zt i# the re#ult oO mer%in% #ome commonl* u#ed Yii cla## Oile#.
coth
comment# and trace #tatement# are #tripped Orom the mer%ed Oile. "hereOore<
u#in%
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
*iilite.php would reduce the number oO Oile# bein% included and avoid
e'ecution
oO trace #tatement#.
Note that u#in% *iilite.php without APa ma* actuall* reduce
perOormance. "hi# i# becau#e *iilite.php contain# #ome cla##e# that
are not nece##aril* u#ed in ever* re)ue#t and would take e'tra par#in%
time. zt i# al#o ob#erved that u#in% *iilite.php i# #lower with #ome
#erver conOi%uration# even when APa i# turned on. "he be#t wa* to
ud%e whether to u#e *iilite.php or not i# to run a benchmark u#in%
the Hello World demo that i# included in the code bundle.
Using caching techniques
A# we de#cribed and demon#trated in thi# chapter< Yii provide# man* cachin%
#olution# that ma* improve the perOormance oO a web application #i%niOicantl*.
zO the
%eneration oO #ome data take# a lon% time< we can u#e the data cachin%
approach to
reduce the data %eneration Ore)uenc* iO a portion oO pa%e remain# relativel*
#tatic<
we can u#e the Ora%ment cachin% approach to reduce it# renderin% Ore)uenc*
iO a
whole pa%e remain# relative #tatic< we can u#e the pa%e cachin% approach to
#ave the
renderin% co#t Oor the whole pa%e.
EnabIing schema caching
zO the application i# u#in% Active Record eARf< *ou can turn on the #chema
cachin%
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
in a production environment to #ave the time oO par#in% databa#e #chema.
"hi# can
be done b* conOi%urin% the aDbaonnection::#chemaaachin%Duration propert*
to
be a value %reater than ero.
ce#ide# the#e applicationlevel cachin% techni)ue#< we can al#o u#e #erver
#ide
cachin% #olution# to boo#t the applicationw# perOormance. "he enablin% oO APa
cachin% that we de#cribed here< belon%# to thi# cate%or*. "here are other
#erver#ide
techni)ue#< #uch a# Uend mptimier< eAccelerator< and ()uid< u#t to name a
Oew.
rod'ction eadiness
[ 304 ]
"he#e< Oor the mo#t part< u#t provide #ome %ood practice %uideline# a# *ou
work to
prepare *our Yii application Oor production or trouble#hoot an e'i#tin%
application
Oor bottleneck#. ueneral application perOormance tunin% i# much more oO an
art
than a #cience< and there are man*< man* Oactor# out#ide oO the Yii Oramework
that
pla* into the overall perOormance. Yii ha# been built with perOormance in mind
#ince it# inception and continue# to outperOorm man* other PHPba#ed
application
development Oramework# b* a lon% #hot e#ee http:ddwww.*iiOramework.comd
perOormanced Oor more detail#f. mO cour#e< ever* #in%le web application will
need to
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23
January 1, 2013
[WEB.APPLlCATlON.DEVELOPMENT.WlTH.Yll.A
ND.PHP 2ND EDlTlON]
be tweaked to enhance the perOormance< but makin% Yii the development
Oramework
oO choice certainl* put# *our application on a %reat perOormance Oootin% Orom
the on#et.
nor Ourther detail#< #ee the erformance T'ning #ection oO the Yii deOinitive
%uide at
http:ddwww.*iiOramework.comddocd%uided.dendtopic#.perOormance.
L o n g t 8 x - P h a m d u c b a c t l k l 0 @g m a i l . c o m Page l23

You might also like