Professional Documents
Culture Documents
[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
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');
}
...
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
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
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
...
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
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
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
/@* 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
return arra*e
#elO::("A"(+Nm"YE"<
#elO::("A"(+YE"<
#elO::("A"(+nzNA$<
f
u#er#Arra*xaHtml::li#tDataethi#yu#er#<widw<wu#ernamewf
return u#er#Arra*
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
modelxnew z##ueew#earchwf
iOei##ete+uE"wz##uewff
modelyattribute#x+uE"wz##uew
$modeI->project_id = $this->_project->id;
thi#yrenderewadminw<arra*ewmodelwxymodelff
/@* 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
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
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
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*
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
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
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
return thi#yha#hPa##wordepa##wordfxxxthi#ypa##word
command x Yii::appefydbycreateaommandef
commandyin#ertewtbl+proect+u#er+a##i%nmentw< arra*e
wrolewxyrole<
wu#er+idwxyu#erzd<
wproect+idwxythi#yid<
ff
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
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
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
iOethi#yha#Error#eff
u#er x #er::modelefyOindc*Attribute#
earra*ewu#ernamewxythi#yu#ernameff
iOethi#yproectyi##erznProecteu#erff
thi#y+u#er x u#er
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
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]
proect x thi#yloadYodeleidf
iOeYii::appefyu#erycheckAcce##ewcreate#erw<
arra*ewproectwxyproectfff
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
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
...
public Ounction #cope#ef
return arra*e
wrecentwxyarra*e
worderwxywcreate+time DE(aw<
wlimitwxy<
f<
f
...
thi#y%etDbariteriaefymer%eWithe
arra*e
worderwxywt.create+time DE(aw<
wlimitwxylimit<
f
f
return thi#
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]
i##uexthi#yloadYodeleid< truef
....
return #elO::modelefywithewi##uewf
yOindAllearra*eworderwxywt.create+time DE(aw<wlimitwxylimit<ff
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\:
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
array('aIIow',
// aIIow aII users to perform 'index' and 'view' actions
'actions'=>array('index','view','feed','maiI'),
'users'=>array('*'),
),
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
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
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
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
el#e
return Oal#e
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
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
ke*xwDevYii.Proect$i#tin%.(*#temYe##a%ew
iOee#*#Ye##a%excachey%eteke*ffxxOal#ef
return #*#Ye##a%e
el#e
return null
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