Web Application Development With Yii and PHP Nov2012 Vietnamese

Embed Size (px)

Citation preview

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    1/206

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    2/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Phn : i /t cIu h>nh *ii Oramework5n vo tran% chK : http:www.*iiOramework.comdownload

    Download phin b\n m8i nhIt v; #au / %i\i nn e'tract heref< #au / rename l5i lcookbook< cop* Oolder cookbook m8i %i\i nn vo :g'amppghtdoc#g< b5n ph\i down'ampp v; ri ci /t #etup thnh cCn% th> ch5* 'ampp center thiSt l9p apache vm*#)l runnin%.

    htt,)o"a)ho/ta,,0((1*!(1*m*nt/(n*x.,h,

    Phn m;m "h50 )o"a) 3am,,4 Wam,4 %o n5n 6ng a,,/*17 7 #)(9 ng!05n nh:n mnh %o#(St4 t=t nhIt n5n t1;nhappyii!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 ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://www.yiiframework.com/download/http://localhost/appyii/requirements/index.phphttp://www.yiiframework.com/download/http://localhost/appyii/requirements/index.php
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    3/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    '( tip cd framewrk !" g yiic we#app de$yii%

    )d"%yii*+> Tn ng dng

    Cr"at" a -"b appication und"r /-"brootd"%yii )Y"s&o*Y"s)y*

    V90 )= %h!ng x?ng "Ka Y(( @ @?R" t5o 1a th=nh ">ng

    T1"h n*70((

    (n*x.,h, ()* Zng `ng

    (n*x-t*/t.,h, ()* %(Fm t1a "hZ" nJng

    a//*t/ "hZa t=( ng!05n Zng `ng

    "// "hZa C

    (mag*/ "hZa hnh \nh

    th*m*/ "hZa g(ao (Jn

    ,1ot*"t* "hZa ";" ()* @?R" #\o 7J

    Mo app*iiOramework chu*Fn Oolder m8i t5o l dev*ii ra n%oi Oolder web#ite:'ampphtdoc#. 1?-n% dn m8i cKa app l :'ampphtdoc#dev*ii.

    "o,0 o)*1 1am*o1% "Ka app*ii #an% dev*ii.

    V=o ()* (n*x.,h, tha0 @Q( @?-ng n th=nh $yii=dirname(__FILE__).'/framework/yii.php';

    B5n "? thFt1!0 "9, 7=o Zng `ng t^t1nh !0Jt

    htt,)o"a)ho/t*70(((n*x.,h,

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.phphttp://localhost/devyii/index.php
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    4/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    k< hon tIt cIu h>nh /F tiSn hnh coder.

    . T o contro"r-V=o th m " ng ng

    M%cd /Weroo!/de"yii

    C

    C Cxam,,hto"/*70((

    -h ( @ ng Y(( h*))

    M

    %#iioo!/framework/yiic he&&

    C Cxam,,hto"/*70((

    Cxam,,hto"/*70(( Cxam,,hto"/*70((1am*o1%0((" /h*))

    T o th "ont1o))*1 t5n m*//ag* "? h=nh @ ng )= h*))oWo1) CK ,h;, "ont1o))*1 S"ont1o))*1-ID [a"t(on-ID]

    VD

    M

    con!ro&&er meae he&&oWor&d

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    5/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    -M*//ag*Cont1o))*1 @ @ " t o th=nh ">ng th m " ,1ot*"t*"ont1o))*1/

    -B5n " nh @? n? "ng t o 1a 7(* t ( th m " ,1ot*"t*7(*/m*//ag* -ChKng ta "ng "? th t1!0 " , 7=o h=nh @ ng h*))oWo1) t t1nh !0 t

    D*a!)t htt,)o"a)ho/t*70(((n*x.,h,U1m*//ag* - "h 0 7=o (n*x

    htt,)o"a)ho/t*70(((n*x.,h,U1m*//ag*h*))oWo1)

    4. T o in $ t -Y(( "? h t1 / n h=m t o )(5n % t t1ong ")a// CHtm) -VD

    T o )(5n % t t ( V(* goo#0*

    M

    p*php echo +,!m&--&ink(oodye0array('meae/oodye')); */p

    T o )(5n % t t ( V(* H*))o

    M

    p*php echo +,!m&--&ink(,e&&o0array('meae/he&&oWor&d')); */p

    5.C6i 7 t databas"1.Yii h tr c8c daabas" sau: -M0L X.$ o1 )at*1

    -Po/tg1*/L G.+ o1 )at*1

    -L(t* 2 an +

    -M("1o/ot L *17*1 2&&& o1 )at*1

    -O1a")*

    2. 9 t n i % i databas" -Th " h( n ()* ,1ot*"t*"on(gma(n.,h, -M " @ nh n? @ang %"h ho t @Rng n=0 )= ata#a/* " a %h!ng x ng Y((

    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 ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message/helloWorldhttp://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message/helloWorld
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    6/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    - ( / ta "? $ # ng t1ong M0/) nh /a!

    M

    +E34E 435LE IF 674 E8I141 9!&_:er9 ( 9id9 in!() 674 6?) 674 6

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    7/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    M

    // :ncommen! !he fo&&owin !o ena&e !he ii !oo&

    'ii'=array( 'c&a'='y!em.ii.iiod:&e'0 'paword'=''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

    - _ ( ,a//o1 m ( @ @Jng nh , (( - V=o (( th>ng !a !1)

    htt, )o"a)ho/t*70(((n*x.,h,U1g((

    2. T o us"r ?od"-V=o m " Mo*) *n*1ato1 @ t o Mo*) "ho # ng !/*1 - Ta#)* P1*(x 7( t ,h n t( n t " a # ng 9n ! "?< 7=o @:04 @:0 # ng " a ta t5n )= t#)`!/*1 n5n t( n t / )= t#)` - Ta#)* Nam* ,h n "Rn ) ( " a t5n # ng )= !/*1 9ta "? th t o t @ ng t t " # ng # ng ";"h nh , ! 7=o @:0ng 7( t hoa

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    8/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    XII CC TRNG D LIU TRONG FORM

    bth(/-,ag*T(t)*cTha0 @ ( t(5! @ t1ang t ( @:0c

    C8ch 1:

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    9/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    'Ly code mXi'0 'c&icka&eImae' =!r:e0

    'imae7p!ion' = array('id' = 'cap!chaim') )); *

    B: )C n* T o !orm trong %i"#

    3*m th5m ";" M*tho @ a ";" ^(*) %h;" "ho o1m H*1*Ngo=( ";"h 6ng ZCA"t(7*^o1mZ ta "? th 6ng ZChtm)Z t o o1m CHtm)

    V( t L ( o1m t15n # ng ";"h 6ng ZCHtm)Z

    M PHP

    di" id='form'*php

    echo +,!m&--einForm();* 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@awordFie&d($mode&0'pa')* /di" di" id='row' *php echo +,!m&--ac!i"eLae&($mode&0'repa');* r/

    *php echo +,!m&--ac!i"e@awordFie&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'='frme'0'name'='frme'));*

    /di"*php

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://www.yiiframework.com/doc/api/1.1/CActiveFormhttp://www.yiiframework.com/doc/api/1.1/CHtmlhttp://www.yiiframework.com/doc/api/1.1/CActiveFormhttp://www.yiiframework.com/doc/api/1.1/CHtml
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    10/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    echo +,!m&--endForm();*/di"

    a! %h( t o o1m xong th t ( # " t( , th*o )= a ";" 1=ng #! " "ho th>ng t(n m= !/*1 nh , 7=o 7 nh

    C;" t*xt #ox %h>ng @ " # t1 ng Ema() ,h=( @Kng ng

    Pa// ,h ( ) n h n d nh h n $\

    Q*,a// ,h=( 0 "hang Pa//

    Dat* ,h=( h , ) th*o %( ! 9-mm-0000< ha0 9mm--0000ng ";( @. htt,)o"a)ho/t*70(((n*x.,h,U1/(t*"a,t"han ! 1a nh 7 0 )= o%

    -N ! n? %h>ng h( n "a,t"ha th ta n5n "h*"% ) ( x*m @ )oa g2 ha0 "h a # ng ";"h "h 0 m " t*/t " a Y((

    g( ng nh hnh )= )oa 1 (4 n? 7=ng ha0 @ th x*m ) ( " ! hnh *#/*17*1 !

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=site/captchahttp://localhost/devyii/index.php?r=site/captcha
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    11/206

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    12/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Z#a"%Co)o1Z m=! n n nh e )= m=! @*n4 Zo1*Co)o1Z m=! "h "a,t"ha.

    Zh*(ghtZ "h( ! "ao =nh Z(thZ Ch( ! 1 ng

    ZmaxL*ngthZ "h( ! =( t ( a " a "h "a,t"ha

    Zm(nL*ngthZ @ =( t ( th( ! " a /t1(ng.

    Z7*1(0Co*Z ) 0 g(; t1 m x;" nh n

    C! ( "6ng 1a ";( o1m nh 7 0

    -_ 1=ng #! " ";" th>ng t(n m= * nh , 7=o ta 7( t m t ,h ng th " t1ong Mo*) nh /a!

    M PHP

    p:&ic f:nc!ion r:&e() re!:rn array ( array ('code'0'cap!cha'0 'a&&owEmp!y'=g++ap!cha--checke2:iremen!()0 'meae' = 'Q 8^c 6hVn 6hVp Ahn YRn' )0 array('emai&'0'emai&'0'meae' ='a!!ri:!e khn h

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    13/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    p &')0 array('name0emai&0pa0repa0da!e0code' 0're2:ired'0'meae'=a!!ri:!e khn NSc \ !rjn)0

    array('repa'0'compare'0'compare3!!ri:!e'='pa'0'meae'=a!!ri:!e phi chlnh J^c)0 array('pa'0'&en!h'0'min'=''0'meae'=a!!ri:!e khn N N dPi )0 array('da!e'0'da!e'0'forma!'='ddKmmKyyyy'0'meae'=a!!ri:!e phi chlnh J^c dn ddKmmKyyyy)0 );

    h?n% Bt /u v8i Yii ramework

    . u=n #4ch n* dnh cho ai

    NSu b5n l m6t l9p tr>nh vin php v8i c4c k n7n% hiFu biSt v; P$9p tr>nh h?8n% /=i t?Rn%f v mu=n '@* dLn% hJ th=n% hiJn /5i v8i c4cZn% d`n% web th> cu=n #4ch n* dnh cho b5n /F /c cu=n #4ch Yii n*.

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    14/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    2. "5o controller helloworld

    1F t5o nn m6t controller tron% *ii b5n c ! c4ch< c4ch m6t l nSu b5n / lm

    viJc )uen v8i *ii b5n c thF t5o t^ m6t Oile m8i v khai b4o n v8i tn cla##/?Rc kS th^a t^ component< c4ch hai l t5o t^ dsn% lJnh tron% cmd c4chn* b5n c thF tham kh\o t^ ebook *ii cookbook hoc trn din /n*iivn.com< c4ch ! n* c phn d hn cho c4c b5n m8i t>m hiFu *ii / l t5ocontroller t^ m6t cCn% c` tron% *ii c tn l ii.

    Iu h>nh ii

    "r?8c khi #_ d`n% iich3n% ta cn ph\i conOi% Zn% d`n% nh Zn% d`n% chVnh t5i Oile tron% /ach : prtected&cnfig&main%php ull cIu h>nh:g'amppghtdoc#gdev*iigprotectedgconOi%gmain.phpf c4c b5n l?u T ln#au tCi ch %G tBt /a ch b5n ph\i tL vo webroot th? m`c web %=cf /Ft>m.

    h3n% ta vo Oile v thI* dsn% code #au /@* :

    return arra*

    ba#ePathxydirname++z$E++f.Dz{E"{Y+(EPA{A"{...n thI* cCn% c` chVnh

    Cont1o))*1 *n*1ato1 9T o 1a "ont1o))*1

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    17/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    1h2 3y l! bn "4 c# th to "25c 6 file controller ! action helloworld bn c#th !o file MessageController " tin h!nh code theo 7 c8a bn .9:c ban";' bn s thy nh thS nn controller hiJn t5i b5n khi t5o ch?a cactionznde'f v khi b5n cn thm action mc /nh bjn% dsn% lJnh:

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    18/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    php

    cla## e##a%eontroller e'tend# ontroller

    pu#(ic Ndefau(tLctin * 'he((wr(d'O

    Nh? v9* th> khi ch5* link trn n # khCn% csn b4o li 0 na bi v> hJth=n% / nh9n action mc /nh l helloworld

    0iai n cuSi cTngMiew# l %iai /o5n cu=i c]n% /F 'uIt ra dsn% thCn% tin t?n% t4c trLc tiSp t8iZn% d`n% v> thS nn /n %i\n l b5n m Oileprotectedview#me##a%ehelloword.php v cu#tom t]* T vV d`:

    hyHello Worldhy

    "rn tr>nh du*Jt # hiFn th nh? #au: http:localho#tdev*iiinde'.phprxme##a%e

    UhVm nWi dung Wng

    "ron% view# Oile b5n c thF lm bIt kq cCn% viJc lin )uan t8i php bjn% c4ch#_ d`n%ctec c4c th m th /n% cKa php nh? b>nh th?-n% php < y

    MV d` thm th-i %ian cKa hJ th=n%:h!yphp echo dateD :i:# " Yf yh!y

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    19/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    {eOre#h l5i tr>nh du*Jt b5n # thI* n%* %i- cKa hJ th=n%.

    N%oi c4ch nh3n% m php /F hiFn th n%* %i- ra ta csn c m6t c4ch kh4ccLc kq )uan trn% / l %enerate d liJu t^ model ra view thCn% )uacontroller / l ph?n% thZc ho5t /6n% theo mC h>nh M.

    h3n% ta c]n% 'em vV d` #au: b5n voprotectedcontrollere##a%eontroller.php v thI* actionHelloworldf:

    public Ounction actionHelloworldf

    thi#yrenderhelloworldf

    h3n% ta khai b4o m6t biSn m8i v render n #an% view# bjn% c4ch #au:

    pu#(ic functin actin6e((wr(d+G

    M

    NtheUime * date+XK 1 Y 0FiF) U 4XGO

    Nthi)!"render+'he((wr(d'2array+'time'*"NtheUimeGGO

    P

    "iSp / b5n vo protectedview#me##a%ehelloworld.php v hiFn th biSn time/ nh? #au :

    a* %io la:h!yphp echo time yh!y

    $?u l5i Oile view v vo tr>nh du*Jt 'em kSt )u\:

    http:localho#tdev*iiinde'.phprxme##a%e

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    20/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Nh? v9* l c4c b5n v^a lm )uen viJc thao t4c d liJu theo mC h>nh M nh

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    21/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    "iSn hnh m6t #= #L trR %i3p cKa Yii html

    "h9t ma* mBn< Yii h trR c4c ph?n% thZc /F c thF #_ d`n% tron% view#template c4c ph?n% thZc tnh / tn t5i t^ #L trR %i3p cKa Oramework v8icla## I6tm(. "ron% tr?-n% hRp ch3n% ta mu=n t5o c4c link tnh v8icontrollerzD actionzD tha* v> c4c cIu tr3c link# c b\n cKa H"$. 1F #_ d`n%link helper ch3n% ta thm /o5n m #au vo Oile helloworld.php tron% Oolderprotectedview#me##a%ehelloworld.php:

    Zp"ZRphp ech I6tm(FF(ink+'0d#ye'2array+'me))age&gd#ye'GGO R"Z&p"

    Mo tr>nh du*Jt 'em kSt )u\ : http:localho#tdev*iiinde'.phprxme##a%e #hiFn th t?n% tL nh? h>nh bn trn.

    "?n% tL b5n th_ #_a tron% Oile %oodb*e.php tron%protectedview#me##a%e%oodb*e.php:

    hyoodb*e< Yii developerhy

    pyphp echo Html::linkHellonh du*Jt ta c kSt )u\ : http:localho#tdev*iiinde'.phprxme##a%e%oodb*e

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message/goodbyehttp://localhost/devyii/index.php?r=message/goodbyehttp://localhost/devyii/index.php?r=messagehttp://localhost/devyii/index.php?r=message/goodbyehttp://localhost/devyii/index.php?r=message/goodbye
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    22/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    23/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    h?n% 2 i8i thiJu n% d`n% Developer Yii

    X@* dLn% m6t dL 4n m8i trn Yii OrameworkCh:ng ta c# th tip t>c gi< i(c th%m cc ?ng d>ng "@n gi0n nh2Ahello,worldB t Dii, nh2ng "iE' "# s khFng thGc sG gi:p bn hi' "25c Eyii framework trong ng< c0nh c8a ?ng d>ng ngo!i "Hi thGc, " l!m "25c "iE'n!y ch:ng ta c;n +Iy dGng mJt ?ng d>ng mKi,"# l! nJi d'ng c8a ch2@ngn!y.

    Ch:ng ta s giKi thi(' proLecttasktracking application N dG n * taskNnhi(m

    >O tracking Ncch th?cO c8a ?ng d>ng "25c gPi l! Developer Yii. C# nhiE'proLect '0n l7 khc ! iss'etrackingapplication N cc vIn /; cch th?cc8a ?ng d>ng "# trong ngo!i "Hi th2c. Qn s "Rt ra cI' hSi * ATaU sao ph0i+Iy dGng n# B, bVi & n# tr0 E cc th'Jc tWnh c@ b0n c8a ?ng d>ng ! +ah@n c# t3p l(nh trong ?ng d>ng.

    0ii thi[u $> Ke$e(per 4ii

    Ke$e(per 4ii bao %m ! *Su t= chVnh l dL 4n proectf < n%?-i #_ d`n%u#erf< c4c vIn /; i##uef.

    "ron% / u#erbao %m hai tr5n% th4i :

    Anon*mou# l m6t kh4ch n%oi /7n% nh9p vo hJ th=n% khCn% cn ph\ithCn% )ua viJc '4c thLc )u*;n tru* c9p h ch c )u*;n /7n% kT ti kho\n vkVch ho5t ti kho\n< khCn% c )u*;n )u\n tr.

    N%?-i d]n% /?Rc '4c thLc authenticatedf l bIt kq n%?-i d]n% no /?Rc'4c thLc c4c tiu chu}n thCn% )ua )u4 tr>nh /7n% nh9p vo hJ th=n%

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    24/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Xa hn: 4c item /?Rc tr>nh din 'a hn tron% thS %i8i thLc khi /?Rc thmvo Zn% d`n% vV d` : thLc thi hm lo%in /F '4c thLc u#er.

    4c chZc n7n%: ztem /?Rc hiFn th cn ph\i hon thnh c4c cCn% viJc khCn%%p li v #L c=.

    4c li: ztem khCn% ho5t /6n% hon h\o do li ph4t #inh tron% hJ th=n% hoct4c /6n% bn n%oi.

    ! tr5n% th4i chVnh tron% c4c vIn /; / l :

    h?a bBt /un # )ua chi tiSt cKa dL 4n

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    25/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    ~hi m6t n%?-i d]n% bBt /u tru* c9p Zn% d`n%< h ph\i /7n% nh9p v '4cthLc )u*;n tru* c9p t8i Zn% d`n%< nSu /7n% nh9p thnh cCn% h # /?Rc

    'em c4c dL 4n hiJn t5i cKa h hoc h c thF thm m8i.~hi lLa chn m6t dL 4n no / # kVch ho5t vo chi tiSt cKa tran% /

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    26/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    i m6t dL 4n # c nhi;u vIn /; kh4c nhau< nh?n% m6t vIn /; kh4c nhauch thu6c tron% m6t dL 4n m thCi v> v9* nn c )uan hJ 0 ..| vIn /; chZa

    /Ln% tron% dL 4n

    ~St n=i t8i c # d liJu databa#efYii h trR hu hSt c4c c#dl d5n% D( < PD bao %m:

    ($ite: #)lite:pathtodbOile

    *($: m*#)l:ho#txlocalho#tdbnamexte#tdb Po#t%re($: p%#)l:ho#txlocalho#tportx!2dbnamexte#tdb

    ($ (erver: m##)l:ho#txlocalho#tdbnamexte#tdb

    racle: oci:dbnamexlocalho#t:2te#tdb

    "5i /@* tCi # #_ d`n% m*#)l lm c # d liJu.

    5n vo protectedconOi%main.php v t>m dsn% #au:

    dbxyarra*

    connection(trin% xy#)lite:.dirname++z$E++f...datate#tdrive.db < theo mC h>nh Mth> rG rn% ch3n% ta cn ph\i c controller /F thLc thi c4c hnh /6n% t^ model

    hoc '_ lT /u vo t^ u#er /nh danh< tiSp / ch3n% ta ph\i %enerate c4c biSnd liJu v item t^ model #an% view /F thLc hiJn tr?n% b* d liJu.~S ho5chn* t?n% ch^n% kh kh7n nh?n% th9t /n %i\n v8i Yii nh- cCn% c` {Dreatet5o m8i

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    35/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    (au khi / c hon thnh /* /K mC h>nh ruD ri b5n c thF tiSn hnh kiFmth_ v t5o m8i m6t proect bjn% viJc vo tr>nh du*Jt %G :

    http:localho#tdev*iiinde'.phprxproect

    "It nhin ban /u # khCn% c b\n %hi no v b5n # nh>n thI* dsn% chno re#ult Oound< ta click vo create proect menu bn ta* ph\i theo h>nh#au:

    HJ th=n% # *u cu b5n /7n% nh9p v b5n # /7n% nh9p v8i tikho\n mc /nh cKa Yii Oramework l : Account: dem< Pa##word: dem

    1@* l m6t ti kho\n /F te#t u#er tron% *ii v %G 'on% b5n # thI* m6t Oorm*u cu nh9p c4c dL 4n< c4c b5n /i;n b^a vo # c kSt )u\ %i=n% #au /@*:

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=projecthttp://localhost/devyii/index.php?r=project
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    36/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    1i;u n* n%ha l hJ th=n% / thLc hiJn thnh cCn% m6t b\n %hi v /i vo chitiSt b\n %hi / < b@* %i- ta t5o thm vi b\n %hi na ri )ua* tr l5i /?-n% link

    http:localho#tdev*iiinde'.phprxproectinde' # c kSt )u\ t?n% tL #au:

    1@* chVnh l li#t item cKa dL 4n< khi b5n thm m8i b5n / chu*Fn #an% linkcKa action Add< b5n chnh #_a th> tZc l b5n /an% thLc thi action edit< v b5nclick vo chi tiSt m6t b\n %hi b5n / /i vo action detail

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=project/indexhttp://localhost/devyii/index.php?r=project/index
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    37/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    !. X4c thLc hRp lJ c4c tr?-n% d liJu tron% orm

    ~hi lm viJc v8i A{ model cla## khCn% ch c orm tron% Yii< thiSt l9p '4cthLc c4c )u* tBc 'un% )uanh c4c tr?-n% d liJu cKa orm l cn thiSt< /i;u/ %i3p cho viJc kiFm tra nhn% d liJu /c biJt v h5n chS li tron% c#dl< n/?Rc thLc hiJn /* /K tron% m6t m\n% /nh n%ha hm rule#f nh? tCi /ni tron% model Proect.php

    h3n% ta vo Protectedmodel#Proect.php v ch3n% ta / thI* hm n* //?Rc /nh n%ha mc /nh khi %enerate v8i m6t #= )u* tBc:

    ||

    | ,tr\ v; m\n% d liJu '4c thLc cho c4c phn t_ cKa model

    |

    publicOunction rule#f

    5n ch /nh n%ha c4c )u* tBc cho c4c phn t^ / khi m ch3n%

    # nh9n /?Rc thCn% tin t^ Oorm input /u vo ch3n% ta v^a nh9p Tf

    return arra*

    arra*name< de#cription< re)uiredf< tn v mC t\ /?Rc *u cu

    arra*create+u#er+id< update+u#er+id< numerical< *u cu kiFu #=n%u*n

    inte%ernl*xytruef

    +J))ueG`3y l! hFm nay "4 l! nm mKi h6 ph:t ng!y //6, tFi "ang ngi V gh "

    V b%n cnh "i di(n Ki t25ng "!i 2ng jo `2@ng Tr;n 'c T'n, hFm nay th3t "Fngng2Hi "i c;' may c;' lJc, c=n tFi th& n l!m i(c, cFng i(c mang con ng2Hi ta "n sG chtch#c, m(t mSi cng thng nh2ng lao "Jng l! inh 'ang h4y "#n nh3n n#, "2Hng ph "Fngnght ng2Hi cH hoa ging kWn nh

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    43/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    . "hiSt kS biFu / d liJu v '@* dLn% /=i t?Rn% /F h trR c4c vIn/; tron% dL 4n

    2. "5o Yii model cla## cho php Zn% d`n% d dn% khi t5o kSt n=iv8i d liJu tron% b\n%

    !. "5o controller cla## # chZa c4c hm cho php ch3n% ta thLchiJn :

    "5o m8i vIn /;

    $c danh #4ch c4c vIn /; tn t5i tron% proect t^ databa#e

    9p nh9t/i;u chnh c4c vIn /;

    Xa c4c vIn /; /

    . "5o view# Oolder /F render %iao diJn n%?-i d]n% cho c4c action

    @* %i- ch3n% ta /i vo thLc hiJn

    JJJ%B Uhit k #iu

    "ron% ch?n% tr?8c ch3n% ta c thF nh>n # )ua v; T t?n% cKa thLcthF i##ue#< ch3n% ta thLc hiJn n c c4c tr?-n% d liJu %m : name tn vIn/;< t*pe thu6c tVnh vIn /;< owner b\n th@n< re)ue#tor n%?-i *u cunh #chema databa#e /F c thF nh>n thI* mC h>nh d liJu v8ic4c kha

    III.1. 23o 45 (4-tive 5e-or% mo%el -lass@* %i- ch3n% ta / c /* /K c4c b\n%< b?8c tiSp theo l t5o model

    v8i Yii model A{ cho php ch3n% ta d dn% thao t4c v8i b\n% tron% Zn%d`n%< ch3n% ta cn% / lm viJc n* tron% viJc t5o model cKa Proect ch?n% tr?8c v> thS nn thao t4c # khCn% c %> tha* /Qi nhi;u< ch3n% ta vo0ii

    III.1.6 23o Issue 7o%el -lassMo ii v8i /?-n% dn :http:localho#tdev*iiinde'.php

    rx%iideOaultlo%in v nhIn ch3n% ta # vo menu %enerate

    h3n% ta click vo 1de( 0eneratr < t5i tr?-n% table preOi' ch3n% ta/i;n tbl+ /@* l ti;n t= b\n%f

    "5i tr?-n% table name ta /i;n : tbl+i##ue< t5i tr?-n% odel cla## ta/i;n z##ue

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=gii/default/loginhttp://localhost/devyii/index.php?r=gii/default/loginhttp://localhost/devyii/index.php?r=gii/default/loginhttp://localhost/devyii/index.php?r=gii/default/login
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    48/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    "iSp theo b5n nhIn preview y %enerated.

    h3n% ta c thF vo protectedmodel# /F kiFm tra v # thI* z##ue.php

    zzz.. "5o #er model cla##

    MiJc t5o #er model cla## l khCn% cn thiSt bi v> b\n th@n Yii /h trR thao t4c v8i u#er khi #_ d`n% Yii# DA < A{ model cun% cIp m6t iFu/ )uan hJ /=i t?Rn% bect {elational appin% {f cho Zn% d`n% %i3pch3n% ta d dn% )u\n lT /=i t?Rn% tron% domain.

    zzz.. "5o z##ue {D operation5n vo ii v click vo menu link I^oK 0eneratr #au / /i;n vo tr?-n%odel la## : z##ue< /i;n vo tr?-n% controller id : z##ue $?u T z /u /?RcviSt hoaf y preview y %enerated.

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    49/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    (au khi thLc hiJn 'on% ta / hon thnh v vo tr>nh du*Jt kiFm tra:http:localho#tdev*iiinde'.phprxi##ue

    zzz.. "5o m8i z##ue

    (au khi thLc hiJn 'on% b5n t5o m8i m6t vi b\n %hi v vo /7n% nh9p v8iacc: admin pa##: admin khi hJ th=n% *u cu /7n% nh9p

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=issuehttp://localhost/devyii/index.php?r=issue
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    50/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    zzz..& "hm thu6c tVnh drop down

    h3n% ta # t5o ! thu6c tVnh dropdown bao %m u%#

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    51/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    "iSp theo '@* dLn% hm %et"*peption#f # tr\ v; %i4 tr cho c4c thu6c tVnhtrn:

    public Ounction %et"*peption#f

    return arra*

    #elO::"YPE+xyu%m tr?-n% Uypetron% Oorm:

    di class-row

    php echo Xformlabel{+NXmodel,_typeYid_O]

    php echo Xformte+t|ieldNXmodel,_typeYid_O]

    php echo XformerrorNXmodel,_typeYid_O]

    /di

    "iSp theo b5n nh>n ln /o5n /u cKa Oile # c /o5n khai b4o Oorm:

    php Xform-XthisbeginidgetN_CZctie|orm_, arrayN

    _id_-_iss'eform_,

    _enableZLa+`alidation_-false,

    OO]

    1@* l c4ch /nh n%ha m6t Oorm #_ d`n% activeorm tron% Yii.

    "iSp theo b5n vo z##ueontroller.php tron%protectedcontrollerz##ueontroller v nh>n vo hm actionreatef.

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    52/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    p'blic f'nction actionCreateNO

    \

    Xmodel-new ss'e]

    // }ncomment the following line if ZZ alidation is needed

    // XthisperformZLa+`alidationNXmodelO]

    ifNissetNXY[T_ss'e_OO

    \

    Xmodelattrib'tes-XY[T_ss'e_]

    ifNXmodelsaeNOO

    XthisredirectNarrayN_iew_,_id_-XmodelidOO]

    ^

    XthisrenderN_create_,arrayN

    _model_-Xmodel,

    OO]

    ^

    "5i /@* ch3n% ta c thF nh>n thI* view# /?Rc render< n /an% tr\ v; m6tkhi t5o cKa z##ue model< v %i4 tr tr\ v; %i l model.

    @* %i- ch3n% ta )ua* l5i Oile view# /F 'em c4ch '_ lT cKa tr?-n% Uypetron%Oorm. Dsn% /u l :

    OormylabelE'model

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    53/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    \

    ret'rn arrayN

    _id_ - _z_,

    _name_ - _1ame_,

    _description_ - _zescription_,

    _proLectYid_ - _roLect_,

    8t$pe0i%8 9: 82$pe8;

    _stat'sYid_ - _[tat's_,

    _ownerYid_ - _wner_,

    _re'esterYid_ - _e'ester_,

    _createYtime_ - _Create Time_,

    _createY'serYid_ - _Create }ser_,

    _'pdateYtime_ - _}pdate Time_,

    _'pdateY'serYid_ - _}pdate }ser_,

    O]

    ^

    TrV li iew Ki tr2Hng t$pe0i% ta c# th nh&n s@ 'a nh2 sa'*

    php echo Xformlabel{+NXmodel,_typeYid_O]

    (_ d`n% labelE'f cn% c thF '_ lT /?Rc dsn% tVch /[ khi Oield# d liJu /?Rc*u cu. labelE'f # thm m6t c## cla## tn Html::re)uired## v8i mc/nh l re)uired v Html::aOter{e)uired$abel v8i mc /nh l #pancla##xre)uiredy|#pany #au mi phn t_ /?Rc *u cu.

    Dsn% tiSp theo php echo Oormyte'tieldmodel

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    54/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    u=i c]n% l php echo Oormyerrormodel v9* ii %enerated v8i )u* tBc '4c thLctron% z##ue::rule#f ph?n% thZc n* /nh n%ha nh? #au:

    public Ounction rule#f

    N"E: *ou #hould onl* deOine rule# Oor tho#e attribute# that

    will receive u#er input#.

    return arra*

    arra*name< re)uiredf thS nn ch3n% ta cn thm dropdown Oield cho ch3n%.h3n% ta khCn% thF biSt /Vch '4c mi kS ho5ch ch3n% ta lm choph;n ttype ! stat's, cng nh2 ownerv re'esters/?Rc tiSn hnh t^ tbl+u#er.Nh? v9* mi n%?-i d]n% tron% hJ th=n% # lin )uan v8i proect bi nhn%vIn /; i##uef lin )uan. M nhn% i##ue / khCn% thF dropdown v8i d liJut^ b\n% tbl+u#er. h3n% ta cn lc ra danh #4ch /u vo cKa nhn% n%?-id]n% c lin )uan t8i proect /.

    1i;u n* /?Rc /; n%h tron% kS ho5ch 'a hn ch?n% n*< ch3n% ta cn)u\n lT vIn /; v8i khCn% ch n6i dun% cKa proect /c biJt. N%ha l proect/c biJt # /?Rc chn tr?8c khi b5n c thF t5o vIn /; i##uef m8i. ~hi m6tproect /?Rc chn ch3n% ta cn chBc chBn c\ b\n th@n ownerf v n%?-i *ucu re)ue#terf dropdown lLa chn /?Rc trVch lc ra nhn% u#er lin )uan t8idL 4n.

    zzz.. ~iFm tra n6i dun% proect

    h3n% ta mu=n chBc chBn rjn% n6i dun% proect hRp lJ /?Rc hiFn thtr?8c khi ch3n% ta cho php tru* c9p t8i )u\n tr z##ue. 1F lm /i;u n*

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    58/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    ch3n% ta cn lm viJc thLc thi c4i %i l b6 lc Oilterf. 6t b6 lc tron% *ii lnhn% /i;u chnh cIu h>nh /?Rc thLc hiJn tr?8c khi hoc #au khi m6t

    controller action /?Rc thLc hiJn. 6t vV d` v; lJnh l nSu ch3n% ta mu=n chBcchBn n%?-i d]n% / /7n% nh9p t^ viJc thLc hiJn controller action method.h3n% ta c thF viSt /n %i\n b6 lc tru* c9p / # kiFm tra c4c *Su t= tr?8ckhi action / /?Rc thLc hiJn.

    zzz..2 1nh n%ha Oilter# b6 lcf

    6t Oilter#b6 lcf c thF /?Rc /nh n%ha nh? m6t controller cla## hocn c thF l m6t cla##. ~hi #_ d`n% kS hoc /n %i\n #implemethodf< tn

    ph?n% thZc ph\i bBt /u bjn% t^ filter v c m6t kT tL /c biJt. ho vV d`nSu ch3n% ta /an% t5o m6t b6 lc ph?n% thZc : #omeethodName< ta #viSt Oilter nh? #au:

    public Ounction Oilter(omeethodNameOilterhainf

    ...

    m6t kS ho5ch kh4c /F #_ d`n% Oilter / l d]n% cla## thLc hiJnOilter lo%ic< khi #_ d`n% cla## Oilter ph\i /?Rc kS th^a t^ Oilter v /?Rc

    t\i Vt nhIt preilterf hoc po#tilterf method )u*St /nh mZc lo%ic /F thLchiJn tr?8c hoc #au m6t action /?Rc khi t5o.

    zzz..! "hm m6t ilter@* %i- ch3n% ta # thm m6t Oilter tron% z##ueontroller cla## /F /i;u

    khiFn kiFm tra m6t proect hRp lJ. h3n% ta tiSn hnh kS ho5ch:

    protectedcontroller#z##ueontroller.php v viSt hm Oilter #au /o5n d?8i %m cla## #au Ounction #earchf.

    public Ounction OilterProectonte'tOilterhainf

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    59/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Oilterhainyrunf

    k< b@* %i- ch3n% ta / /nh n%ha m6t Oilter< tu* nhin ch?a c %>nhi;u /@* /n %i\n ch l thLc thi Oilter Oilterhainyrunff< c4i m '_ lTOilter v cho php thLc hiJn c4c ph?n% thZc action /?Rc lc bi methodnh n %i khiactionreatef /?Rc kVch ho5t v8i z##ue controller cla##.

    "u* nhin n vn khCn% thLc hiJn theo lo%ic

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    61/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    |

    private +proect x null

    ||

    | 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 modelba#ed on the primar* ke*

    |

    protected Ounction loadProectproectzdf

    iO the proect propert* i# null< create it ba#ed on input id

    iOthi#y+proectxxxnullf

    thi#y+proectxProect::modelfyOind*Pkproectzdf

    iOthi#y+proectxxxnullf

    throw new HttpE'ception0

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    62/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    #et the proect identiOier ba#ed on E" input re)ue#t variable#

    iOi##et+E"pidff

    thi#yloadProect+E"pidf

    el#e

    throw new HttpE'ception0!

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    63/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    zzz.. "hm Proect zD

    ch?n% t5o proect ch3n% ta / thm nhi;u proect /F kiFm tra hJ th=n% vui lsn% vo http:localho#tdev*iiinde'.phprxproect t5o kho\n% vi proect /F chu}n b cho cCn% viJc tiSp theo n*.iv> cCn% viJc tiSp theo *u cu ph\i c zD cKa proect /F c thF thm id n* t8iviJc t5o m8i i##ue {$.

    "iSp theo ch3n% ta cn /i;u chnh link# tron% view Oile i##ue :protectedview#i##ueinde'.php. Nh>n phVa trn /u Oile b5n # thI* ni link#/?Rc /nh n%ha )ua m\n% menu item#:

    thi#ymenuxarra*

    array+'(a#e('*"'Ireate J))ue'2 'ur('*"array+'create'GG2

    arra*labelxyana%e z##ue< urlxyarra*adminffnh du*Jt %G : http:localho#tdev*iiinde'.phprxproectviewidx2'em kSt )u\:

    III.1.!6 2i

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    77/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    public Ounction %et(tatu#"e'tf

    #tatu#ption#xthi#y#tatu#ption#

    return i##et#tatu#ption#thi#y#tatu#+idf

    #tatu#ption#thi#y#tatu#+id : unknown #tatu# thi#y#tatu#+

    idf

    ||

    | ,return #trin% the t*pe te't di#pla* Oor the current i##ue

    |

    public Ounction %et"*pe"e'tf

    t*peption#xthi#yt*peption#

    return i##ett*peption#thi#yt*pe+idf

    t*peption#thi#yt*pe+id : unknown t*pe thi#yt*pe+idf

    "5i /@* # tr\ v; c4c tr5n% th4i value hua #an #an%

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    78/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    public Ounction %et(tatu#ption#f

    return arra*

    #elO::("A"(+N"YE"xyhua (an (an%2#bn thln mWt u)er nm trng #bngu)er

    're_ue)ter' *" array+)e(fFF3s70U2 'o)er'2 're_ue)terid'G2

    && n n.y ) dng (iVn kt thuWc $>2ngi yVu cu nm trng #bngu)er

    1o5n n* / /?Rc khi t5o ri v b5n khCn% cn ph\i vo / /i;u chnh l5inh

    lo%in

    zM. #er {D"r?8c tin b5n vo ii : http:localho#tdev*iiinde'.phprx%iideOaultinde'

    hn 1de( 0enerated v /i;n %i=n% nh? h>nh #au:

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=gii/default/indexhttp://localhost/devyii/index.php?r=gii/default/indexhttp://localhost/devyii/index.php?r=gii/default/indexhttp://localhost/devyii/index.php?r=gii/default/index
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    92/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    lick Preview y enerated.

    (au khi hon thnh b5n vo protectedmodel# /F kiFm tra Oile m8i t5ob5n # thI* #er.php

    "iSp theo b5n vo ii chn I^oK 0eneratr < t5i odel cla## : /i;nvo : }ser< t5i ontroller zD /i;n vo : }ser, #au / chn preview y%enerated.

    "iSp theo b5n vo http:localho#tdev*iiinde'.phprxu#er kiFm tra b5n/ thI* 2 u#er /?Rc t5o #n ban /u

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=userhttp://localhost/devyii/index.php?r=user
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    93/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    h3n% ta cn% c thF t5o m8i u#er n%a* b@* %i- t^ link menu createu)erF http:localho#tdev*iiinde'.phprxu#ercreate

    NSu hiJn t5i b5n khCn% /7n% nh9p b5n # c router lo%in #_ d`n% v8i 2ti kho\n l demodemo v adminadmin.

    I?.! 2h=nh ph@n Aehaviorehavior tron% Yii cla## /?Rc thLc thi %iao diJn cKa zbehavior< vph?n% thZc cKa n c thF kS th^a t^ hm thnh phn bi viJc kVchho5t component. $T do ch3n% ta #_ d`n% component n* bi v> #er cn thiSt cho tr?-n% d liJu. vch3n% ta c thF thLc hiJn c4c lJnh kSt n=i v8i A{ model cla##.5n #

    lLa chn kS ho5ch n* t=t hn cho Zn% d`n% Yii c Ka b5n.

    1F lm viJc ch3n% ta cn t5o m8i A{ model cla##< tr?8c tin 'a b[hm #eha$ir)+G -hBng ta v*a t3o trong ser.php mo%el.

    "iSp theo b5n vo protectedmodel# b5n t5o OileKe$4iiLcti$e^ecrd%php $i nWi dung )auF

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    96/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    php

    ab#tract cla## DevYiiActive{ecorde'tend# Active{ecord

    protected Ounction beOore(avef

    iOnull xx Yii::appfyu#erf

    idxYii::appfyu#eryid

    el#e

    idx

    iOthi#yi#New{ecordf

    thi#ycreate+u#er+idxid

    thi#yupdate+u#er+idxid

    return parent::beOore(avef

    public Ounction behavior#f

    return arra*

    "ime#tampehavior xy arra*

    cla## xy ii.behavior#."ime#tampehaviornh thLc hiJn proect. hi tiSt vui lsn% vo )u*FnYii cookbook nh tCi / dch ri vo Yiivn.com m t>m tron% phnebookf< c hai ph?n% thZc h trR cho php ch3n% ta chu*Fn d liJub\n %hi tr?8c khi ch3n% ta thLc hiJn nhn% %> cn thiSt tr?8c khi hoc

    #au khi kVch ho5t b\n %hi %m 2 hm : before[aeNO ! after[aeNONTr2Kc khi l2' ! sa' khi l2'O.

    "ron% tr?-n% hRp n* ch3n% ta )u*St /nh l?u t5m 2 biSn time tr?8ckhi tiSn hnh l?u vo (D$.

    h3n% ta )u*St /nh viJc khCn% ch %i\i )u*St viJc t5o m8i b\n %hi ha*c9p nh9t b\n %hi #_ d`n% thu6c tVnh thi#yi#New{ecord v thiSt l9pd liJu. ~hi ch3n% ta chBc chBn khi t5o tr\ v;parent**before[aeNO l!ch:ng ta "4 thGc hi(n +ong mPi th? "25c l!m trong h!m. Ch:ng tathGc hi(n Dii**appNO'ser " + l7 nhnghn ch tr'y c3p.

    h3n% ta c thF di chu*Fn behavior#f method /F lm cla## c b\n choc4c A{ model kS th^a n v8i viJc kVch ho5t behavior#

    "a th_ /i;u n*< ch3n% ta cn chnh l5i mi ! A{ cla## model l Proectnh (D$f

    "run% t@m t8i '4c thLc u#er cKa Yii l m6t thnh phn Zn% d`n% %i lu#er< n l m6t /=i t?Rn% /?Rc thLc thi t^ %iao diJn zweb#er< cla## /c biJtn* /?Rc #_ d`n% bi viJc thLc thi web#er< thnh phn u#er n* l thCn%tin tIt c\ /nh danh u#er hiJn t5i c tron% Zn% d`n%. "hnh phn n* /?Rc

    cIu h>nh cho ch3n% ta nh? m6t phn tL /6n% tron% hJ th=n% khi Zn% d`n%

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    104/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    /?Rc t5o bi *ii tool< 1F cIu h>nh n b5n vo protectedconOi%main.php vnh>n /o5n n*:

    u#erxyarra*

    enable cookieba#ed authentication

    allowAuto$o%inxytruen thI* m\n% chZa 2 phn t_ demodemo v adminadmin chVnh l 2u#er ch3n% ta c thF /7n% nh9p vo web#ite

    1F hiFu rG hn v; )u* tr>nh cKa n b5n h* 'em # / #au:

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    106/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    u* tr>nh bBt /u v8i viJc thiSt l9p cla## tron% Oorm model$o%inorm.php protectedmodel#$o%inorm.php f < v khi Oorm /?Rc #ubmitnh )u\n lT tru* c9p

    Mo protectedconOi%main.php v thm /o5n #au vo m\n%component:

    component#xyarra*

    authana%erxyarra*

    cla##xyDbAuthana%ernh du*Jt http:localho#tdev*iiinde'.phprxproect

    hn m6t id bIt kq ri nh>n vo menu bn ph\i chn Ireate J))uenh>n vo menu bn ph\i chn 1anage J))ue #au / click vo chi tiStm6t tran% # nh>n thI* kSt )u\ #au:

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=projecthttp://localhost/devyii/index.php?r=project
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    132/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Mz. "5o wid%et comment %n /@* Mz.. Name# #cope#

    1F bBt /u t5o wid%et < ch3n% ta # chnh l5i omment A{ modelf tr\ v;comment %n /@*

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    133/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    comment#xomment::modelfyrecentfyOindAllf

    Hoc /n %i\n hn na ch3n% ta ch thLc hiJn nh? #au:

    public Ounction recentlimitxf

    thi#y%etDbriteriafymer%eWith

    arra*

    orderxyt.create+time DE(nh (E ph4t triFn< ta cn c m6t url /p hn na nh?

    kiFu: http:localho#tdev*iicommentOeed hochttp:localho#tdev*iicommentOeed.'ml

    1F lm /?Rc /i;u n* ch3n% ta cn ph\i nh- #L trR %i3p cKa Yii# {$ana%ement /F chnh l5i /nh d5n% cKa {$.

    Mzz.!. (_ d`n% {$ ana%er1F '@* dLn% cIu h>nh l5i {$ b5n cn vo protectedconOi%main.php Oile /F/nh n%ha url mana%er:

    urlana%erxyarra*

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php?r=comment/feedhttp://localhost/devyii/index.php?r=comment/feedhttp://localhost/devyii/commentfeedhttp://localhost/devyii/commentfeed.xmlhttp://localhost/devyii/index.php?r=comment/feedhttp://localhost/devyii/index.php?r=comment/feedhttp://localhost/devyii/commentfeedhttp://localhost/devyii/commentfeed.xml
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    142/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    urlormatxypath : $ m6t %i url tr\ v; m tr>nh du*Jt *u cu hJ th=n% '_ lT< nchZa c\ phn #= v phn ch< vV d` nh? c4c biSn +E"< +P("< hoc /n%i\n l c4c tiu /; n6i dun% cKa m6t bi no / m tr>nh du*Jt *u cu 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 ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/index.php/comment/feedhttp://localhost/devyii/index.php/gii/default/indexhttp://localhost/devyii/index.php/projecthttp://localhost/devyii/index.php/comment/feedhttp://localhost/devyii/index.php/gii/default/indexhttp://localhost/devyii/index.php/project
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    143/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    /F /nh n%ha c4c khai b4o tru* nh9p router /F )u*St /nh 'em controller nov action no /?Rc thLc hiJn .

    M8i nhn% )u* tBc {$< thiSt l9p urlana%er Oile # tr\ v; c4c thu6c tVnh )u*tBc theo m\n% arra* v8i /nh d5n% patternxyroute.

    M /@* l vV d` )ua v; m6t )u* tBc: "hm /o5n #au vo urlana%er:

    khai b4o /?-n% dn i##ue tr\ v; controlleraction i##ueinde'f

    khai b4o /?-n% dn chi tiSt i##ue theo d5n% i##ue#id d5n% #=f tr\ v;i##ueviewf.

    urlana%erxyarra*

    urlormatxypathnh du*Jt %G :

    http:localho#tdev*ii#itelo%in

    Mzzz.. i8i thiJu luePrint (( ramework

    luePrint c## Oramework /?Rc thm vo Zn% d`n% khi b5n khi t5o Zn%d`n% t^ dsn% lJnh Yii tool

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    157/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    tiu chu}n /nh n%ha H"$ %i3p cho phn /nh n%ha (( %p Vt li< n t8iv8i nhi;u #creen print Oriendl* la*outng "2Hng dn iews chWnh trong ?ng d>ng h@n i(c s d>ng"2Hng dn t mod'les. h3n% ta # hc v; module# tron% ch?n% t8i /@*.

    h3n% ta h* c]n% 'em vV d` c4ch t\i view# Oile v8i(iteontroller::action$o%inf:

    thi#yrenderlo%inf

    "5i /@* hJ th=n% # thLc hiJn:

    "\i tIt c\ n6i dun% tron% Oile protectedview##itelo%in.php v lmcontent chIp nh9n %i4 tr content t^ la*out Oile tron% controller < / lcol'mn.pphp

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    163/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    (au / col'mn.php# %i t8i la*out chVnh l main.php< n6i dun% njm%ia be%inontentf v endontentf # chu*Fn %i4 tr v; la*out

    main.phpv8i biSn content. $a*out Oile main.php # t\i ln v tr\ v; %iao diJn t8i n%?-i d]n%< nh?

    v9* ta c thF nh>n thI* thnh phn controller nh9n c\ Oile main.php !col'mn.php chZ khCn% ph\i du* nhIt la*out chVnh nh? b5n n%h.

    6t la*out kh4c # tL /6n% /?Rc t\i khi t5o m8i Zn% d`n% / l la*outcol'mn6.php< khi b5n /7n% nh9p admin 'on% b5n vo tron% )u\n tr proectb5n # thI* c6t menu navi%ation njm bn ta* ph\i / l cIu h>nh cKa la*outcolumn2.php < b5n vo protectedcontrollerProectontroller.php # thI* dsn%khai b4o #au:

    public la*outxla*out#column2

    N6i dun% cKa Oile column2.php

    php | ,var thi# ontroller | y

    php thi#ybe%inontentla*out#mainf ydiv cla##x#pany

    div idxcontenty

    php echo content y

    divy content y

    divy

    div cla##x#pan la#ty

    div idx#idebary

    php

    thi#ybe%inWid%etii.wid%et#.Portlet< arra*

    titlexyperation# n # tr\ v; tiSn% viJtnh? h>nh trn /3n% khCn%

    5n ch cn t5o thm tron% protectedme##a%e# m6t Oolder env m6t Oilephp l : deOault.php v thLc hiJn * hJt nh? b?8c t5o Oile vi< khi c4c b5n t5ohai l4 c- chu*Fn dch b5n # t5o thm m6t tr?-n% /F '4c /nh /@u l en v/@u l vi #au / b5n tha* thS tron% protectedconOi%main.php c /o5n2xx0 < nh? v9* l ton b6 n6i dun% # chu*Fn dch.

    HN ( DN Yzz D$E

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

    http://localhost/devyii/site/loginhttp://localhost/devyii/site/login
  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    172/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    ~ HH

    "5o module m8i t^ admin

    "5o kh\ n7n% admin c thF thm tin nhBn m r6n% t^ hJ th=n% cho

    Zn% d`n% n%?-i d]n%< hiFn th t5i tran% danh #4ch proect

    "h_ theme m8i t8i module

    "5o b\n% c#dl m8i l?u c4c tin nhBn cKa hJ th=n%

    "5o tIt c\ rud cho tin nhBn hJ th=n%

    H5n chS tru* c9p tIt c\ c4c hm kF c\ module m8i t^ admin

    HiFn th tin nhBn hJ th=n% tron% danh #4ch proect

    zX. $m viJc v8i module

    6t module Yii # chZa rIt nhi;u miniZn% d`n% tron% /

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    173/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    ipilter#xyarra*2.0.0.nh du*Jt 'em kSt )u\:

    http:localho#tdev*iiadmindeOaultinde'

    zX.2 odule la*out6t thZ ch3n% ta # ph\i nh>n l5i ch?n% tr?8c / l newtheme,n cn%cn /?Rc 4p d`n% vo module

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    174/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    module c` thF khi t>m kiSm t9p tin< v> thS ch3n% ta cn 4p d`n% cho nhn%la*out n%oi module /F Zn% d`n% cKa ch3n% ta d lm viJc hn.

    NSu ch3n% ta ch thLc hiJn m6t dIu %5ch cho tr?8c phn /nh n%ha tnla*out cn t>m< ta # thI* module admin # khCn% /?Rc 4p d`n% tron% tIt c\mi nimkiSm c4c t9p tin tron% module chZ khCn% ph\i njm tron% Zn% d`n% chVnh.h3n% ta # tha* /Qi /i;u n* v8i viJc #_ d`n% module:

    5n c thF cIu h>nh hu hSt mi thZ tron% module < vV d` thm thnh phnla*out Oile< mc /nh la*out cho module # njm tron% protectedmodule#modulezdview#la*out#< modulezd /@* l adminchn% h5n

    h3n% ta c thF nh>n thI* l3c khi t5o # khCn% c Oile n* v> thS nn module# nh9n la*out mc /nh t^ Zn% d`n%

    zX.! "h_ m6t la*out"r?8c tin b5n cn vo protectedview#la*out#column v /Qi l5i nh? #au:

    php thi#ybe%inontentla*out#mainf y

    1Qi thnh

    php thi#ybe%inontentla*out#mainf y

    1u tin h* thiSt l9p la*out mc /nh cho module< ch3n% ta # thLc hiJn bihm initf tron% protectedmodule#adminAdminodule#.php

    cla## Adminodule e'tend# Webodule

    public Ounction initf

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

    *ou ma* place code here to cu#tomie the module or the

    application

    import the modulelevel model# and component#

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    175/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    thi#y#etzmportarra*

    admin.model#.|n chun%

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    198/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    khi b6 nh8 /Jm d liJu th-i %ian< ch /nh /6 di n6i dun% c thF vn c %i4 trtron% b6 nh8 cache.

    1 l t?n% tL nh? c4c thCn% #= th-i %ian ch3n% tCi #_ d`n% khi b6 nh8 /JmthCn% b4o hJ th=n% cKa ch3n% tCi. "a c thF ch /nh tham #= th-i %ian khi%i be%inache f nh? #au: thi#y be%inache ke*< arra* durationxy !00ffi /t mc /nh cho c4ch tiSp c9n n* ph@n m\nh b6 nh8 /Jm l kh4c nhauhn #o v8i d liJu b6 nh8 /Jm. NSu ch3n% ta khCn% thiSt l9p th-i %ian nmc /nh l 0 %i@*< c n%ha l n6i dun% /?Rc l?u tron% b6 nh8 cache # hSthiJu lLc #au 0 %i@*. rIt nhi;u lLa chn kh4c b5n c thF thiSt l9p khi #_

    d`n% b6 nh8 /Jm. 1F biSt thm thCn% tin< h* tham kh\o c4c APzti liJu h?8n% dn cho utputache cn% nh? m\nh phn b6 nh8 /Jm cKac4c Yii http:www.*iiOramework.comdoc%uide.encachin%.Ora%ment

    X. (_ d`n% ph@n m\nh cacheH* thLc hiJn /i;u n* tron% Zn% d`n% DevYii. h3n% ta m6t ln na # t9ptrun% vo li#t proect. b5n c thF nh8 l5i< phVa d?8i c]n% cKa tran% li#t proectc m6t li#t comment / /?Rc n%?-i d]n% comment v8i vIn /; lin )uan

    t^n% dL 4n. Danh #4ch n* ch cho thI* nhn% n%?-i csn l5i m6t b>nh lu9nv; vIn /; no. "ha* v> hi ph`c danh #4ch n* mi khi c *u cu< ch3n% tah* #_ d`n% m\nh b6 nh8 /Jm cache danh #4ch n* hai ph3t. 4c Zn% d`n%c thF chu /Ln% c4c d liJu n* hi c< v hai ph3t l thLc #L khCn% ph\i ldi ph\i ch- /Ri cho m6t danh #4ch b>nh lu9n /?Rc c9p nh9t.

    Mo protectedview#proectinde'.php:

    php

    ke*xDevYii.Proect$i#tin%.{ecentomment#

    iOthi#ybe%inacheke*

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    199/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    thi#yendachef

    y

    X.& Pa%e achin%N%oi b6 nh8 /Jm m\nh< Yii cun% cIp c4c t]* chn /F cache c4c kSt )u\ cKa*u cu ton b6 tran%. 4ch tiSp c9n b6 nh8 /Jm tran% t?n% tL nh? c4c b6nh8 /Jm m\nh. "u* nhin< bi v> n6i dun% cKa ton b6 m6t tran% th?-n%/?Rc t5o ra /F 'em< ch3n% ta c thF khCn% ch /n %i\n %i be%inache f v

    endache f tron% Oile. $T do l n /?Rc 4p d`n% tron% cu6c %i/F ontroller :: render f ph?n% ph4p #au khi 'em n6i dun%. M> v9*< ch3n%ta # luCn luCn b[ l c h6i /F lI* n6i dun% t^ b6 nh8 cache.M> v9*< /F cache ton b6 tran%< ch3n% ta hon ton nn b[ )ua viJc thLc hiJnc4c hnh /6n% t5o ra c4c n6i dun% tran%. 1F thLc hiJn /i;u n*< ch3n% ta cthF #_ d`n% l8p outputache nh? l m6t b6 lc hnh /6n% tron% l8p /i;ukhiFn .MV d`< ch3n% ta h* #_ d`n% c4ch tiSp c9n b6 nh8 /Jm tran% b6 nh8 cache

    c4c tran% kSt )u\ cho mi tran% chi tiSt proect . "ran% chi tiSt proect tron%DevYii /?Rc tr\ l5i bjn% re)ue#t: {$ http:localho#tdev*iiproectviewidid< tron% / id l zD dL 4n c` thF ch3n% ta /an% *u cu chi tiSt . Nhn% %>ch3n% ta mu=n lm l thiSt l9p m6t b6 lc b6 nh8 /Jm # cache ton b6 n6idun% cKa tran% n* m6t c4ch rin% biJt cho mi zD *u cu. h3n% ta cnph\i kSt hRp zD dL 4n vo %i4 tr b6 nh8 cache. 1 l< ch3n% ta khCn% mu=nlm cho m6t *u cu 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. 4c b6 lcoutputache cho php ch3n% ta lm /i;u /.

    protectedcontroller#Proectontroller.php v chnh l5i Oilter#f

    public Ounction Oilter#f

    return arra*

    acce##ontrol< perOorm acce## control Oor {D operation#

    array+

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    200/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    'IutputIache q $iew'2 &&cache the entire utput frm the

    actinjiew+G methd fr C minute)

    'duratin'*"BCE2

    '$aryy5aram'*"array+'id'G2

    G2

    f

    Iu h>nh b6 lc n* #_ d`n% c4c b6 lc utputache cache ton b6 #\nl?Rn%/?Rc t5o ra bi c4c Zn% d`n% t^ m6t cu6c %i /Sn Proectontroller ::

    ActionMiew f."hCn% #= 'em thm n%a* #au khi t- khai utputache< nh? b5n c thF%i l5i< l c4ch tiu chu}n ch3n% tCi bao %m c4c ph?n% ph4p hnh /6n% c`thF m m6t b6 lc

    nn 4p d`n%. "ham #= th-i %ian )u* /nh ""$ l 20 %i@* 2 ph3tfnh var**Param l m6t lLa chn thLc #L tu*Jt v-i m ch3n% tCi4m ch /Sn tr?8c."ha* v> /t tr4ch nhiJm ln b5n< c4c nh ph4t triFn< /Sn v8i m6tchiSn l?Rc )uan trn% du* nhIt cho c4c n6i dun% /?Rc l?u tr< tVnh n7n% n*cho php #L tha* /Qi/?Rc '_ lT tL /6n%. MV d`< tron% tr?-n% hRp n*< bjn% c4ch '4c /nh m6tdanh #4ch tnt?n% Zn% /F E" c4c thCn% #= tron% *u cu /u vo. M> ch3n% tCi l b6nh8 /Jmtran% n6i dun% *u cu /=i v8i c4c dL 4n do proect+id< n lm cho c\m %i4chon h\o /F #_ d`n%zD n* nh? l m6t phn cKa thS hJ ch>a kha du* nhIt cho b6 nh8 /Jm n6idun%. jn% c4ch '4c /nhMar**Param xy arra* idf< utputache thLc hiJn /i;u n* cho ch3n% tadLa trn input

    uer*(trin% id. nhi;u lLa chn c #n /F /5t /?Rc /i;u n* lo5i

    L o n g t 8 x - P h a m ! " # a " t $ % $ & ' g m a ( ) . " o m Pag* $2+

  • 8/12/2019 Web Application Development With Yii and PHP Nov2012 Vietnamese

    201/206

    January 1, 2013

    [WEB.APPLICATION.DEVELOPMENT.WITH.YII.A

    ND.PHP 2ND EDITION]

    Autoontent #L tha* /Qi chiSn l?Rc khi #_ d`n% utputache b6 nh8 cached liJu cKa ch3n% ta. Yii ..2 c #n c4c tVnh n7n% #au

    var**{oute: jn% c4ch thiSt l9p t]* chn n* l true< c4c re)ue#t route c`thF/?Rc /?a vo /nh danh du* nhIt cho c4c d liJu /?Rc l?u tron% b6 nh8cache. M> v9*< b5nc thF #_ d`n% #L kSt hRp cKa b6 /i;u khiFn /?Rc *u cu v hnh /6n% /Fph@n biJtl?u tr n6i dun%. var**(e##ion: jn% c4ch thiSt l9p t]* chn n* /F th9t #L< m6t #e##ion zD

    du* nhIt /?Rc #_ d`n%/F ph@n biJt c4c n6i dun% tron% b6 nh8 cache. i phin n%?-i d]n% c thF'em #L kh4c nhaun6i dun%< nh?n% tIt c\ c4c n6i dun% n* vn c thF /?Rc ph`c v` t^ b6 nh8cache. var**Param: Nh? / th\o lu9n tr?8c /< /i;u n* #_ d`n% c4c /u voE" chui tru* vInc4c thCn% #= /F ph@n biJt c4c n6i dun% tron% b6 nh8 cache. var**E'pre##ion: jn% c4ch thiSt l9p t]* chn n* /F m6t biFu thZc PHP v9*< v8i c4c b6 lc trn cIu h>nh tron% l8p Proectontroller cKa ch3n% tCi