深入理解Yii2.0


ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0 Release 1 Linuor September 14, 2014 CONTENTS 1 ¸¸¸ ˚˚˚ 3 1.1 Yii/¿H .............................................. 3 1.2 ÃoÂ∆ ............................................... 4 1.3 Yii2.0ÓMÑ∂ .......................................... 4 1.4 Yii2.0 ˘‘ Yii1.1 ÑÕ'ÿ(TBD) ................................ 4 2 Yii ˙˙˙ @@@ 11 2.1 ^' ................................................. 11 2.2 ãˆEvent ............................................ 16 2.3 L:Behavior .......................................... 24 3 Yii ¶¶¶ ööö 35 3.1 + ................................................. 35 3.2 Mnyconfiguration (TBD) ................................... 42 3.3 Yiiî(ÑÓU”Ñ ......................................... 44 3.4 YiiÑ{Í®†}:6 ........................................ 47 4 Yii !!! ✏✏✏ 51 4.1 MVC ................................................. 51 4.2 Yii-Ñ°öMhùVËe(TBD) ............................... 52 5 âââ ≈≈≈ Yii 61 5.1 (Composerâ≈Yii ........................................ 61 5.2 Œã)⇧â≈ ............................................ 61 5.3 ænWeb°h ........................................... 61 5.4 Yii-ÑM ............................................ 63 5.5 Mnî(ØÉ ............................................ 63 5.6 ØÉÑMnü⇡ ........................................... 64 5.7 .................................................... 65 i ii ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 SMH,˜⇢Ver 20140901 ,f⌃è@Yiiòπ˘Yii2Ñ—≠0ÃÖπå€LÙ∞Å⇧˚∞ÑÖπ˜øÓ ⌦Òe⌃ „Yii2.0↵Ñ∞H ⇥˙Æ6œQ@Âø øÓ⇥ ˚UëÓByƒ˙Æ˝"Œ˚⇧↵À⇢«ï˃∫YJÂ""/ CONTENTS 1 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 2 CONTENTS CHAPTER ONE ¸¸¸ ˚˚˚ Ÿ/,r'⇥;Å≤„Yii2.0 @„hÑ∞„Web—F∂Ñ∞y'∞Ä/∞Ô∞! ✏⇥ f`åŸÃbÑhËÖπ`ó0Ñ≈≈/YiiH(ÑûÕÄÁÿ⌃Ü„vû∞ÑÄ/ ü⌃åÖU Ù:ÕÅÑ⌃/Web—-:ALå⇣üÑæ°!✏å—Ô⇥˚⇧↵À˝ Œ- @6∑'⇥ 1é,∫Ñ4s PÄ/ æôÑÖπö˘"Œ˚⇧↵ÀÏ(QuïËY↵ƒ∫yƒ ⌥c⇥ 1.1 Yii/// ¿¿¿HHH Yii/*PHPF∂(é—⌅Õ{ãÑWebî(⇥Yiiòπ⌃vöI:ÿ'˝˙郈ÑF∂⇥ 1*∫Ñœå;”Yiiw Â↵yπ⇢ • Yii‘É“n”⇥ Yii—‚ÙsË⇢ÖWeb—Ñ∞Ä/àË✏86S↵:ALÑÄ/⇥ ÔÂÙ—teWeb—-nÑÄ/˝ÔÂ(Yii´⌦⇢⌘Ñ↵0qP⇥ ‘Ç⇢⇢ÀÑ ˆ⇡Yii& >ÑRuby on RailsŒ< ‘ÇYii2-⇢⇢∞ûÑ}zÙIPHP∞y'/I⇥ *fló⌦nAåãøÑF∂Mw 8õå}õf`weM ✏ ✏I⇥ • Yii‘É“◆”⇥ cÇvWÑ—ÛYii/*‘É◆f◆(ÑF∂⇥„(œàÿ ∏⇢Ô f`Ñ0π⇥ Ë ⇧pác0Ã⇧˚„æ¶ÿ⇥>:;√òπ∫[ -á:˝Ö∫[ ∫_ÿOKÂ∆∑÷π◆⇥ ∂ѯ˘3öŒYii1.10Yii2Ñÿ↵∏⇢üeѶöåâ¿ Ñœå˝ÿ⇥(⇥ • Yii‘É“h”⇥ 1Web—‡∫/ÍÕ{ãÑî(‡∫/Í*—6µÑ8¡ÓòYii˝ ⇣üÿHÔ`Ñ„≥πH⇥ ˘éxãÑWeb—ŸÚœ/≥Ü⇥‘Ç*Y ˝ERESTfulIYii˝ –õ↵ÑF∂⇥ F/Œƒ!⌦e≤Yiiÿó⌦*'ã F∂⇥ *∫˘vу˜/*-ãO{πÑF∂˘é›'⇢pÑî(Øö/E⌃ÑÜ⇥ • Yii‘ɓΔ⇥ Yiiòπä–LHá\:*ÕÅÑyπe£ ⇥ŒûE(↵(¯⇢PHPF∂ -nûHá⌦w ö⇠ø⇥ F*∫§:Ÿπvû/yπÕÅ\:F∂Ñ(⇧_1 /—∫Xe≤ÙÕÅÑ/—Há⇥ é1Yii∂Ñ⌃Web-—8(ÑÔå!✏˝Ô àz0W⌦(⇥ (Web—-8GÂÑ∆Ç⌦Yii_–õÜ∏⇢∞⇣„≥πHˇe1Ô (^8ÿHπø⇥ ÓMYii $*;ÅÑH,Yii1.1åYii2.0⇥Yii1.1/ÑH,⌘ôŸ*Y↵ˆ∞H,˜ /1.1.15⇥ Yii1.1∞(Úœç€L∞Ñ—ÜòπÍ/€LÙ§Ù∞âh✏I⇢ç ∞Ñü ˝y'Ñe⇥ Yii2.0/(Yii1.1Ñ˙@⌦åh®✓Õ∞ôÑ*F∂86ÜSM∞ÑÄ/å— -;AѶö⇥ Yii2.0/∞„Web—F∂Ñ„h⇥ Å"Yii—‚æ BæÑ»™õ:'Web—⇧ ÜÇd⇠¿ÑF∂⇥ ,∫ ÍYii1.1w1À•Êv(YiiÜ1éÂ\å1}s˚_•Ê«õF∂Ü⇥;ÑÙÛ ˘Yiià ·✏ √ÍÑ/$π⇢fÜYii1f0Ü∏⇢S↵AL⇣üÑ⌧—wÎ9€e Î⇥ 3 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1.2 ÃÃà ooo  ∆∆∆ ˜Y✏}6,Y↵ÂYii2:;ÅÖπFvÅB˚⇧w Yii1.1Ñ—œå⇥ }6w Ÿõà oÂ∆ÔÂÙÎÑå·Yii2F(Y↵-⌘⇢.©° Yii1.1¯sÂ∆Ñ˚⇧eE sÑÇı⇥ ÍÅ ÜŸõÇı˚⇧vÅŒ4f`Yii1.11ÔÂÙ•⌦KYii2Ü⇥ S6Yii\:*PHPF∂˚⇧↵À}˝Ü„↵PHPvÅ⇢æ⇢ÍÅ↵ó¬„ ⇢ôÄUÑ„↵ш⇡'ÇÂSÅ(Íõ˝p1˙,≥Üπ(πf_/Õf` π’⇥ ˆYiiÿ/*b⌘˘aÑF∂⇥Ÿ✏s@(„ƒ«åÓò„≥ÑÔ⌦Yii˝S∞Üb ⌘˘aÑÛ⇥ Å(Yiie—}_Åu™ŸÛ⇥‡d˚⇧}˘b⌘˘a↵ öÑ Ü„⇥ vû↵*↵èX4sÑÿNU/˘é–ÕÌ–Õ—F∂Ñü√↵¶⇥ ÙÕÅ Ñ/↵v„≥ÓòÑÔåπ’⇥v-'{π’1/b⌘˘aπ’⇥ ŒŸπe↵}6f`å (YiivÅ⇢ÿÒÑb⌘˘aÑπ’⇥ F\:«e∫⌘ÿ/⌅M˚⇧↵ÀÔÂ˚fl0hb 0f`b⌘˘aÑ—π’⇥ y+/Web—-8(Ñæ°!✏,Y↵_⇢(â 0ˆ€L◆Ë≤ „⇥ 1.3 Yii2.0ÓÓÓ MMM ÑÑÑ ∂∂∂ Yii2.0ÓM/BetaH,⇥ÍAlphaHÀòπ˙ÆÅ⌃Yii2.0(éß¡FÔÂ(évf`⇥ (—⇤BetaHKòπ»ÙÍAlphaÂeÚœ ∏⇢QŸåî(((Yii2.0—Ü(∂ µ3ö⇥ BetaH⇢â 0∂Ñ •„πbÑÓ9;Å/ácåÑåBugÓ⇥ Œ*∫Ñ(œå↵(AlphaˆnûX(1éF∂„ÑÙ∞¸ÙüeÍÒôÑ˝c8– L„ ˙∞´ô⇡ÔÑ≈µ⇥'Ç 78!'⇥Fœ!˙∞Óòˆ9nYiiÑ⇤’·oÔ öM⇡Ô„ÑMnåü‡ àÎÔÂ$≠/1éF∂Ó9Ñü‡ ⇣Ñ⇥èÍÅÂ↵∞ ÑYii2.0„ìÂ↵—–§Ñcomment 1Ô—∞„ì-Ó9ÑÖπÜ⇥9n∞ÑF∂„ ˘ÍÒÑ„\˙¯îÑ⇤tsÔ⇥ îÂÙ(AlphaH,ˆ}6X(„3öÑ≈µF˙∞∂µÑ!p˘⌘*∫/Ô•◊ Ñ íÂweàπ◆„⇤twe_æ⇥0ÜBetaH,ÓM:bÿ*˙∞{<Óò⇥ ŸÃÍ/¢¬≤Yii2.0ÓM@⌅Ñ∂⇥˘éf`vπ®ç⌘Ï˘YiiÑÒef`⇥ ˘éß—Çú˘é'ãî(‚'ƒ!—Ñ:o˙ÆÔÂHIIIc✏H—⇤ç (⇥ Çú/✏ãî(23∫✏‚—*∫—£HÙ•⌦Yii2.0/° *'ÓòÑ⇥ Í /œ!Ù∞F∂„MY✏F∂„Ó9ÑÖπvà˘¯îÑÓ9\˙⇤’n›ÍÒÑ„◊ qÕ⇥ 1.4 Yii2.0 ˘˘˘ ‘‘‘ Yii1.1 ÑÑÑ ÕÕÕ ''' ÿÿÿ(TBD) ŸÇ/◆Ë:Úœ Yii1.1˙@Ñ˚⇧↵ÀôÑ⇥⌃Yii2.0Yii1.1Ñ@Õô˙e˘‘fw e⇢Îó⇢⇥ ˘éŒ*•Ê«YiiÑ˚⇧↵ÀŸÇÑÖπkk1ÔÂÜ\:˘«ÄÜÚÑ *Ü„1Ü⇥ Çú ÑÖπ`ˆ°↵}_Å'b⇢≤⇧ZÑ⇥Ê° Yii1.1Ñœ åv®ç˘Yii2.0Ñf`⇥ Yiiòπ ◆ËÑácR≥;”1.1H,å2.0H,Ñ⇥Â↵Öπ;ÅeÍéòπÑác⌘Z Ü↵æÄ È‘ÉÕÅÑÿv†eÜõ*∫Ñœå⇥ 1.4.1 PHP∞∞∞ yyy ''' Œ˘PHP∞y'Ñ(⌦$⇧1X(à'⇥Yii2.0'œ(ÜPHPÑ∞y'Ÿ(Yii1.1-/ ° Ñ⇥‡dPHP2.0˘éPHPÑH,ÅBÙÿÅBPHP5.4 Â⌦⇥Yii2.0-(0ÑPHP∞y' ;Å ⇢ • }zÙ(Namespace) 4 Chapter 1. ¸¸¸ ˚˚˚ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 • ?˝p • pƒÌÌ’b✏⇢ [1,2,3] ÷„ array(1,2,3) ⇥Ÿ(⇢ÙpƒLWpƒ-„Ù⇧p ÄÌ⇥ • (∆˛áˆ-(PHPÑ WÑ/˘éPHP}zÙÑ(⇥Yii1.1-° }zÙÙ : MYii8√{(7ÍöI{Ñ}≤Å@ ÑYii8√{Ñ}G†Â C MÂ::+⇥ Yii2.0-@ 8√{˝(Ü}zÙ‡d C M_1∫‡ƒ˙ÜÚÜ⇥ }zÙûEÔѯsT‘Ç yii\base\Object ˘îYiiÓU↵Ñ base/Object.php á ˆ⇥ 1.4.3 ˙˙˙ @@@ {{{ Yii1.1-  ( Ü*˙ @ { CComponent – õ Ü ^ ' / I ˙ , ü ˝ ‡ d ‡ N @ ÑYii8 √ { ˝ > Í Â { ⇥ 0 ÜYii2.0 ⌃ ∂ Ï ' Ñ CComponent € L Ü ∆ ⌃ ⇥ ∆ ⌃ ⇣ Ü yii\base\Object å yii\base\Component ⇥ ∆⌃Ñ⇤Q;Å/ CComponent >'â q Õ'˝KÃ⇥ é/Yii2.0-ä yii\base\Object öMéÍÅ^'/‡ãˆL:⇥ yii\base\Component ⇡(M⇧Ñ˙@⌦†e˘éãˆåL:Ñ/⇥ Ÿ7—⇧ÔÂ9n Å ÈÁÍÍ˙@{⇥ Ÿü˝⌦Ñn✓⌃&eÜHá⌦Ñ–G⇥(≈h:˙@pn”Ñ^Õ ¢¬ãiÑ≈µ ↵ yii\base\Object ‘É⇥(⇥ <ó–Ñ/ yii\base\Object yii\base\Component $⇧v/BßÑM⇧/ ⇧÷9⇥ 1.4.4 ãã㈈ˆ(Event) (Yii1.1-⇢«* on MÑπ’e˙ãˆ‘Ç CActiveRecord -Ñ onBeforeSave() ⇥ (Yii2.0-ÔÂ˚✏öIãˆÑvÍÒÊ—É: 1 $event = new \yii\base\Event; 2 // ( trigger() ʗ㈠3 $component->trigger($eventName, $event); 4 5 // ( on() Mãˆhandler˘a—ö 6 $component->on($eventName, $handler); 7 // ( off() „d—ö 8 $component->off($eventName, $handler); 1.4. Yii2.0 ˘˘˘ ‘‘‘ Yii1.1 ÑÑÑ ÕÕÕ ''' ÿÿÿ(TBD) 5 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1.4.5 +++ (Alias) Yii2.0-9ÿÜYii1.1-+Ñ(b✏vi'Ü+Ñ⇤t⇥ Yii1.1-+ . Ñb✏(: RootAlias.path.to.target (Yii2.0-+ @ MÑπ✏(: @yii/jui ÊYii2.0-≈ ÔÑ+ÿ URL+: 1 // ÔÑ+ 2 Yii::setAlias(’@foo’, ’/path/to/foo’); 3 4 // URL+ 5 Yii::setAlias(’@bar’, ’http://www.example.com’); Ê}zÙ/'∆¯sÑYii˙Æ:@ 9}zÙ˝öI*+‘Ç⌦b–0Ñ yii\base\Object ãû⌦/öIÜ @yii Ñ+h:Yii(˚fl-Ñâ≈ÔÑ⇥ Ÿ7eYii1 ˝9n}zÙ~0ûEÑ{áˆ@(ÔÑvÍ®†}⇥Ÿπ⌦Yii2.0Yii1.1v° ,(:+⇥ Çú° :9}zÙöI+⇡Å€LùÑMn⇥⌃}zÙûEÔÑÑ ⌅s˚ JÂYii⇥ sé+ÑÙÊ∆Öπ˜↵ + ⇥ 1.4.6 ∆∆∆ ˛˛˛ (View) Yii1.1-MVCmodel-view-controller -Ñ∆˛Ù/ùVéControllerÑv^/c✏I⌦Ï ÀÑView⇥ Yii2.0eÜ yii\web\View {óViewåhÏÀ⇥Ÿ_/*¯SÕÅÿ⇥ ñHYii2.0-View\:ApplicationÑ*ƒˆÔÂ(h@-„-€LøÓ⇥ ‡d∆˛2 ”„≈ç@PéController-Widget-⇥ v!Yii1.1-∆˛áˆ-Ñ $this ⌥Ñ/SMß6h( Yii2.0-⌥Ñ/∆˛,´⇥ Å(∆ ˛-øÓß6h( $this->context ⇥ ˆYii1.1-Ñ CClientScript _´ÿpܯsÑMÔDê vùVs˚Ñ°⌃§1Assert Bundle yii\web\AssertBundle e◆L⌅⌃⇥ *Assert Bundle„h˚⌫ÑMÔDêŸõMÔD êÂÓUb✏€L°⌃Ÿ7>óÙ è⇥ Ù:ÕÅÑ/Yii1.1-Å`<Ë✏Dê(HTML-Ñ zè‘ÇCSSáˆÑzèbÑ⇢Ü÷MbÑ JavaScriptáˆÑzèMzè˙⇡⇢¸Ù Ñ ì*†} I⇥ (Yii2.0-(*Assert BundleÔÂöIùVéÊÑ*⇢*Assert BundleÑ s˚ Ÿ7(⌘HTMLubËåŸõCSS⇧JavaScriptˆYii2.0⇢Í®ä@ùVÑáˆHËå⌦⇥ (∆˛!HŒπbYii2.0Õ6(PHP\:;ÅÑ!HÌ⇥ ˆòπ–õÜ$*iUÂ/ SM$';APHP!HŒ⇢SmartyåTwig˘éPardoŒòπç–õ/Ü⇥ S6—⇧Ô Â⇢«æn yii\web\View::$renderers e(v÷!H⇥ ÊYii1.1-⇤( $this->render(’viewFile’, ...) /Å( echo }‰Ñ⇥ Yii2.0-∞ó render() Í/‘fi∆˛2””úv˘Ù•>:˙eÅÍÒ⇤( echo echo $this->render(’_item’,[’item’ => $item]); Çú )`—∞HYiiì˙Ü*z}uŸ`1ÅË✏//ÿ∞( echo Ü⇥ ÿ+ÙŸ *⇡Ôà8¡y+/(˘Ajax˜B\˙Õîˆ⇢Ùæ—∞Ÿ⇡Ô⇥˜`Ï↵ˆY✏⇥ ( ∆ ˛ Ñ ; ò(Theme) π b Yii2.0Ñ – \ : ⌃ « ( Ü å h Ñ π ✏ ⇥ (Yii2.0-  ( Ô Ñ ⌅ Ñ π ✏ ⌃ *ê ∆ ˛ á ˆ Ô Ñ ⌅ ⇣ *;ò Ñ ∆ ˛ á ˆ Ô Ñ ⇥ ‡ d [’/web/views’ => ’/web/themes/basic’] ö IÜ*;ò ⌅ s ˚ ê ∆ ˛ á ˆ /web/views/site/index.php ;ò⌃/ /web/themes/basic/site/index.php ⇥ ‡d Yii1.1-Ñ CThemeManager _´ÿpÜ⇥ 6 Chapter 1. ¸¸¸ ˚˚˚ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1.4.7 !!! ããã (Model) MVC-ÑM⌥Ñ1/!ãYii1.1-( CModel eh:Yii2.0( yii\base\Model eh :⇥ Yii1.1- CFormModel (eh:(7ÑhUìeÂ:+épnì-Ñh⇥ Ÿ(Yii2.0-_´ÿ pYii2.0>⌘é(ÁÍ yii\base\Model eh:–§ÑhUpn⇥ ÊYii2.0:ModeleÜ yii\base\Model::load() å yii\base\Model::loadMutiple() $*∞Ñπ’ (éÄ⌃(7ìeÑhUpnK<ŸModel: 1 // Yii2.0(load()Ié↵bÑ(’ 2 $model = new Post; 3 if ($model->load($_POST)) { 4 ... ... 5 } 6 7 // Yii1.1-8(ÑWÔ 8 if (isset($_POST[’Post’])) { 9 $model->attributes = $_POST[’Post’]; 10 } Ê*ÕÅÿ1/Yii2.0-eÜ:oÑÇıó*ModelÔÂ⇥(éÑ:o và ˘Ñ:o≥öÍõWµ H⇥‘Ǣé(7!ã(Ëå:o-⇢8Å(7ìe$!∆ €Ln§ ({F:o-∆ÍÅìe!⇥(Yii1.1-à˘ŸÕ≈µ⇢8Å $*Modele û∞⇥ (Yii2.0-ÍÅ*Modelv⇢« yii\base\Model::secnarios() 1 public function scenarios() 2 { 3 return [ 4 ’login’ => [’username’, ’password’], 5 ’registeration’ => [’username’, ’password’, ’repeat_password’], 6 ]; 7 } Ÿ_✏s@ unsafe å¡(Yii2.0-_° ÜÀ≥K0·/ unsafe ÑWµ1(yöÑ: o-⌫˙e⇥ ⇧ŸŸ*Wµ†⌦ ! M⇥ 1.4.8 ßßß 666 hhh (Controller) dÜ⌦b≤0Ñß6h-Å( echo e>:2”∆˛Ñì˙Ÿπ:+ Yii1.1Yii2.0Ñß6h ÿh∞˙Ù:>Ñ:+£1/®\«‰h(Action Filter) Ñ⇥ (Yii2.0-®\«‰hÂL:(behavior)Ñπ✏˙∞ ,ÁÍ yii\base\ActionFilter vËe0*ß6h-—\(⇥‘ÇYii1.1-à8¡Ñ: 1 public function behaviors() 2 { 3 return [ 4 ’access’ => [ 5 ’class’ => ’yii\filters\AccessControl’, 6 ’rules’ => [ 7 [’allow’ => true, ’actions’ => [’admin’], ’roles’ => [’@’]], 8 ], 9 ], 10 ]; 11 } ↵@// πœF»nû7 1.4. Yii2.0 ˘˘˘ ‘‘‘ Yii1.1 ÑÑÑ ÕÕÕ ''' ÿÿÿ(TBD) 7 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1.4.9 Active Record ÿ ∞ ó H (Yii1.1- p n ì  ‚ ´ ⌃ c ⇣ CDbCommand CDbCriteria å CDbCommandBuilder ⇥ @◆)↵'ø⌃E≈0ÜYii2.0«( yii\db\Query eh:pnì ‚: 1 $query = new \yii\db\Query(); 2 $query->select(’id, name’) 3 ->from(’user’) 4 ->limit(10); 5 6 $command = $query->createCommand(); 7 $sql = $command->sql; 8 $rows = $command->queryAll(); =Ñ/ yii\db\Query ÔÂ( Active Record-((Yii1.1-Å”$⇧vπ ◆⇥ Active Record(Yii2.0-'Ñÿ*/‚ÑÑ˙Ê*/sT‚Ñ⌅⌃⇥ Yii1.1- Ñ CDbCriteria (Yii2.0- ´ yii\db\ActiveQuery @ ÷ „ Ÿ * ä M à Õ { ( ô È ⌦ Ñ ∂ ⇡ Á  Í yii\db\Query @ Â Ô Â € L { < ⌦ b „ Ñ Â ‚ ⇥ ⇤ ( yii\db\ActiveRecord::find() 1ÔÂ/®Â‚ÑÑ˙Ü: $customers = Customer::find() ->where([’status’ => $active]) ->orderBy(’id’) ->all(); Ÿ(Yii1.1-/π◆û∞Ñ⇥y+/‘ÉBÑ‚s˚⇥ (sT‚πbYii1.1/(*flÑ0π relations() öIsTs˚⇥ Yii2.09ÿÜŸZ ’öI*sTs˚⇢ • öI*getterπ’ • getterπ’Ñπ’h:sTs˚ÑÇ getOrders() h:s˚ orders • getterπ’-öIsTÑùn⇢8/.s˚ • getter‘fi* yii\db\ActiveQuery ˘a ‘ÇÂ↵„1öIÜ Customer Ñ orders sTs˚: 1 class Customer extends \yii\db\ActiveRecord 2 { 3 ... ... 4 5 public function getOrders() 6 { 7 // sTÑùn/ Order.customer_id = Customer.id 8 return $this->hasMany(’Order’,[’customer_id’ => ’id’]); 9 } 10 } Ÿ7Ñ›ÔÂ⇢« Customer øÓsTÑ Order 1 // ∑÷@ SM $customer sTÑ orders 2 $orders = $customer->orders; 3 4 // ∑÷@ sT orders -status=1 Ñ orders 5 $orders = $customer->getOrders()->andWhere(’status=1’)->all(); ˘ésT‚ ÔÅÑπ✏_ àÅÑπ✏⇥:+(é«(ÔÅπ✏ˆsTÑ‚⇢vg L àÅπ✏ˆ≈(>:⇤(sT∞UˆP⇢gLsTÑ‚⇥ 8 Chapter 1. ¸¸¸ ˚˚˚ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 (ÔÅπ✏Ñû∞⌦Yii2.0Yii1.1_X(⇥Yii1.1(*JOIN‚ eû∞ˆÂ‚;∞ U vsTÑ∞U⇥ Yii2.0⇤(JOIN‚Ñπ✏($*zèÑSQLÌ ,*Ì‚;∞ U,å*ÌÂ9n,*ÌÂÑ‘fi”ú€L«‰⇥ ˆYii2.0:Active RecordeÜ asArray() π’⇥(‘fi'œ∞UˆÔÂÂpƒb✏›X ç¢ab✏›XŸ7ÔÂǶ'œÑzÙ–ÿHá⇥ Ê*ÿ/(Yii1.1-WµÑÿ§<ÔÂ⇢«:{Ñpublic ⇣XÿœKÀ⇥ /ÙŸõÖ π/q®t*YiiF∂Ñ˙Û⇥ŸõÂ∆˘éYii-@ Ñ{‡N˝⇥( _/⌃„t*Yii@≈{w⌥Ñ ˙@⇥ ÷Ï⌃+/^'property ãˆevent L:hebavior ⇥ 2.1 ^^^ ''' Yii/*Øb⌘˘aÑF∂⇥‡d,‡;ÅÀÕYii- sb⌘˘aÑ˙@Â∆⇢^' property ãˆevent L:behavior I⇥ 2.1.1 ˘˘˘ aaa object ååå ƒƒƒ ˆˆˆ component MbÙ«òπ⌃YiiöMé*˙郈ÑF∂⇥Ô¡ƒˆŸÇı/YiiÑ˙@⇥Çú` t£⇧˚YiiÑê„/APIác`⌃⇢—∞Yii‡N@ Ñ8√{˝>éÁÍ yii\base\Component ⇥ Yii1.1/ 1 Ú œ ÜcomponentÜ ⇥Yii2⌃Yii1- ÑCComponent∆ ⌃ ⇣ $*{ ⇢ yii\base\Object å yii\base\Component ⇥Object‘ É{œ ß õ ⇢ «getteråsetterö IÜ { Ñ ^ ' property ⇥Component> ÍObject v / ãˆevent å L : behavior ⇥ ‡ dComponent{w *ÕÅÑy'⇢ • ^'property • ãˆevent • L:behavior ¯·`⇢⌘Ü„«Ÿ *y'/0Ãå”U{ü˝9ÿ{L:ÑÕÅ⌥eπ⇥ 2.1.2 ^^^ ''' property ^'(éhÅ{Ñ∂ŒøÓÑb✏⌦↵^'⇣Xÿœ° :+`˝<↵˙ $object->foo -Ñ foo /⇣Xÿœÿ/^'H>6L⇥⇣Xÿœå^'Ñ:+T˚(é⇢ • ⇣Xÿœ/^'^'ö/⇣Xÿœ⇥F,≈µ↵^'⇢1–*–õ⇣Xÿœeh :Ÿõ⇣Xÿœ⇢8/¡ Ñ⇥ • ⇣Xÿœ° ˚ôCPß6^'ÔÂ⌥ö:Í˚Íô⇥ • ⇣Xÿœ˘˚˙\˚U⌅⌃˘ôe\˚UÑ⌅⌃^'ÔÂ⇥ (Yii-1 yii\base\Object –õܢ^'Ñ/‡dÇúÅ`Ñ{/^'≈{Á Í yii\base\Object ⇥Yii-^'/⇢«PHPÑT’˝p __get() __set() eß\(Ñ⇥↵b Ñ„/ yii\base\Object {˘é __get() å __set() ÑöI: 11 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1 public function __get($name) // ŸÃ$name/^' 2 { 3 $getter = ’get’ . $name; // getter˝pÑ˝p 4 if (method_exists($this, $getter)) { 5 return $this->$getter(); // ⇤(Ügetter˝p 6 } elseif (method_exists($this, ’set’ . $name)) { 7 throw new InvalidCallException(’Getting write-only property: ’ . get_class($this) . ’::’ . $name); 8 } else { 9 throw new UnknownPropertyException(’Getting unknown property: ’ . get_class($this) . ’::’ . $name); 10 } 11 } 12 13 public function __set($name, $value) // $name/^'$value/flôeÑ^'< 14 { 15 $setter = ’set’ . $name; // setter˝pÑ˝p 16 if (method_exists($this, $setter)) { 17 $this->$setter($value); // ⇤(setter˝p 18 } elseif (method_exists($this, ’get’ . $name)) { 19 throw new InvalidCallException(’Setting read-only property: ’ . get_class($this) . ’::’ . $name); 20 } else { 21 throw new UnknownPropertyException(’Setting unknown property: ’ . get_class($this) . ’::’ . $name); 22 } 23 } ⌘ÏÂS(øÓåôe˘aÑ*X(Ñ⇣Xÿœˆ __get() __set() ⇢´Í® ⇤(Yiic/)(Ÿπ–õ˘^'Ñ/Ñ⇥Œ⌦bÑ„-ÔÂ↵˙ÇúøÓ*˘ aÑ–*^'Yii⇢⇤(: get^'() Ñ˝p⇥Ç SomeObject->Foo ⇢Í®⇤( SomeObject->getFoo() ⇥ÇúÓ9–^'⇢⇤(¯îÑsetter˝p⇥Ç SomeObject->Foo = $someValue ⇢Í®⇤( SomeObject->setFoo($someValue) ⇥ ‡dÅû∞^'⇢8 *e§⇢ • ÁÍ yii\base\Object ⇥ • *(é›XÂ^'Ñ¡ ⇣Xÿœ⇥ • –õgettersetter˝p$⇧˝–õ(éøÓÓ9⌦b–0Ñ¡ ⇣Xÿœ⇥ÇúÍ–õ Ügetter£HÂ^':Í˚^'Í–õÜsetter⇡:Íô⇥ Ç↵ÑPost{û∞ÜÔ˚ÔôÑ^'title: 1 class Post extend yii\base\Object // ,e⇢ÁÍ yii\base\Object 2 { 3 private $_title; // ,åe⇢*¡ ⇣Xÿœ 4 5 public function getTitle() // , e⇢–õgetteråsetter 6 { 7 return $this->_title; 8 } 9 10 public function setTitle($value) 11 { 12 $this->_title = trim($value); 13 } 14 } Œ⌃∫⌦e≤⌃ private $_title ô⇣ public $title _/ÔÂû∞˘ $post->title Ñ˚ôÑ⇥FŸ/}Ñ`Ô⌃1Ç↵⇢ • 1ªÜ{Ñ≈'⇥,⇣Xÿœ˘Ô¡/‘É}Ñ↵`Ô⇥ŒŸÃ`_∏°↵˙ eF/GÇ )`Û©(7Ó9⌥òÜ`H9Hn›„-° Ù•Ó9⌥ òÇú–õÜsetterÍÅäsetter â£HÊ °⇧⌃r¿Ñ˘⌥òÑôe1⇢õ˙⇥ 8⇥( public $title Ñπ’Ñ›`9⇣ private $title ÔÂíÂôeÑ⇥8F /˚÷Ñ_´ÅbÜ⇥ 12 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 • ˘é⌥òÑôe`Ûªâz<(setterÑπ’ÍÅœ⌦bÑ„µ7(Ÿ*0π⇤( trim() 1ÔÂÜ⇥FÇú( public $title Ñπ’£H·ëIœ*ôe̽Å⇤ ( trim() ⇥`˝›¡° ⌅W✏ ( public $title /ˆKÎ↵weÄUF ÑÓ9/*ªÊã⇥ÄÙÔÂÙ/v¶⇥ Ÿ1/oˆÂ↵Ñ✏I@(⇢«öÑπ’„◆éÙ§øéÓ9⇥ˆ↵@}œ°≈ÅF ûE⌦⇤«èÑ↵À⇧´¢7<@Ó9⌦*↵èXôÑ„Ó⇡«÷≤∫Ñ˝⇢…óŸ/ A⌃≈ÅÑ⇥ <óË✏Ñ/⇢ • 1éÍ®⇤( __get() __set() ш:≈≈—(øÓX(Ñ⇣Xÿœˆ⇥‡dÇúöI Ü⇣Xÿœ public $title £H1óöIÜ getTitle() setTitle() ÷Ï_⇢´⇤ (⇥‡: $post->title ˆ⇢Ù•⌥⌘ pulic $title , __get() __set() /⇢´⇤( Ñ⇥Œ9⌦1´⌥≠Ü⇥ • 1 éPHP˘ é { π ’ : ⌃ ' ✏ ô s ' ✏ ô O $post->getTitle() å $post->gettitle() /⇤(¯Ñ˝p⇥‡d $post->title å $post->Title /* ^'⇥ • 1é __get() __set() ˝/publicч∫⌃ getTitle() setTitle() : public, private, protected˝° ✏IË7˝/ÔÂøÓ⇥@Â@ Ñ^'˝/public Ñ⇥ • 1é __get() __set() ˝/staticчd° û’(static Ñ^'⇥ 2.1.3 ObjectÑÑÑ vvv ÷÷÷^^^ ''' ¯¯¯ sss ÑÑÑ πππ ’’’ dÜ __get() __set() K yii\base\Object ÿ–õÜÂ↵π’øé(^'⇢ • __isset() (éK’^'property) ˆ´Í®⇤ (⇥Ë✏Â^'Å ¯îÑgetter⇥ • __unset() (é⌃^'<æ: null ( unset($object->property) ˆ´Í®⇤(⇥Ë✏ Â^'Å ¯îÑsetter⇥ • hasProperty() (éK’/& –*^'⇥söIÜgettersetter⇥Çú hasProperty() Ñ ¬p $checkVars = true ÿ§:true £HÍÅw Ñ⇣Xÿœ_§:w Â^' ÇMb–0Ñ public $title ⇥ • canGetProperty() K’*^'/&Ô˚¬p $checkVars Ñ✏I⌦⇥ • canSetProperty() K’*^'/&Ôô¬p $checkVars Ñ✏I⌦⇥ 2.1.4 ComponentÑÑÑ ^^^ ''' yii\base\Component ÁÍ yii\base\Object ‡d÷_w ^'I˙,ü˝⇥ F/1éComponetÿeÜãˆL:‡dÉv^ÄUÁÜObjectÑ^'û∞π✏/ ˙é7Ñ:6Õ}Ü __get() __set() I˝p⇥FŒû∞:6⌦e≤/7Ñ⇥Ÿ*qÕ⌃ „⇥ 2.1.5 ObjectÑÑÑ MMM nnn πππ ’’’ Yii–õÜ*flÑMn˘aÑπ✏⇥Ÿπ✏/t*Yii⇥Application˘aÑMn1/ŸÕMn π✏ÑS∞: 1 $config = yii\helpers\ArrayHelper::merge( 2 require(__DIR__ . ’/../../common/config/main.php’), 3 require(__DIR__ . ’/../../common/config/main-local.php’), 4 require(__DIR__ . ’/../config/main.php’), 5 require(__DIR__ . ’/../config/main-local.php’) 6 ); 2.1. ^^^ ''' 13 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 7 8 $application = new yii\web\Application($config); $config ↵@BF,(⌦1/*⌅ÕMnyÑpƒ⇥Yii-1/fl(pƒÑπ✏˘˘a €LMnû∞Ÿ⌥Ñs.1( yii\base\Object öIÑÑ ˝p-: 1 public function __construct($config = []) 2 { 3 if (!empty($config)) { 4 Yii::configure($this, $config); 5 } 6 $this->init(); 7 } @ yii\base\Object ÑÑ˙A↵/⇢ • Ñ˙˝p $config pƒ:¬p´Í®⇤(⇥ • Ñ˙˝p⇤( Yii::configure() ˘˘a€LMn⇥ • (Ñ ˝p⇤(˘aÑ init() π’€LÀ⇥ pƒMn˘aÑÿ∆( Yii::configure() -FÙ4Üvû_° ¿H^GÑ: 1 public static function configure($object, $properties) 2 { 3 foreach ($properties as $name => $value){ 4 $object->$name = $value; 5 } 6 7 return $object; 8 } MnÑ«↵1/MÜ $config Mnpƒ⌃pƒÑ.\:^'¢îÑpƒC Ñ<˘˘a Ñ^'K<⇥‡dû∞YiiŸflÑMnπ✏ÑÅπ ⇢ • ÁÍ yii\base\Object ⇥ • ˘˘a^'–õsetterπ’Âcn⌅⌃Mn«↵⇥ • ÇúÅÕ}Ñ ˝p˜⌃ $config \:ÂÑ ˝pÑ*¬pv⌃¬p ✓Ÿ6Ñ ˝p⇥ • Õ}ÑÑ ˝pÑö∞ó⇤(6Ñ ˝p⇥ • ÇúÕ}Ü yii\base\Object::Init() ˝pË✏öÅ(Õ}˝pÑ4⇤(6{Ñ Init() ⇥ ÍÅû∞ÜÂ⌦Åπ1ÔÂó`ôÑ{Ô gYii¶ö◊⇣Ñπ✏€LMn⇥Ÿ(ô„ Ñ«↵-&e∏⇢ø)⇥ œ`ŸHjÑØö⇢–˙eÇúMnpƒ/Ñ–*Mny_/*pƒŸHûÇú –*˘aÑ^'_/*˘a^*ÄUÑprequest -Ñ request ^'1/*˘ a⇥£H(Mn $app ˆ≈6ÅMn0Ÿ* reqeust ˘a⇥‚6 request _/*˘a£H÷ ÑMnÅ/ gYiiуÈe_1/(*pƒeMnÉ⇥‡d⌦b–0ÑŸ$*ÓòÄÄ/( Ñ⇥ £HHû∞bÿ∆(ésetter˝p⇥1é $app (€LMnˆ»⇢⇤( configure ˝p⇥½p»:⌃Mny/ÄUÑp<ÿ/pƒ1Ù•( $object->$name = $value å⇣^'ÑK<⇥£H˘é˘a^'vMn< $value /*pƒ:ÜvcnMn⇥` Å(vsetter˝p⌦Z˙cnÑ⌅⌃π✏⇥xãÑYiiî( yii\web\Application 1/ù`öI ◆ËÑsetter˝pû∞Í®⌅⌃MnyÑ⇥‘Ç⌘Ï(YiiÑMnáˆ-ÔÂ↵0*Mny components ,≈µ↵÷ÑÖπ/Ÿ7Ñ: 14 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1 ’components’ => [ 2 ’request’ => [ 3 // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 4 ’cookieValidationKey’ => ’v7mBbyetv4ls7t8UIqQ2IBO60jY_wf_U’, 5 ], 6 ’user’ => [ 7 ’identityClass’ => ’common\models\User’, 8 ’enableAutoLogin’ => true, 9 ], 10 ’log’ => [ 11 ’traceLevel’ => YII_DEBUG ? 3 : 0, 12 ’targets’ => [ 13 [ 14 ’class’ => ’yii\log\FileTarget’, 15 ’levels’ => [’error’, ’warning’], 16 ], 17 ], 18 ], 19 ’errorHandler’ => [ 20 ’errorAction’ => ’site/error’, 21 ], 22 ], Ÿ/*xãÑpƒ/LWpƒ⇥£HYii/ÇUä÷ÏMn}ÑbjÑ`Øö Û0ÜYiiö/öIÜ*: setComponents Ñsetter˝p⇥S6Yiiv*⌃½p>( yii\web\Application Ã/>(Ü yii\di\ServiceLocator Ãb: 1 public function setComponents($components) 2 { 3 foreach ($components as $id => $component){ 4 $this->set($id, $component); 5 } 6 } ŸÃ *⇣X˝p $this->set() ⌘ÏÇ≤Ÿ*⌧ÍÅÂSŸ*˝päMnáˆ-Ñ components Mnyö1ÔÂÜ⇥Ûé yii\web\Application å yii\di\ServiceLocator Ñ s˚1/M⇧ÁÍ⇧F/Ù•ÁÊ«Ÿ*s˚v'Ü⇥ F/cÇlÑ$b(–õø)шcomponent1éeventåbehaviorŸ$*y'_ zrÜöÑHá⇥Çú—-Å(eventåbehaviorŸ$*y'‘Çh:pnÑ{£ HÔÂŒComponentÁŒObjectÁ⇥¯‘éComponentObjectÍ–õÜproperty° eventåbehavior⇥xãÑî(:o1/Çúh:(7ìeуpn£H(Object⇥y+/ ÅÂ*'pƒeh:pn£HpƒC ,«(Object/«(Component⇥ÇúÅ ˘˘aÑL:å˝Õî⌅⌃Ñ㈀L⌅⌃·ëÓîS«(Component⇥ŒHáe≤ObjectÙ• —üÑPHP{‡d(Ô˝Ñ≈µ↵îS⇠H(Object⇥ ˘é@ ÑObject⇧ÏComponentu™Â↵}h⇢ 1. ÑÀ6µ⇥ÔÂ(Ÿ6µænpropertyÑÿ§<⇥ 2. ˘aMn6µ⇥_1/Mb–0Ñ ˝pÑ $config ¬p⇥Ÿ6µÔÂÜ÷M6µæn ÑpropertyÑÿ§√ ('⇥ 2.1. ^^^ ''' 15 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 2.2 ãã㈈ˆ Event (ãˆÔÂ(yπшπÊ—gLÑHæöѵ„㈂/„„&ÑÕπ✏_ /æ°⇢°A↵ÑÕ!✏⇥∞„oˆ-㈇⌅(‘Ç`—Ü*ÆZÊ—Ü*㈸ ÙsË`Ñ∫↵0Ü`∞—˙eÑÖπ⇥˘é㈠ŸH‡*Å ⇢ • Ÿ/*¿Hãˆ*oˆ˚flà ¯⇢㈗⇤∞ÆZ/㈠dÆZ_/Õã ˆ⇥ • Ê—Üãˆ`—ÑÆZ1/`Ê—Ñãˆ⇥ • #—,Ÿ*ãˆ⇧˝ÂSŸ*㈗ܰh⌦⌅⌃(7ËåÑ!WØö⇢ 60`—˙∞ÆZÑãˆ⇥ • ãˆH⌅⌃˘é—⇤∞ÆZÑãˆ1/⇢ÂsËÜ`Ñv÷(7⇥ • ㈯spn/¿H˘é—⇤∞ÆZãˆ⇧+ÑpnÛ⌘Å ∞ÆZÑÖπˆÙI⇥ 2.2.1 Yii---ãã㈈ˆ¯¯¯ sss ÑÑÑ {{{ Yii-ãˆ/( yii\base\Component -eÑË✏ yii\base\Object /ãˆ⇥@ ÂS`Å(㈈˜Œ yii\base\Component €LÁ⇥ˆYii-ÿ *ãˆ'∆ ¯sÑ yii\base\Event ÷≈Ü㈯sÑ spnv–õõü˝˝p\:Ö©: 1 class Event extends Object 2 { 3 public $name; // ㈠4 public $sender; // ㈗⇤⇧⇢8/⇤(Ü trigger() Ñ˘a{⇥ 5 public $handled = false; // /&»bãˆÑÌ⌅⌃ 6 public $data; // ㈯spn 7 8 private static $_events = []; 9 10 public static function on($class, $name, $handler, $data = null, $append = true) 11 { 12 // ... ... 13 // (é—öãˆhandler 14 } 15 16 public static function off($class, $name, $handler = null) 17 { 18 // ... ... 19 // (é÷àãˆhandler—ö 20 } 21 22 public static function hasHandlers($class, $name) 23 { 24 // ... ... 25 // (é$≠/& ¯îÑhandlerãˆ˘î 26 } 27 28 public static function trigger($class, $name, $event = null) 29 { 30 // ... ... 31 // (éʗ㈠32 } 33 } 16 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 2.2.2 ãã㈈ˆhandler @◆ãˆhandler1/ãˆ⌅⌃↵è#ãˆÊ—HûÑÓò⇥Œ,(⌦e≤*ã ˆhandler1/µPHP„s*PHP˝p⇥˘é*ãˆhandlerÔÂ/Â↵Ñb✏–õ⇢ • *PHPh@˝pÑ˝p&¬påϘI√√Ñ1*˝p⇥Ç trim Ë✏/ trim($str) _/ trim() ⇥ • *˘aÑπ’*{ÑYπ’⇥Ç $person->sayHello() ÔÂ(:ãˆhandler FÅ9ô⇣ÂpƒÑb✏ [$person, ’sayHello’] Çú/{ÑYπ’£îÂ/ [’namespace\to\Person’, ’sayHello’] ⇥ • ?˝p⇥Ç function ($event) { ... } F‡∫/UÕπ✏–õ*ãˆhandler≈{w Â↵b✏: function ($event){ // $event 1/Mb–0Ñ yii\base\Event } _1/Í óœ⌦bŸ7ÑMÔÂ\:ãˆhandler⇥ ÿ π π ◆ Ø ⇡ Ñ 0 π 1 / ˘ é { Í Ò Ñ ⇣ X ˝ p = ° ( ⇤ ( on() € L — ö ˆ ↵@Ÿ *handler/ H Ñ ‡ d Ñ ✏ ⇡41 ô ⇣ Ÿ 7 Ü $this->on(EVENT_A, ’publicMethod’) Fãû⌦Ÿ/*⇡ÔÑô’⇥ÂW&2Ñb✏–õhandlerÍ˝/PHPÑ h@˝p⇥Ÿ/1éhandlerÑ⇤(/⇢« call_user_func() eû∞Ñ⇥‡dhandlerÑb✏ call_user_func() ÑÅB/ÙÑ⇥Ÿ⌃( ãˆÑÊ— -ÀÕ⇥ 2.2.3 ãã㈈ˆÑÑÑ ——— ööö „„„ ddd ãã㈈ˆÑÑÑ ——— ööö ÜãˆhandlerÿÅJ…YiiŸ*handler/#⌅⌃ÍÕãˆÑ⇥Ÿ*«↵1/ãˆÑ—ö äãˆåãˆhandlerŸ$*DZ—(9Û⌦SãˆÛweш⇡1⇢o®ãˆhandlerf⇥ yii\base\Component::on() 1 / ( e — ö Ñ à π ◆ 1 ⌧ 0 yii\base\Component::off() 1/(e„dÑ⇥˘é—ö Â↵b✏: 1 $person = new Person; 2 3 // (PHPh@˝p\:handlere€L—ö 4 $person->on(Person::EVENT_GREET, ’person_say_hello’); 5 6 // (˘a$objÑ⇣X˝psay_helloe€L—ö 7 $person->on(Person::EVENT_GREET,[$obj, ’say_hello’]); 8 9 // ({GreetÑY⇣X˝psay_hello€L—ö 10 $person->on(Person::EVENT_GREET,[’app\helper\Greet’, ’say_hello’]); 11 12 // (?˝p 13 $person->on(Person::EVENT_GREET, function ($event){ 14 echo ’Hello’; 15 }); ãˆÑ—öÔÂœ⌦bŸ7(–LˆÂ„Ñb✏€L—ö_ÔÂ(Mn-€L—öS6Ÿ *MnHÑ«↵vû_/(–LˆÑ⇥ü⌃Å↵MnÑË⌃‡Ç⇥TODO ⌦bÑãPÍ/ÄUÑ—öÜãˆãˆhandlerÇú ùÑpn ✓ŸhandlerÔÂ( yii\base\Component::on() Ñ, *¬p⇥Ÿ*¬p⌃⇢ô€ Event ѯspnWµs^' data ⇥Ç: 2.2. ãã㈈ˆ Event 17 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1 $person->on(Person::EVENT_GREET, ’person_say_hello’, ’Hello World!’); 2 3 // ’Hello World!’ ÔÂ⇢« $eventøÓ⇥ 4 function person_say_hello($event) 5 { 6 echo $event->data; // ⌃>: Hello World! 7 } yii\base\Component Ù§Ü*handlerpƒ(e›X—öÑhandler: 1 // Ÿ*1/handlerpƒ 2 private _events = []; 3 4 // —ö«↵1/⌃handlerôe_event[] 5 public function on($name, $handler, $data = null, $append = true) 6 { 7 $this->ensureBehaviors(); 8 if ($append || empty($this->_events[$name])) { 9 $this->_events[$name][] = [$handler, $data]; 10 } else { 11 array_unshift($this->_events[$name], [$handler, $data]); 12 } 13 } ››› XXX handlerÑÑÑ ppp nnn ””” ÑÑÑ Œ⌦b„⌘ÏÔÂÜ„$*π⌘ÑÖπ/ $_event[] Ñpn”Ñå/—öhandlerÑ; ë⇥ Œhandlerp ƒ $_evnet[] Ñ ” Ñ ↵ ñ H ÷ / *p ƒ › X Ü ÂComponentÑ @ ã ˆhandler⇥ÂpƒÑ↵⌥:ãˆpƒC /b:˚⌫ [$handler, $data] ÑpƒÇ↵˛ @:⇢ (ãˆÑ—ö;ë⌦ gÂ↵zè⇢ - ¬p $append /&: true ⇥: true h:@Å—ö ÑãˆhandlerÅ>( $_event[] pƒÑb⇥Ÿ_/ÿ§Ñ—öπ✏⇥ - ¬p $append /&: false ⇥h:handlerÅ>(pƒÑMb⇥Ÿ*ˆ⇡Å⇢€L!$ö⇥ - Çú@ —öÑãˆÿ° Úœ—ö}Ñhandler_1/Ù⌃Å—öÑhandler/,*£H‡∫ $append /&/ true Âhandler≈6/,*C _/*C ⇥ - Çú $append : false Å—öÑãˆÚœ Ühandler£H1⌃∞—öÑ㈓e0pƒÑMb⇥ handler( $event[] pƒ-ÑMnàÕÅ„hÑ/gLÑHzè⇥Ÿ*( ⇢*ãˆhandlerÑ zè -⇢≤0⇥ 18 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 ãã㈈ˆÑÑÑ „„„ ddd ( „ d ˆ 1 /  ( unset() ˝ p ⌅ ⌃ $_event[] p ƒ ѯî C ⇥ yii\base\Component::off() Ç↵@:: 1 public function off($name, $handler = null) 2 { 3 $this->ensureBehaviors(); 4 if (empty($this->_events[$name])) { 5 return false; 6 } 7 if ($handler === null){ // $handler === null ˆ„d@ Ñhandler 8 unset($this->_events[$name]); 9 return true; 10 } else { 11 $removed = false; 12 foreach ($this->_events[$name] as $i => $event){ // MÜ@ Ñ $handler 13 if ($event[0] === $handler){ 14 unset($this->_events[$name][$i]); 15 $removed = true; 16 } 17 } 18 if ($removed){ 19 $this->_events[$name] = array_values($this->_events[$name]); 20 } 21 return $removed; 22 } 23 } ÅY✏Â↵‡π⇢ - S $handler : null ˆh:„d $name ãˆÑ@ handler⇥ - („d $handler ˆ⌃⇢„d@ ÑŸ*ãˆ↵Ñ $handler ⇥}6*handler⇢!—ö(ãˆ⌦Ñ≈ µ⇢¡FŸv/° _/° ✏IÑã≈⇥(yöÑ≈µ↵nû *handler⇢!—ö( ãˆ⌦⇥‡d(„dˆ@ Ñ $handler ˝⇢´„d⇥° û’Í„dv-Ñ$*⇥ 2.2.4 ãã㈈ˆÑÑÑ ÊÊÊ ——— ãˆÑ⌅⌃↵èhandler ÜãˆãˆhandlersT}Ü£HÍÅãˆÊ—Ühandler1⇢ g æ°ÑÔPp⇥ãˆÑÊ—Å⇤( yii\base\Component::trigger() 1 public function trigger($name, Event $event = null) 2 { 3 $this->ensureBehaviors(); 4 if (!empty($this->_events[$name])) { 5 if ($event === null){ 6 $event = new Event; 7 } 8 if ($event->sender === null){ 9 $event->sender = $this; 10 } 11 $event->handled = false; 12 $event->name = $name; 13 foreach ($this->_events[$name] as $handler){ // MÜhandlerpƒvù!⇤( 14 $event->data = $handler[1]; 15 call_user_func($handler[0], $event); // (PHPÑcall_user_func⇤(handler 16 if ($event->handled){ // Çú(–handler-⌃$evnet->handled æ:true1ç⇤(ÌÑhandler 17 return; 18 } 19 } 20 } 21 Event::trigger($this, $name, $event); // Ê—{ßÑ㈠22 } 2.2. ãã㈈ˆ Event 19 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1  yii\base\Application : ã ÷ ö IÜ$*㈠EVENT_BEFORE_REQUEST EVENT_AFTER_REQUEST ⌃+(⌅⌃˜BÑMÊ—: 1 abstract class Application extends Module 2 { 3 // öIÜ$*㈠4 const EVENT_BEFORE_REQUEST = ’beforeRequest’; 5 const EVENT_AFTER_REQUEST = ’afterRequest’; 6 7 public function run() 8 { 9 try { 10 11 $this->state = self::STATE_BEFORE_REQUEST; 12 $this->trigger(self::EVENT_BEFORE_REQUEST); // HÊ—EVENT_BEFORE_REQUEST 13 14 $this->state = self::STATE_HANDLING_REQUEST; 15 $response = $this->handleRequest($this->getRequest()); // ⌅⌃Request 16 17 $this->state = self::STATE_AFTER_REQUEST; 18 $this->trigger(self::EVENT_AFTER_REQUEST); // ⌅⌃å’Ê—EVENT_AFTER_REQUEST 19 20 $this->state = self::STATE_SENDING_RESPONSE; 21 $response->send(); 22 23 $this->state = self::STATE_END; 24 25 return $response->exitStatus; 26 27 } catch (ExitException $e){ 28 29 $this->end($e->statusCode, isset($response) ? $response : null); 30 return $e->statusCode; 31 32 } 33 } 34 } ⌦bÑ„(h˪˚¬⇥ÍÅË✏/HöIãˆHÊ—ãˆÑ1ÔÂÜ⇥ ˘ éãˆÑ ö I – !  (const 8 œ Ñ b✏ Ô Â  Mô⇡ ⇥ trigger(’Hello’) å trigger(’hello’) Ô/ÑãˆÊ⇥ü‡(éhandlerpƒ↵⌥1/ãˆ⇥PHPÃpƒ↵ ⌥/:⌃'✏ôÑ⇥@Â({8œÑπ✏ÔÂMŸÕ4ºÑÓò⇥ (ʗ㈈ÔÂä㈯sÑpn ✓Ÿ@ Ñhandler⇥‘Ç—⇤∞ÆZãˆ: 1 // öIãˆÑsTpn 2 class MsgEvent extend yii\base\Event 3 { 4 public $dateTime; // ÆZ—˙ш٠5 public $author; // ÆZÑ\⇧ 6 public $content; // ÆZÑÖπ 7 } 8 9 // (—⇤∞ÑÆZˆ∆⌥}Å ✓ŸhandlerÑpn 10 $event = new MsgEvent; 11 $event->title = $title; 12 $event->author = $auhtor; 13 14 // ʗ㈠15 $msg->trigger(Msg::EVENT_NEW_MESSAGE, $event); Ë✏ŸÃpnÑ e( on() —öhandlerˆ epnπ’Ñ⇥( on() -(*Ä Uÿœ ev(handler-⇢« $event->data €LøÓ⇥Ÿ*/(—öˆnöÑpn⇥ Ñp 20 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 n/°û’(—öˆnöÑÇ—˙ÆZшÙ⇥Ÿ*ˆ⇡1Å(ʗ㈈–õv÷ÑpnÜ⇥ _1/⌦bŸµ„(Ñπ’Ü⇥Ÿ$Õπ’Õ(é–õ—öˆÑ¯spnÕ(é–õ㈠ʗˆÑpn⌅ @í¯eE⇥`ÔÅó4ÔsÅödÑ|Ü⇥ 2.2.5 ⇢⇢⇢ ***ãã㈈ˆhandlerÑÑÑ zzz èèè ( yii\base\Component::on() ÔÂ:⌅Õ㈗öhandler_ÔÂ:㈗ö⇢ *handler⇥GÇ`/ÆZ˚flÑÄ/∫X⇢Àш⇡`yö∞—ÆZÑãˆhandler1/⇢ ÂsË⇧ ∞ÑÖπ—⇤Ü⇥∞(`IÅ›YŸ*ü˝`ÿÅ⇢ÂÆZ-@0Ñ@ ∫⇥Ÿ*ˆ ⇡ÕZ’/Ù•(üeÑhandler+>†⌦∞Ñ„Â⌅⌃Ÿ*∞ÑÅ⇥Ê*π’1/çô *handlerv—ö0Ÿ*ãˆ⌦⇥Œ◆é٧ѓ¶e≤,åÕπ’/‘É⌃Ñ⇥MÕπ’1 éÓ9Üüec8(Ñ„Ô˝⇢qÕüeÑc8ü˝⇥ˆÇúÙ ∞ÑB£HàΟ *handler1⇢ÿóàBà'⇥@Â˙Æ(,åÕπ’⇥ Yii-//ŸÕ˘⇢Ñ—öÑ⇥£H(*ãˆÊ—ˆÍ*handler⇢´HgLb ⌅handlerKÙ; *HÓò'⇥Ÿ*Ô˝Ñ↵ÌÑF∂ Ñû∞π✏⇥ ÑÌ/Â⌃Ñb✏e›XhandlerÔ˝⇢—ö⌦ªÑãˆHgLÑπ✏–\⇥ŸÕπ✏Ñ }⌅/Ñ∫CP'õÔÂ˘ãˆ€LÙ9Ê*-b˚±•(w)b‡- ⌅Õ:óbÑhandler⇥Yii/(pƒe›XhandlerÑv zègLŸõhandler⇥Ÿ✏s@ ,F∂⌦ÑæÑhandler⇢HgL⇥F/ÅÂ:YiiÑãˆhandler1°û’w)bÂÜņ⌦ ÑãˆhandlerH–LÍ(⇤( yii\base\Component::on() €L—öˆ⌃,€*¬pæ: $append æ: false £HŸ*handler1⇢´>(pƒÑMbÜÉ1⇢´HgLÉ_1 Ô˝ :óbÑhandlerÜ⇥ :܆:âhß˝∂â—@˘h˝dˇ€L—°Ê—ˇæ÷Ï⇢60•fŸ1/ *ãˆå*handler: 1 $coal->on(Coal::EVENT_DISASTER,[$government, ’onDisaster’]); 2 3 class Government extend yii\base\Component 4 { 5 ... ... 6 7 public function onDisaster($event) 8 { 9 echo ’DISASTER! from ’ . $event->sender; 10 } 11 } 1édˇÍ´_Å€L°⌃@Â?úA∏dˇÔÂôÍÒÑhandler˘ˇæ€L⌅⌃⇥F/ *✏dëÑ` o°⌘ «ôØ˘é—ˇæŸãˆôÜ*handler◆Ë(éí•: 1 // ,€*¬pæ:falseóÂhandler(t*handlerpƒ-⌅é,* 2 $coal->on(Coal::EVENT_DISASTER,[$baddy, ’onDisaster’], null, false); 3 4 calss Baddy extend yii\base\Component 5 { 6 ... ... 7 8 public function onDisaster($event) 9 { 10 $event->handled = true; // ⌃ãˆ⌥∆:Úœ⌅⌃å’;bÌãˆhandlerÀe⇥ 11 } 12 } O ∫Ô ⇢ ↵ Ñ O ∫ M Ô ⇥ ⌘ Ï Å ; b ÷ @ Â Å ä — ö } Ñhandler„ d ⇥ Ÿ * „ d / — ö Ñ ⌃ ⌘ « ↵ ( û ( ⌦ 1 / ä ˘ î ÑhandlerŒhandlerp ƒ - d ⇥  ( yii\base\Component::off() 1˝ d: 1 // d@ EVENT_DISASTERãˆÑhandler 2 $coal->off(Coal::EVENT_DISASTER); 2.2. ãã㈈ˆ Event 21 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 3 4 // d*PHPh@˝pÑhandler 5 $coal->off(Coal::EVENT_DISASTER, ’global_onDisaster’); 6 7 // d*˘aÑ⇣X˝pÑhandler 8 $coal->off(Coal::EVENT_DISASTER,[$baddy, ’onDisaster’]); 9 10 // d*{ÑY⇣X˝pÑhandler 11 $coal->off(Coal::EVENT_DISASTER,[’path\to\Baddy’, ’static_onDisaster’]); 12 13 // d*?˝pÑhandler 14 $coal->off(Coal::EVENT_DISASTER, $anonymousFunction); v-, Õπ’1ÔÂä✏dëÑhandler„d↵e⇥ ∆√Ñ˚⇧↵ÀÔ˝Y✏0( d?˝phandlerˆÅ(*ÿœ⇥˜˚⇧↵ÀY✏1 ó`⇤( yii\base\Component::on() yii\base\Component::off() ˆôÜ$*!7Ñ ?˝p`_°û’ä`MbÑ?handler„d⇥Œ,(⌦e≤$*?˝p1/$*ÑX (:Ü˝cn„dÅHä?handler›X⇣*ÿœÇ⌦bÑ $anonymousFunction 6 çù!—ö„d⇥F/(Üÿœ11ªÜ?˝pÑ'√⌃⌦Ñ⇠ø`,(ªs√ ÷Ñ⌘Ñ˙Æ/(ŸÕ≈µ↵1Å(?˝pÜ⇥‡d(\:handlerˆÅNÕ(?˝ p⇥Í (nöÅ„dˆMÔÂ(⇥ 2.2.6 ãã㈈ˆÑÑÑ ßßß +++ MbÑãˆ˝/à˘{ÑûãÑ_1/ãˆÑÊ—⌅⌃hË˝(ûã⇤ÙÖ⇥ŸÕß+ Ñãˆ(≈◆{Ñv÷ûã—s˚_v÷{v÷ûã—s˚⇥dÜûãß+Ñã ˆÿ {ß+Ñãˆ⇥˘éYii1éApplication/*Uã@ Ñ„˝ÔÂøÓŸ*Uã⇥‡ d *yäß+Ñãˆh@ãˆ⇥F/,(⌦÷Í/*ûãß+Ñãˆ⇥ Ÿ1}‘/l¯ÃÑ6B⇥ïBÑúÏÍ˝ÍÒ——bö*∫Ñú✓¿PÍ—(ÍÒ´ ⌦qÕÜv÷∫⇥‚#∫Çú√≈}t*‚Ñ@ ⇣X )˝Å⇠⇠bbNN L⇥0Ül¯;£Ã÷ )=Í*<Ñb⌦ªÊ 4ãˆ_/Ÿ7ÑB!Ñã ˆ≥öÜ÷qÕ0Ñ⇤Ù⇥ {{{ ßßß +++ ãã㈈ˆ H≤≤{ß+Ñãˆ⇥{ß+ãˆ(éÕî@ {ûãÑãˆ⇥‘ÇÂ4ÅÜ„@ Â∫Ñ↵ ̈٣H÷1—ö*handler0Worker{Ÿ7œ*Â∫↵̈÷˝˝ÂSÜ⇥ûãß+Ñ ãˆ{ß+ãˆÑ—öÅ( yii\base\Event::on() 1 Event::on( 2 Worker::className(), // ,*¬ph:㈗Ñ{ 3 Worker::EVENT_OFF_DUTY, // ,å*¬ph:/¿H㈠4 function ($event){ // ˘ãˆÑ⌅⌃ 5 echo $event->sender . ’ ↵ÌÜ’; 6 } 7 ); Ÿ7œ*Â∫↵̈⇢Ê—ÍÒÑãˆ⌅⌃˝p‘ǪSa⇥K⇢Ê—{ß+ãˆ⇥{ ß+ãˆÑÊ—Õ6/( yii\base\Component::trigger() -ÿ∞ó½pÑ*ÌÂH: Event::trigger($this, $name, $event); // Ê—{ßÑ㈠Ÿ * Ì Â 1 Ê — Ü { ß + Ñ ãˆ⇥ { ß + ㈠; / ( û ã ãˆ Ê — ⇥ ‚ 6 Ê — ˆ : ` £ H Ç ú ) ` Å È » Û   S `⇢ ⇢ ⌘ ✏ d ë  £ 7 ⇢ « $event->handled = true e»bãˆ⌅⌃Çú/Ù⌘t`ÿ/˛7˛Ó4J⇥‡∫/ yii\base\Component::trigger() ÿ/ yii\base\Event::trigger() -˝˘é $trigger ¯ÑÑ⌅⌃: 22 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 1 if ($event->sender === null){ 2 $event->sender = $this; 3 } 4 $event->handled = false; // ÕnÜhandled 5 $event->name = $name; // ÕnÜname 1é⌦bÑ„ÕnÜ $event ÑË⌃Öπ@Â`Ñ)´‡»S%Ü⇥‡dó˙*ÕÅ ”∫⇢ $event->handled = true Í˝»bß+ãˆÑ⌅⌃°û’Ëãˆß+€LrÑ⇥@ ÂcnÑZ’/⇢«—ö*{ß+ãˆÑhandlere-b⇥ Ÿvû/àc8Ñã≈`w öÑCPÇÔÂ(–ß+Ñãˆ⌦—ö㈣H`1Ô Âè✏»b÷⇥Çú`Í Nß+ÑCP`S61˝»bÿß+ãˆÑ⌅⌃Ü⇥ @Â?úËË✏dëÑóâÔ»bÜÍÅ?úËË(ûã⌦€L—ö˘é{ ß+㈀L—ö✏dë1° û’í•Ü⇥ Œ yii\base\Event::trigger() Ѭp⌫he↵‘ yii\base\Component::trigger() ⇢ Ü*¬ p $class h : Ÿ / Í * { Ñ ãˆ⇥ ‡ d ( › X $_event[] p ƒ ⌦ yii\base\Event _‘ yii\base\Component Å⇢*Ù¶: 1 // Component-Ñ$_event[] pƒ 2 $_event[$eventName][] = [$handler, $data]; 3 4 // Event-Ñ$_event[] pƒ 5 $_event[$calssName][$eventName][] = [$handler, $data]; £HÕ«eÑ›Nß+ÑhandlerÔÂ(ÿß+㈗ˆ—\(HŸS6_/LÑ⇥1 é{ß+ãˆ˚✏Ñûã¯sT@Â{ß+ãˆÊ—ˆ{ÑûãÔ˝˝ÿ° bHÔ˝ €L⌅⌃b {ß+ãˆÑÊ—î( yii\base\Event::trigger() ⇥Ÿ*˝p⇢Ê—ûãß+Ñã ˆ⇥<óË✏Ñ/ $event->sender (ûãß+ãˆ- $event->sender ⌥⌘Ê—ãˆÑû ã({ß+ãˆ- ⌥⌘ш{⇥( yii\base\Event::trigger() „- : 1 if (is_object($class)) { // $class /trigger()Ñ,*¬ph:{ 2 if ($event->sender === null){ 3 $event->sender = $class; 4 } 5 $class = get_class($class); // eÑ/*ûã⇡Â{ˇbK 6 } else { 7 $class = ltrim($class, ’\\’); 8 } Ÿµ„⇢˘ $evnet->sender €LænÇú eш⇡Úœ⌥öÜ÷Ñ<£HŸ*<⇢ ›Y&⇡1⇢ˇb⇣{⇥ ˘é{ß+㈠*Å<Ë✏Ñ0π1/÷I⇢Ê—Í´Ÿ*{Ñ㈟*{Ñ@ VH{Ñãˆ_⇢´Ê—⇥F/Í´{ãˆ@ VH{Ñãˆ∆:ß+: 1 // bÑ™ØMÜ@ VH{ 2 do { 3 if (!empty(self::$_events[$name][$class])) { 4 foreach (self::$_events[$name][$class] as $handler){ 5 $event->data = $handler[1]; 6 call_user_func($handler[0], $event); 7 8 // @ Ñãˆ˝/ß+ÔÂ舻b 9 if ($event->handled){ 10 return; 11 } 12 } 13 } 14 } while (($class = get_parent_class($class)) !== false); 2.2. ãã㈈ˆ Event 23 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 ⌦bÑLW™ØÑÒ¶⇧ΈÙB¶◊$*πbqÕ/{Á”ÑÑÒ¶å/ $_event[$name][$class][] pƒÑC *psÚœ—öÑhandlerÑpœ⇥Œûıœå↵, oˆÂ↵ÁÒ¶Ö«ABÑ1à⌘¡ãˆ—ö⌦ãˆÑ—öhandlerÖ«A‡*_‘É⌘ ¡⇥‡d⌦bÑLW™Ø–ópœß'¶(100^1000KÙŸ/Ô•◊Ñ⇥ F/Œ:6⌦e≤1é{ß+ãˆ⇢´{Í´{Ñûã„{„{ûã@Ê—@ ¢éäïBÑ{v{ãˆÑqÕ⇤Ù1ä'⇥‡d(({ãˆ⌦ÅË✏=ԽĄ {âíÂß6}qÕ⇤Ù=Ô˝(˙@{⌦âí{ãˆ⇥ hhh @@@ ãã㈈ˆ •↵eç≤≤h@ß+ãˆ⇥⌦b–0«@◆Ñh@ãˆ,(⌦Í/*ûããˆbÜ⇥÷Í /)(ÜApplicationûã(t*î(Ñ}h-h@ÔøÓÑy'eû∞Ÿ*h@ãˆÑ⇥S6 `_ÔÂ⌃÷—ö(˚✏h@ÔøÓÑÑComponent⌦⇥ h@ãˆ*'⇠ø(é⇢(˚✏Åш⇡˝ÔÂÊ—h@ãˆ_ÔÂ(˚✏≈Åш⇡ —ö„d*ãˆ: 1 Yii::$app->on(’bar’, function ($event){ 2 echo get_class($event->sender); // >:SMÊ—ãˆÑ˘aÑ{ 3 }); 4 5 Yii::$app->trigger(’bar’, new Event([’sender’ => $this])); ⌦ b Ñ Yii::$app->on() Ô Â ( ˚ U 0 π ⇤ ( 1 Ô Â å ⇣ ãˆÑ — ö ⇥ Yii::$app->trigger() ÍÅ(—öKÑ˚Uˆ⇡⇤(1OKÜ⇥ 2.3 LLL ::: Behavior (L:behavior ÔÂ(Ó9∞ {Ñ≈µ↵˘{Ñü˝€LiE⇥⇢«⌃L:—ö0 *{ÔÂ{w L:,´@öIÑ^'åπ’1}œ{,e1 Ÿõ^'åπ’7⇥ Åô*∞Ñ{ªÁ⇧+∞ {⇥ Yii-ÑL:vû/ yii\base\Behavior {ÑûãÍÅ⌃*behaviorûã—ö0˚✏Ñ yii\base\Component ûã⌦Ÿ*Component1Ô Âbehavior@öIÑ^'åπ’Ü⇥Çú ⌃L:ãˆsTweÔ©ѱ71Ù⇢Ü⇥ F πÅË✏behaviorÍ˝Component{—ö÷Ï/)Ñ˘1≈/`ÛpÛp 1˝pÑ≈ÅÑi(/⌘ÜÑKó'⌘t⇥@ÂÇú`ôÜ*{Å(0L:£H1 ú≠0ÁÍ yii\base\Component ⇥ Êπ1≈_/Ãπ`≈⌘?Ñ⇥L:_/ÅÃπq/ÑYii˘éL:Ñ/dÜ` yii\base\Behavior K yii\base\Component _/œ«Üæ√æ°⇥ 2.3.1  ((( LLL ::: *—öÜL:Ñ{h∞we/Ÿ7Ñ: 1 // Step 1: öI*⌃—öL:Ñ{ 2 class MyClass extends yii\base\Component 3 { 4 // zÑ 5 } 6 7 // Step 2: öI*L:{÷⌃—ö0MyClass⌦ 8 class MyBehavior extends yii\base\Behavior 9 { 10 // L:Ñ*^' 11 public $property1 = ’This is property in MyBehavior.’; 24 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 12 13 // L:Ñ*π’ 14 public function method1() 15 { 16 return ’Method in MyBehavior is called.’; 17 } 18 } 19 20 $myClass = new MyClass(); 21 $myBehavior = new MyBehavior(); 22 23 // Step 3: ⌃L:—ö0{⌦ 24 $myClass->attachBehavior(’myBehavior’, $myBehavior); 25 26 // Step 4: øÓL:-Ñ^'åπ’1åøÓ{Í´Ñ^'åπ’7 27 echo $myClass->property1; 28 echo $myClass->method1(); ⌦bÑ„`(h˝↵¬}6`Ô˝Úœ(⇢æ4⌧0ÜŸõ„Ñ✏FŸÃ`Í Å∞OL:-Ñ^'åπ’Ô´@—öÑ{œøÓÍ´Ñ^'åπ’7Ù•øÓ1OKÜ⇥„ - $myClass /° property1 method() ⇣XÑ⇥ŸÈ/ $myBehavior Ñ⇣X⇥F/⇢« attachBehavior() ⌃L:—ö0˘aK $myCalss 1}œ√⇣Ü8'’ü'’∞hÑ "'ó⌃+∫Ñ^'åπ’˝ÿ⇣ÜÍÒÑ⇥ ÊŒ⌦bÑ„-`ÿÅå·(L:Ñ'ÙA↵⇢ • Œ yii\base\Component >ÍÒÑ{Âø(L: • Œ yii\base\Behavior >ÍÒÑL:{ÃböIL:â 0Ñ^'π’ • ⌃ComponentåBehavior—öwe • œ(ComponentÍ´Ñ^'åπ’7=≈(L:-öIÑ^'åπ’⇥ 2.3.2 Behavior{{{ ÑÑÑ ÅÅÅ ⌘Ï–0ÜL:Í/ yii\base\Behavior {Ñûã⇥£HŸ*{vfl ¿Hÿ∆bvûÙ4 Ü_° ¿HÑ÷Í/*ÄUÑ≈Ú^8ÑÄU: 1 class Behavior extends Object 2 { 3 // ⌥⌘L:,´@—öÑComponent˘a 4 public $owner; 5 6 // Behavior ˙{,´°(;Å/Í{(‘fi*pƒh:L:@sTÑ㈠7 public function events() 8 { 9 return []; 10 } 11 12 // —öL:0 $owner 13 public function attach($owner) 14 { 15 $this->owner = $owner; 16 foreach ($this->events() as $event => $handler){ 17 $owner->on($event, is_string($handler) ? [$this, $handler] : $handler); 18 } 19 } 20 21 // „d—ö 22 public function detach() 23 { 24 if ($this->owner){ 2.3. LLL ::: Behavior 25 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 25 foreach ($this->events() as $event => $handler){ 26 $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler); 27 } 28 $this->owner = null; 29 } 30 } 31 } Ÿ1/BehaviorÑhËÜ„//àÄUBehavior{ÑÅ ÑnàÄU⇢ • $owner ⇣Xÿœ(é⌥⌘L:ÑùD˘a • events() (éh:L:@ ÕîÑ㈠• attach() (é⌃L:Component—öwe • deatch() (é⌃L:ŒComponent⌦„d⇥ ↵b⌃+€L≤„⇥ 2.3.3 ööö III***LLL ::: öI*L:1/∆⌥}ÅËe0∞ {-ªÑ^'åπ’Ÿõ^'åπ’Åô0* yii\base\Behavior {-⇥@ÂöI*L:1/ô* BehaviorÑP{P{-⇧+Ü@Å ËeÑ^'åπ’: 1 namespace app\Components; 2 3 use yii\base\Behavior; 4 5 class MyBehavior extends Behavior 6 { 7 public $prop1; 8 9 private $_prop2; 10 private $_prop3; 11 private $_prop4 12 13 public function getProp2() 14 { 15 return $this->_prop2; 16 } 17 18 public function setProp3($value) 19 { 20 $this->_prop2 = $value; 21 } 22 23 public function foo() 24 { 25 // ... 26 } 27 28 protected function bar() 29 { 30 // ... 31 } 32 } ⌦ b Ñ „ ⇢ « ö I* app\Components\MyBehavior { ⇥ 1 é MyBehavior Á  Í yii\base\Behavior ŒÙ•0ÁÍ yii\base\Object ⇥°⇡Ÿ/⌘ÏÑ↵ÀÜ⇥‡ dŸ*{ *publicÑ⇣Xÿœ prop1 *Í˚^' prop2 *Íô^' prop3 *publicÑπ 26 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 ’ foo() ˆ *private Ñ⇣Xÿœ $_prop4 *protected Ñπ’ bar() ⇥Çú`⇧ZÍ ˚^'åÍô^'}fi4↵↵ ^' Ë⌃ÑÖπ⇥ SSS ŸMyBehavior*Component—ö—öÑComponent_1Â Ü prop1 prop2 Ÿ$*^' åπ’ foo() ⇥ 2.3.4 LLL ::: ÑÑÑ ùùù DDD ˘˘˘ aaa yii\base\Behavior::$owner ⌥⌘Ñ/Behaviorûã,´@ùDÑ˘a⇥Ÿ/L:-(@ù D˘aÑ/KµÜ⇥⇢«Ÿ* $owner L:M˝øÓ@ùDÑComponentM˝⌃,´Ñπ’\:ã ˆhandler—ö0Component⌦⇥ $owner 1 yii\base\Behavior::attach() € L K < _ 1 / ( ⌃ L : — ö 0 – *Componentˆ $owner 1Úœ± ;Ü⇥,≈µ↵Å`ÍÒK®ª⌥ö $owner Ñ <(⇤( yii\base\Componet::attachBehavior() ⌃L:˘a—öˆComponent⇢Í®0 ⌃ $this \:¬p⇤( yii\base\Behavior::attach() ⇥ πÅ<Ë✏1éL:Œ,(e≤/*PHP{vπ’1/{π’1/⇣X˝p⇥@ Â(L:Ñπ’- $this (Ñ/L:,´’˛⇢« $this eøÓL:@ùDÑComponent/ L⇢Ñ⇥cnÑπ’/⇢« yii\base\Behavior::$owner ⇥ 2.3.5 LLL ::: ÑÑÑ ——— ööö L : Ñ — ö / ⇢ 8 1Componente — w ⇥ $ Õ π ✏ Ô Â ⌃ *Behavior— ö 0 * yii\base\Component ⇥Õ/YÑπ’ÊÕ/®Ñ⇥YÑπ’(ûı-(ó‘É⇢ õ⇥‡:,≈µ↵(`Ñ„°—weKM*{îSw UÕL:/nöÑ⇥®—öÑ π’;Å/–õÜÙu;Ñπ✏FûE(-v⇢¡⇥ Y—öL:ÍÅÕ} yii\base\Component::behaviors() 1ÔÂÜ⇥Ÿ*π’(éœ {@w ÑL:⇥ÇUœb(MneœÔÂ/Behavior{_ÔÂ/Behavior{ÑMnp ƒ: 1 namespace app\models; 2 3 use yii\db\ActiveRecord; 4 use app\Components\MyBehavior; 5 6 class User extends ActiveRecord 7 { 8 public function behaviors() 9 { 10 return [ 11 // ?ÑL:≈Ù•Ÿ˙L:Ñ{ 12 MyBehavior::className(), 13 14 // :myBehavior2ÑL:_/≈Ÿ˙L:Ñ{ 15 ’myBehavior2’ => MyBehavior::className(), 16 17 // ?L:Ÿ˙ÜMyBehavior{ÑMnpƒ 18 [ 19 ’class’ => MyBehavior::className(), 20 ’prop1’ => ’value1’, 21 ’prop3’ => ’value3’, 22 ], 23 24 // :myBehavior4ÑL:_/Ÿ˙ÜMyBehavior{ÑMnpƒ 25 ’myBehavior4’ => [ 26 ’class’ => MyBehavior::className(), 27 ’prop1’ => ’value1’, 28 ’prop3’ => ’value3’, 29 ] 2.3. LLL ::: Behavior 27 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 30 ]; 31 } 32 } (⌦bÑãP-ÂpƒÑ.\:L:Ñ}˘é° –õ.ÑL:1/?L:⇥⌦b ÑãP $*?L:$*}L: myBehavior2 myBehaviro4 ⇥ ˘é}ÑL:ÔÂ⇤( yii\base\Component::getBehavior() e÷óŸ*—ö}ÑL :: $behavior = $Component->getBehavior(’myBehavior2’); ˘é?ÑL:⇡° û’Ù•(Ü⇥F/ÔÂ∑÷@ Ñ—ö}ÑL:: $behaviors = $Component->getBehaviors(); Ûé®:—öL:Å⇤( yii\base\Compoent::attachBehaviors(): $Component->attachBehaviors([ ’myBehavior1’ => new MyBehavior, // Ÿ/*}L: MyBehavior::className(), // Ÿ/*?L: ]); Ÿ*π’•◊*pƒ¬p¬pÑ+I⌦bY—öL:/77Ñ⇥ ÿ *û’1/⇢«Mnáˆe—ö: 1 [ 2 ’as myBehavior2’ => MyBehavior::className(), 3 4 ’as myBehavior3’ => [ 5 ’class’ => MyBehavior::className(), 6 ’prop1’ => ’value1’, 7 ’prop3’ => ’value3’, 8 ], 9 ] wS¬¡MnË⌃ÑÖπ⇥ 2.3.6 ——— ööö ÑÑÑ ÖÖÖ ËËË üüü ⌃⌃⌃ Í/Õ}* yii\base\Component::behaviors() 1ÔŸH^G0(L:ÜŸÍ/∞ qÑ“ûE⌦s˚0—öÑ«↵ sÑπb ⇢ • yii\base\Component::behaviors() • yii\base\Component::ensureBehaviors() • yii\base\Component::attachBehaviorInternal() • yii\base\Behavior::attach() yii\base\Component::behaviors() ⌦ b – 0 Ü 1 / ‘ fi *p ƒ ( é œ  L : ⇥ £ H yii\base\Component::ensuerBehaviors() b Ÿ * π ’ ⇢ (ComponentÑ ¯ ⇢ 0 π ⇤( __get() __set() __isset() __unset() __call() canGetProperty() hasMethod() hasEventHandlers() on() off() I(0↵0ŸH⇢//4ºπ˝B› ÍÅâ 0{Ñ^'π’㈟*˝p˝⇢´⇤(0⇥ ŸH⌫Ò´¯⇢·∫@ÅÑ ensureBehaviors() vfl/U∏∫_÷vûÍ/⌃ behaviors() -@œÑL:€L—öÚ: 1 public function ensureBehaviors() 2 { 3 if ($this->_behaviors === null){ 4 $this->_behaviors = []; 28 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 5 6 // MÜ $this->behaviors() ‘fiÑpƒv—ö 7 foreach ($this->behaviors() as $name => $behavior){ 8 $this->attachBehaviorInternal($name, $behavior); 9 } 10 } 11 } Œ⌦b Ñ „ - Í 6 1 ↵ 0 Ü • ↵ e Å Ù Ñ , *⌧⌧ yii\base\Component\attachBehaviorInternal 1 private function attachBehaviorInternal($name, $behavior) 2 { 3 if (!($behavior instanceof Behavior)) { 4 $behavior = Yii::createObject($behavior); 5 } 6 if (is_int($name)) { 7 $behavior->attach($this); 8 $this->_behaviors[] = $behavior; 9 } else { 10 if (isset($this->_behaviors[$name])) { 11 $this->_behaviors[$name]->detach(); 12 } 13 $behavior->attach($this); 14 $this->_behaviors[$name] = $behavior; 15 } 16 return $behavior; 17 } ñHÅË✏0Ÿ/*private⇣X⇥vû(Yii-@ : *Internal Ñπ’˝/¡ Ñ⇥Ÿ*π’rÜŸH‡ˆã⇢ • Çú $behavior ¬pv^/* Behavior ûã1ÂK:¬p( Yii::createObject() ˙˙e⇥ • ÇúÂ?L:Ñb✏—öL:£HÙ•⌃L:D†(Ÿ*{⌦ • Çú/}L:H↵↵/& ÑL:Úœ—ö(Ÿ*{⌦Çú (eÑL:÷„K MÑL:⇥ ( yii\base\Component::attachBehaviorInternal() -  $this : ¬ p ⇤ ( Ü yii\base\Behavior::attach() ⇥ Œ ˙ Ü fl — ö ¯ s Ñ *∂ ⇡ yii\base\Behavior::attach() 1 public function attach($owner) 2 { 3 $this->owner = $owner; 4 foreach ($this->events() as $event => $handler){ 5 $owner->on($event, is_string($handler) ? [$this, $handler] : $handler); 6 } 7 } ⌦bÑ„rÜ$ˆã⇢ • æn}L:Ñ $owner óL:ÔÂøÓÕ\@ùDÑ˘a • MÜL:-Ñ events() ‘fiÑpƒ⌃∆⌥ÕîÑãˆ⇢«@ùD{Ñ on() —ö0{⌦ ÙÜŸH⇢sé—öZ*✏”⇢ • —öÑ®\ŒComponent—w • Y—ö⇢«Õ} yii\base\Componet::behaviors() û∞ • ®—ö⇢«⇤( yii\base\Component::attachBehaviors() û∞ • L:ÿÔÂ⇢«: Component Mn as Mny€L—ö 2.3. LLL ::: Behavior 29 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 • L: ?L:å}L:K⌃:+(é—öˆ/&Ÿ˙}⇥}L:ÔÂ⇢«v}€L ⌥∆Œ à˘'0€L„dIÕ\ • —ö«↵-—öÑL:⇢÷„Úœ—öÑL: • —öÑ✏I $π/:L:æn $owner ⇥å/⌃L:-flÕîÑãˆÑhandler—ö0{- ª⇥ 2.3.7 „„„ ddd LLL ::: „dL:Í⇤( yii\base\Component::detachBehavior() 1OKÜ: $Component->detachBehavior(’myBehavior2’); Ÿ71Ô„dÚœ—ö}Ñ: myBehavior2 ÑL:Ü⇥F/˘é?L:Ÿ*π’1‡ Œ↵KÜ⇥F/Ô„d@ —ö}ÑL:: $Component->detachBehaviors(); Ÿ ⌦ b $ Õ π ’ ˝ ⇢ ⇤ ( 0 yii\base\Behavior::detach() ⇥ yii\base\Behavior::attach() ¯ Õ „ d Ñ « ↵ 1 / r $ˆã⇢ / ⌃ $owner æ n : null h:Ÿ*L:° ùD0˚U{⌦⇥å/⇢«ComponentÑ off() ⌃—ö0{⌦Ñã ˆhanlder„d↵e⇥›ÑÀÑ»⇥ 2.3.8 ((( LLL ::: ÕÕÕ îîî ãã㈈ˆ L:㈔ÔÂ(˘{\Ó9Ñ≈µ↵eE{(ãˆÊ—Ñ⌅ÕÕî⇥ÍÅ Õ} yii\base\Behavior::events() π’h:Ÿ*L:⌃˘{ÑUÕ㈀LUÕÕàsÔ: 1 namespace app\Components; 2 3 use yii\db\ActiveRecord; 4 use yii\base\Behavior; 5 6 class MyBehavior extends Behavior 7 { 8 // ... 9 10 // Õ}events() ó(ãˆÊ—ˆ⇤(L:-Ñõπ’ 11 public function events() 12 { 13 return [ 14 // (EVENT_BEFORE_VALIDATEãˆÊ—ˆ⇤(⇣X˝p beforeValidate 15 ActiveRecord::EVENT_BEFORE_VALIDATE => ’beforeValidate’, 16 ]; 17 } 18 19 // Ë✏beforeValidate /L:Ñ⇣X˝p/—öÑ{Ñ⇣X˝p⇥ 20 // ÿÅË✏Ÿ*˝pÑ~Å·≥ãˆhandlerÑÅB⇥ 21 public function beforeValidate($event) 22 { 23 // ... 24 } 25 } ⌦bÑ„- events() ‘fi*pƒh:@ÅZ}ÕîÑãˆ⌦ã-Ñãˆ/ ActiveRecord::EVENT_BEFORE_VALIDATE ÂpƒÑ.eh:pƒÑ<⇡h:Z}Õî Ñ„⌦ã-/ beforeValidate() ÔÂ/Â↵b✏⇢ • W&2h:L:{Ñπ’Ç⌦bÑã1/ŸÕ≈µ⇥Ÿ*/ãˆhandlerÑã ˆhandler-(W&2/h:PHPh@˝p⇥ 30 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 • *˘a{Ñ⇣X˝pÂpƒÑb✏Ç [$object, ’methodName’] ⇥Ÿ*ã ˆhandler/ÙÑ⇥ • *?˝p⇥ ˘éÕî˝pÑ~ÅBãˆhandler7: function ($event){ } wSÖπ˜¬⇤ ãˆEvent ÑÖπ⇥ 2.3.9 LLL ::: ÕÕÕ îîî ÑÑÑ ãã㈈ˆüüü ⌃⌃⌃ ⌦bÑ—öå„d«↵⌘Ï↵0Yii9Ü£H'≤;Å1/:Ü⌃L:-Ñãˆhandler—ö0{ -ª⇥(ûE↵ˆL:(ó⇢Ñ_/˘éCompoent⌅ÕãˆÑÕî⇥⇢«L:ËeÔÂ( Ó9∞ {Ñ„Ñ≈µ↵Ù9iU{˘éãˆÑÕîå/(Ÿ*ÄÁÔ©˙à´Ñ ±7⇥Å⌃L:ComponentÑãˆsTwe1Å⇢« yii\base\events() π’⇥⌦bÑ„ -Ÿ*π’Í/‘fiÜ*zpƒÙ˘@ùDÑCompoentÑ˚UãˆßsT⇥F/(ûE (ˆÄÄ⇢«Õ}Ÿ*π’eJ…YiiŸ*L:⌃˘CompoentÑUÕãˆ(Í*π’€L⌅ ⌃⇥ ‘ Ç YiiÍ & Ñ yii\behaviors\AttributeBehavior { ö IÜ( * ActiveRecord ˘ a Ñ – õ㈗ ˆ Í ® ˘ – õ W µ € L Ó 9 Ñ L : ⇥ ÷ *à 8 ( Ñ P { yii\behaviors\TimeStampBehavior (é⌃⌥öÑWµæn:*SMшÙ38(éh: Ó9Â⌦!{FˆÙI:o⇥v- yii\behaviors\AttributeBehavior::event() Ç↵: public function events() { return array_fill_keys(array_keys($this->attributes), ’evaluateAttributes’); } Ÿµ„Ñ✏ŸÃ\«⇢Òef YõÑ˚⇧↵ÀÔÂÍLvæ¶vÿ⇥ŸÃ`Í Å'ÙÂSŸµ„⌃‘fi*pƒv.<: $this->attributes pƒÑ.attributes)) { 6 // Õπ↵ŸÃ 7 $this->attributes = [ 8 BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute], 9 BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute, 10 ]; 11 } 12 } ⌦bÑ„Õπ↵Ñ/˘é $this->attributes ÑÀË⌃⇥”⌦b$µ„˘é yii\base\Behavior::events() Ñ‘fipƒv<✏îÂ/Ÿ7Ñ⇢ • pƒÑ.<(é⌥öÅÕîÑãˆÇ BaseActiveRecord::EVENT_BEFORE_INSERT I • pƒÑproperty1 Yii( U r Üõ¿Hb Ÿ * ↵↵ yii\base\Component::__get() 1 public function __get($name) 2 { 3 $getter = ’get’ . $name; 4 if (method_exists($this, $getter)) { 5 return $this->$getter(); 6 } else { 7 // Ë✏Ÿ* else ⌃/ÑÖπc/ yii\base\Object::__get() ÑK⌅ 8 $this->ensureBehaviors(); 9 foreach ($this->_behaviors as $behavior){ 10 if ($behavior->canGetProperty($name)) { 11 return $behavior->$name; 12 } 13 } 14 } 15 if (method_exists($this, ’set’ . $name)) { 16 throw new InvalidCallException(’Getting write-only property: ’ . get_class($this) . ’::’ . $name); 17 } else { 18 throw new UnknownPropertyException(’Getting unknown property: ’ . get_class($this) . ’::’ . $name); 19 } 20 } Õπe↵ yii\base\Compoent::__get() yii\base\Object::__get() ÑK⌅1 /(é˘é*öIgetter˝pKÑ⌅⌃ yii\base\Object /Ù•õ˙⇥8J…`ÛÅøÓÑ^ 'X(K{⇥F/ yii\base\Component ⇡/(X(getterKÿÅ↵↵//ËeÑL:Ñ ^'⇥ • ñ H ⇤ ( Ü $this->ensureBehaviors() ⇥ Ÿ *;Å / ˘P{ ( Ñ yii\base\Compoent ° ˚ U Ñ H Ë e Ñ L : @  Ÿ * ⇤ ( ° ( ⇥ F / ˘ é P {`Ô˝Õ}Ü yii\base\Compoent::behaviros() eÑHËeõL:£HŸ*˝ p⇢⌃ŸõL:HËe€e⇥ • 6 À M Ü $this->_behaviors ⇥Yii⌃ { @ — ö Ñ L : ˝ › X ( yii\base\Compoent::$_behaviors[] pƒ-⇥ • $≠Ÿ*^'/&/@—öL:ÑÔ˚^'Çú/1‘fiŸ*L:ÑŸ*^' $behavior->name ⇥ ˘ésetter„{<ŸÃ1`(«EÜ⇥ πππ ’’’ ÑÑÑ ËËË eee ^'ÑËe⇢« __get() __set() T/π’{ensureBehaviors(); 4 foreach ($this->_behaviors as $object){ 5 if ($object->hasMethod($name)) { 6 return call_user_func_array([$object, $name], $params); 7 } 8 } 9 throw new UnknownMethodException(’Calling unknown method: ’ . get_class($this) . "::$name()"); 10 } 32 Chapter 2. Yii ˙˙˙ @@@ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 Œ⌦bÑ„-ÔÂ↵˙YiiH/⇤(Ü $this->ensureBehaviors() ⇥Ÿ*π’(Mb≤« Ü⇥_1/ÙøÓ{,´X(Ñ^'π’ˆŸ*π’˝⇢´⇤(Ñ⇥Ÿπ'˝⌦Ñ_1/: Ü–õ⌦–õø)/✓óÑ⇥ 6_/MÜ yii\base\Component::$_behaviros[] pƒÇú@—öÑL:-Å ⇤ ( Ñ π ’ X ( ⇡  (PHPÑ call_user_func_array() ⇤ ( K ⇥ Œ Ÿ à _ Ô Â ↵ ˙ yii\base\Components::$_behaviros[] ˘éL:Ñπ’w b✏⌦ÑÅB⇧Ù˘π’Ñ ~ ÅB⇥Ÿ*ÅB/1 call_user_func_array @≥öÑ⇥wSÔÂ↵↵PHPKå⇥ 2.3.11 LLL :::ÁÁÁ  ååå yyy ''' Traits ÑÑÑ ::: +++ Œû∞ÑHú↵`//⇢§:Yii/⇢d>PHP-Åæ0Ÿ7ÑHúÔÂ(Á@ ÔÂ(PHP∞eÑy'Traits @⇥F/L:w Áåy'@° Ñ⇠πŒûE(Ñ“¶ ≤Áåy'Ù`ïBπ⇥`ïB1✏s@—HáN–LHáÿ⇥L:Ñe/ÂÔ•◊ Ñ–LHázr:⇣,↵÷—Há'–GÑpV⇥ LLL :::ÁÁÁ  ñHe≤ˇL:Á‘ÉŒ;ë⌦/˘ÑŸ$⇧/(åhÑBb⌦Ñãi/˘ IÑ⇥K@€L‘É/‡:(û∞ÑHú⌦$⇧ Ñ{<Ñ0π⇥↵weL:åÁ˝Ô *{w Ê*{Ñ^'åπ’Œæ0iE{Ñü˝ÑÓÑ⇥ ¯‘Éé(ÁÑπ✏eiE{ü˝(L:Ñπ✏/≈˘∞ {€LÓ9å /PHP/⇢ÁF/YiiÔ—ö⇢*L:Œæ0{<⇢ÁÑHú⇥ Õ«eL:/›˘‡’ˇ„ÁÑ⇥ö2∫é2∫˝/0⇤∫`ÔÂ⌃ö2∫åé2∫S⇣ 0⇤∫e˘Ö⇥F/`›˘˝äÍ(–õπbh∞óœ∫Ñ4PÑS⇣∫e˘Ö⇥ ŸÃ1U≤Ü⇥Œ,(⌦e≤L:Í/Õæ°!✏/„≥ÓòÑπ’f⇥Á⇡ /PHP\:↵Ì@–õÑy'9,(*B!⌦⇥ LLL :::yyy ''' y'/PHP5.4KeÑ*∞feature⇥Œû∞Hú↵L:y'˝æ0äÍ´Ñpublic ÿœ ^'π’Ëe0SM{-ªÑÓÑ⇥((⌦÷Ï_⌅ @F;Ñü⇡Ô ↵bÑ–:€L ä·⇥ >⌘é(L:Ñ≈µ⇢ • L:Œ,(⌦≤_/PHPÑ{‡d*L:ÔÂÁÍÊ*L:Œû∞„Ñ(⇥ y'Í/PHPÑÕÌ’Hú⌦{<éäy'Ñ„¸e0Ü{-Œû∞„ÑËey '//ÁÑ⇥ • L:Ô®0—ö„d≈Å˘{€LÓ9⇥F/y'≈{({(( use ÌÂÅ„ dy'ˆ⇡Å dŸ*ÌÂ⇥b›ÙÅ˘{€LÓ9⇥ • L:ÿÂ((Mn6µ€L—öy'1LÜ⇥ • L:ÔÂ(é˘ãˆ€LÕày'L⇥ • S˙∞}≤ňL:⇢ÍLíd≤ÅÍ®(H—öÑL:⇥y'(—≤ňŠ∫:rÑÓ9—≤ÅÑÿœ^'π’⇥ >⌘é(y'Ñ≈µ⇢ • y'‘L:(Há⌦Åÿπ‡:L:vû/{ÑûãňÙåzÙ€L⌃M⇥ • y'/PHPÑÌ’‡dIDEÑ/Å}õ⇥ÓMÿ° IDE˝/L:⇥ 2.3. LLL ::: Behavior 33 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 34 Chapter 2. Yii ˙˙˙ @@@ CHAPTER THREE YII ¶¶¶ ööö ŸË˝;Å≤Ñ/Yii-¶öÑπ✏eû∞Ñü˝⇧Ù/Ô(Ñ!✏⇥8¡Ñ¶ö´«é ÿ§<Ü⇥ Yii⇢«¶öõ⇢(ÑÖπóŸË⌃Öπ(↵Ñ«↵-`≈ç±9æõª ⌥ö⇥ Ÿ_/–ÿHáÑÕπ✏⇥ S6‚6K:¶ö1Ù≈/®P'˙Æ'Ñv^/:6'⇥_1/Ù`/ÔÂÙ 9Ÿõ¶öÑÖπÑ⇥ F/d^ ›˘Ñ⌃1&⇡˙Æè✏Ù9YiiæöѶö⇥ Ê ˘¶öÖπ @Ù9öÅ(„-€LÙ⇥ YiiѶöÖπ;Å⇧+î(Ñ+˘aMnÓU”ÑÍ®†}IÖπ⇢ 3.1 +++ ÔÂ⌃+∆:yäÑ8œÿœ÷Ñ\((éM⌃õáˆÔÑURLÂlÑπ✏ôe„ -⇧⇢⌅˙∞2ÑáˆÔÑURL⇥ 3.1.1 ÑÑÑ ööö III ÑÑÑ +++ Yii-+ @ 4Â:+éc8ÑáˆÔÑåURL⇥Yii-ÑöIÜ∏⇢8(Ñ+Œe„ ⇢,ሠindex.php -æ↵˙+;Å/>( aliases.php áˆ-: 1 run(); („-–0Ñ aliases.php áˆ-›XÜ∏⇢ÑöIå—⇧ÍöIÑ+: 1 __DIR__]; yii\BaseYii::$aliases (é›X@ Ñ+ÿ§0ä yii\BaseYii.php @(ÑÓU\ : @yii +⇥ Ê˘é yii\base\Application (vÀ«↵- Â↵„: 1 public function preInit(&$config) 2 { 3 ... ... 4 5 // basePath≈{(Mnáˆ-Ÿ˙&⇡⇢õ˙⇤8 6 if (isset($config[’basePath’])) { 7 // ŸÃ⇢æn @app 8 $this->setBasePath($config[’basePath’]); 9 unset($config[’basePath’]); 10 } else { 11 throw new InvalidConfigException(’The "basePath" configuration for the Application is required.’); 12 } 13 14 // @vendor ÇúMnáˆ-ænÜ vendorPath (MnÑ<&⇡(ÿ§Ñ 15 if (isset($config[’vendorPath’])) { 16 $this->setVendorPath($config[’vendorPath’]); 17 unset($config[’vendorPath’]); 18 } else { 19 $this->getVendorPath(); 20 } 21 22 // @runtime ÇúMnáˆ-ænÜ runtimePath (MnÑ<&⇡(ÿ§Ñ 23 if (isset($config[’runtimePath’])) { 24 $this->setRuntimePath($config[’runtimePath’]); 25 unset($config[’runtimePath’]); 26 } else { 27 $this->getRuntimePath(); 28 } 29 30 ... ... 31 } ⌦bÑ„-ÑöIÜ3*+⇢ @app @vendor @runtime ⇥v- basePath /+ F≈{1—⇧ÍÒ(Mnáˆ-æöh:î(Ñ9ÓU˘éfrontend1/ÓU path/to/yii/application/frontend ⇥(öI basePath ˆYiizøöIÜ @app „( yii\base\Application::setBasePath() -: 1 public function setBasePath($path) 2 { 3 parent::setBasePath($path); 4 Yii::setAlias(’@app’, $this->getBasePath()); 5 } ÔÂ↵˙ @app basePath /ÙÑ⇥ ( yii\base\Application Ñ À « ↵ - Ç ú ° ( M n á ˆ M n vendorPath runtimePath ⇡Yii⇢⇤( getVendorPath() getRuntimePath() Ÿ$*π’⌃+(ÿ§ <öI @vendor å @runtime 1 public function getVendorPath() 2 { 36 Chapter 3. Yii ¶¶¶ ööö ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 3 if ($this->_vendorPath === null){ 4 $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . ’vendor’); 5 } 6 7 return $this->_vendorPath; 8 } 9 10 public function getRuntimePath() 11 { 12 if ($this->_runtimePath === null){ 13 $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . ’runtime’); 14 } 15 16 return $this->_runtimePath; 17 } ⌦bÑ„-⇢⌃+⌃ @vendor æ: @app/vendor ⌃ @runtime æ: @app/runtime ⇥ F/ŸÃ *‘ÉyäÑ1/ @vendor ˘é(Yii˙@!H˙Ñî(⇢(⌦b –0Ñ @app/vendor F/˘é(ÿß!H˙Ñî(`⇢—∞vendorÓUv( frontend backend ÓU↵/fl÷Ï/DÓU⇥Ÿ/‡:˘ét*Â↵Ÿ*vendorÑÖπ/ frontend å backendIq(Ñ⇥‡dûE⌦ @vendor îÂ/ @app/../vendor ⇥Ÿ*Yii_Úœ⇤Q0 Ü((ÿß!˙î(ˆÿ§Ñ config/main.php Mnáˆ⇢æö vendorPath Mny: dirname(dirname(__DIR__)) . ’/vendor’ Ÿ71MÜ(„-ÿ§Ñ<⇥ ÿ *+ +Ñ„( yii\base\Web\Application -: 1 protected function bootstrap() 2 { 3 $request = $this->getRequest(); 4 Yii::setAlias(’@webroot’, dirname($request->getScriptFile())); 5 Yii::setAlias(’@web’, $request->getBaseUrl()); 6 7 parent::bootstrap(); 8 } ŸÃ†eÜ$*∞Ñ+ @webroot @web ⇥ *œ +Ñ0π(éYiiÑiUextensions S(Composerâ≈iU⇢⌘ @vendor/yiisoft/extensions.php ôe·ov-1⇧+¯îÑ+Í«Ÿõ+⇢8 ˝/åß+⇥6( yii\base\Application::bootstrap() -⌃ŸõiUÑ+€LË å⇥ ñH↵↵*xãÑ extensions.php 1 7 array ( 8 ’name’ => ’yiisoft/yii2-swiftmailer’, 9 ’version’ => ’9999999-dev’, 10 ’alias’ => 11 array ( 12 ’@yii/swiftmailer’ => $vendorDir . ’/yiisoft/yii2-swiftmailer’, 13 ), 14 ), 15 16 ... ... 17 18 ’yiisoft/yii2-gii’ => 19 array ( 3.1. +++ 37 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 20 ’name’ => ’yiisoft/yii2-gii’, 21 ’version’ => ’9999999-dev’, 22 ’alias’ => 23 array ( 24 ’@yii/gii’ => $vendorDir . ’/yiisoft/yii2-gii’, 25 ), 26 ), 27 ); Ë ✏ ⌦ b Ÿ µ „ - Ñ alias Ÿ * . ˘ î Ñ 1 / *+   v @ „ h Ñ û E Ô Ñ ⇥ Û é w S ˘ Ÿ * extensions.php Ñ Ö π € L ⌅ ⌃ v Ë å ⇣ + Ñ Â w 1 yii\base\Application::bootstrap() å⇣: 1 protected function bootstrap() 2 { 3 // ⌃ extensions.php ÑÖπ˚÷€ $this->extensions ⌥( 4 if ($this->extensions === null){ 5 $file = Yii::getAlias(’@vendor/yiisoft/extensions.php’); 6 $this->extensions = is_file($file) ? include($file) : []; 7 } 8 9 // MÜ $this->extensions vËå+ 10 foreach ($this->extensions as $extension){ 11 if (!empty($extension[’alias’])) { 12 foreach ($extension[’alias’] as $name => $path){ 13 Yii::setAlias($name, $path); 14 } 15 } 16 ... ... 17 } 18 } ✏”↵ÿ§ÑöI+q 10*v-ÔÑ+9*URL+Í @web 1*⇢ • @yii h:YiiF∂@(ÑÓU • @app h:c(–LÑî(Ñ9ÓU • @vendor h:, πì@ ÓU • @runtime h:c(–LÑî(Ñ–Lˆ(éX>–LˆáˆÑÓU • @webroot h:c(–LÑî(Ñe„ሠindex.php @(ÑÓU • @web URL+h:SMî(Ñ9URL • @common h:⇢(áˆ9 • @frontend h:Mî(@(Ñáˆ9 • @backend h:î(@(Ñáˆ9 • @console h:}‰Lî(@(Ñáˆ9 • v÷(Composerâ≈ÑYiiiUËåÑåß+⇥ Ÿ7(t*Yiiî(-ÍÅ(⌦+1Ôπøfl0h:yöÑÔÑURL⇥ 3.1.2 ööö III„„„ êêê +++ Yii( Yii::$aliases[] e›X+öI+1/⌃+ v„hÑûEÔÑURLôe Ÿ*pƒ„ê+1/⌃+Ñ·oŒpƒ˚÷˙ªvƒ⇥ 38 Chapter 3. Yii ¶¶¶ ööö ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 +++ ÑÑÑ ööö III ««« ↵↵↵ dÜœ⌦bÑ„£7öI*+Kÿ v÷Ñ(’: 1 // (*ÔÑöI*ÔÑ+ 2 Yii::setAlias(’@foo’, ’path/to/foo’); 3 4 // (*URLöI*URL+ 5 Yii::setAlias(’@bar’, ’http://www.example.com’); 6 7 // (*+öIÊ*+ 8 Yii::setAlias(’@fooqux’, ’@foo/qux’); 9 10 // öI*“åß”+ 11 Yii::setAlias(’@foo/bar’, ’path/to/foo/bar’); Œ⌦bÑ„-ÔÂÜ„0 Yii::setAlias() /öI+Ñs.⇥ûE⌦Âπ’Ñ„( BaseYii::setAlias() -: 1 public static function setAlias($alias, $path) 2 { 3 // ÇúflöIÑ+v^Â@S4⇡(Mb†⌦@ 4 if (strncmp($alias, ’@’, 1)) { 5 $alias = ’@’ . $alias; 6 } 7 8 // ~0+Ñ,µs@ 0 / KÙÑÖπÇ@foo/bar/quxÑ@foo 9 $pos = strpos($alias, ’/’); 10 $root = $pos === false ? $alias : substr($alias, 0, $pos); 11 12 if ($path !== null){ 13 // ªdÔÑ+>Ñ \/⇥ÇúÔÑ,´1/*+Ù•„ê˙e 14 $path = strncmp($path, ’@’, 1) ? rtrim($path, ’\\/’) : static::getAlias($path); 15 16 // ¿Â/& $aliases[$root] ↵↵/&ÚœöI}Ü9+⇥Çú° ⇡Â$root:.›XŸ*+ 17 if (!isset(static::$aliases[$root])) { 18 if ($pos === false){ 19 static::$aliases[$root] = $path; 20 } else { 21 static::$aliases[$root] = [$alias => $path]; 22 } 23 // Çú $aliases[$root] ÚœX(⇡ˇb⇣∞ÑÔÑû†∞ÑÔÑ 24 } elseif (is_string(static::$aliases[$root])) { 25 if ($pos === false){ 26 static::$aliases[$root] = $path; 27 } else { 28 static::$aliases[$root] = [ 29 $alias => $path, 30 $root => static::$aliases[$root], 31 ]; 32 } 33 } else { 34 static::$aliases[$root][$alias] = $path; 35 krsort(static::$aliases[$root]); 36 } 37 38 // S eÑ $path : null ˆh:Å dŸ*+⇥ 39 } elseif (isset(static::$aliases[$root])) { 40 if (is_array(static::$aliases[$root])) { 41 unset(static::$aliases[$root][$alias]); 42 } elseif ($pos === false){ 43 unset(static::$aliases[$root]); 44 } 3.1. +++ 39 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 45 } 46 } ˘é+ÑöI«↵⇢ +++ ƒƒƒ ⇤⇤⇤ ÇúÅöIÑ+ $alias v^ @ S4Í®:Ÿ*+†⌦ @ M⇥ ;KÍÅ/+ ≈6 @ S4⇥↵bÑ$*̽öIܯÑ+ @foo Yii::setAlias(’foo’, ’path/to/foo’); Yii::setAlias(’@foo’, ’path/to/foo’); ∑∑∑ ÷÷÷ 999 +++ $alias Ñ9+1/ @ †⌦,* / KÙ0Öπ $root h:⇥ ŸÃÔÂ↵˙ +/⌃B!Ñ⇥↵b3*ÌÂÑ9+˝/ @foo 1 Yii::setAlias(’@foo’, ’path/to/some/where’); 2 3 Yii::setAlias(’@foo/bar’, ’path/to/some/where’); 4 5 Yii::setAlias(’@foo/bar/qux’, ’path/to/some/where’); ∞∞∞ ööö III +++ ÿÿÿ /// ddd +++ Çú eÑ $path / null Ù/c8Ñ+öI⇥ ˘éc8Ñ+ öI1/Ä BaseYii::$aliases[] Ãôe·o⇥ Çú $path : null Ù/Å d+ : 1 // öI+@foo 2 Yii::setAlias(’@foo’, ’path/to/some/where’); 3 4 // d+@foo 5 Yii::setAlias(’@foo’, null); „„„ êêê $path ˘é∞öI+‚6 $path : null £HH€L„ê⇢ Çú $path  @ S4Ù Ÿ/_/*+⇡⇤( Yii::getAlias() v⌃„êÑ”ú\:∞Ñ $path Çú $path  @ S4Ù/*c8Ñpath URL £Hªd $path +>Ñ / å \ ⇥ +++ ÑÑÑ ôôôeee ˘ é h ∞ Ñ + _ s v 9 + / ∞ Ñ BaseYii::aliases[$root] X ( ⇥ £ H h ∞ + Ñ ôe⌃ $ Õ ≈ µ ⇢ Ç ú h ∞ + , ´ 1 / 9 + £ H Ù • BaseYii::aliases[$alias] = $path Çúh∞Ñ+v^/*9+sb Ç @foo/bar & å ß ß I Ô Ñ Ñ BaseYii::aliases[$root] = [$alias => $path] ⇥‘Ç: 1 // BaseYii::aliases[’@foo’] = [’@foo/bar’ => ’path/to/foo/bar’] 2 Yii::setAlias(’@foo/bar’, ’path/to/foo/bar’); 3 4 // BaseYii::aliases[’@qux’] = ’path/to/qux’ 5 Yii::setAlias(’@qux’, ’path/to/qux’); ˘é9+ÚœX(Ñ+(ôeˆ1Å⇤QÜ÷∞ûÑÓòÜ: 1 // À BaseYii::aliases[’@foo’] = ’path/to/foo’ 2 Yii::setAlias(’@foo’, ’path/to/foo’); 3 4 // Ù•Ü÷ BaseYii::aliases[’@foo’] = ’path/to/foo2’ 5 Yii::setAlias(’@foo’, ’path/to/foo2’); 6 7 /** 8 * ∞û 9 * BaseYii::aliases[’@foo’] = [ 10 * ’@foo/bar’ => ’path/to/foo/bar’, 11 * ’@foo’ => ’path/to/foo2’, 12 * ]; 13 */ 14 Yii::setAlias(’@foo/bar’, ’path/to/foo/bar’); 15 40 Chapter 3. Yii ¶¶¶ ööö ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 16 // À BaseYii::aliases[’@bar’] = [’@bar/qux’ => ’path/to/bar/qux’]; 17 Yii::setAlias(’@bar/qux’, ’path/to/bar/qux’); 18 19 // Ù•Ü÷ BaseYii::aliases[’@bar’] = [’@bar/qux’ => ’path/to/bar/qux2’]; 20 Yii::setAlias(’@bar/qux’, ’path/to/bar/qux2’); 21 22 /** 23 * ∞û 24 * BaseYii::aliases[’@bar’] = [ 25 * ’@bar/foo’ => ’path/to/bar/foo’, 26 * ’@bar/qux’ => ’path/to/bar/qux2’, 27 * ]; 28 */ 29 Yii::setAlias(’@bar/foo’, ’path/to/bar/foo’); Ë✏Çú9+˘îÑ/*pƒ(∞ûÜ÷ Yii⇢⇤(PHPÑ krsort() äpƒ g. <Õ∞⌃⌘íè⇥ ŸÔ Hn›Ñ+⇢>(ÌÑ{Â+Mb ‘Ç @foo/bar/qux ⇢´>( @foo/bar 7´>(9+ @foo K↵ FÑ£*⇢´>(Mb⇥ +++ ÑÑÑ ddd e Ñ $path : null h : Å d + ⇥ Yii (PHPÑ unset() Ë BaseYii::$aliases[] pƒ-Ñ˘îC æ0 d+ÑÓÑ⇥ Ë✏ d+ Å⇤( krsort() ˘pƒ€L⌅⌃⇥ +++ ÑÑÑ „„„ êêê ««« ↵↵↵ öI«↵( Yii::setAlias() ¯˘î+Ñ„ê«↵( Yii::getAlias() ûE„ ( BaseYii::getAlias() -: 1 public static function getAlias($alias, $throwException = true) 2 { 3 // ⌥Â@S4Ñ+˝/‡HÑ 4 if (strncmp($alias, ’@’, 1)) { 5 return $alias; 6 } 7 8 // Hnö9+ $root 9 $pos = strpos($alias, ’/’); 10 $root = $pos === false ? $alias : substr($alias, 0, $pos); 11 12 // Œ9+À~wÇú9+°~0⌥M 13 if (isset(static::$aliases[$root])) { 14 if (is_string(static::$aliases[$root])) { 15 return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos); 16 } else { 17 // 1éôeM(Ü krsort() @ÂÉÑ+⇢´HMÜ0⇥ 18 foreach (static::$aliases[$root] as $name => $path){ 19 if (strpos($alias . ’/’, $name . ’/’) === 0){ 20 return $path . substr($alias, strlen($name)); 21 } 22 } 23 } 24 } 25 26 if ($throwException){ 27 throw new InvalidParamException("Invalid path alias: $alias"); 28 } else { 29 return false; 30 } 31 } +Ñ„ê«↵¯˘ÄU⇢ 3.1. +++ 41 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 • H 9+~0Ô˝›X+Ñ⌃/⇥ • MÜŸ*⌃/↵Ñ@ ⌘ˆ⇥1éKMˆP+ / .<⌃íèÑ@Â⇠H9M+⇥ • ⌃~0Ñ9M+ˇb⇣v@˘îÑ<ç•⌦ @alias ÑJ*⇣:∞Ñ+⇥ +Ñ„ê«↵ÔŸH↵: 1 // ‡HÑ++≈{Â@S4+˝>(-Ù 2 // F/ÌÂ⇢˙⇡⇢§:Ÿ/*ÔÑWÿÑÔÑ⇢ path/to/@foo/bar 3 Yii::getAlias(’path/to/@foo/bar’); 4 5 // öI @foo @foo/bar @foo/bar/qux 3*+ 6 Yii::setAlias(’@foo’, ’path/to/foo’); 7 Yii::setAlias(’@foo/bar’, ’path/2/bar’); 8 Yii::setAlias(’@foo/bar/qux’, ’path/to/qux’); 9 10 // ~0 @foobar9+õ˙⇥8 11 Yii::getAlias(’@foobar/index.php’); 12 13 // 9M@foo¯Sé path/to/foo/qux/index.php 14 Yii::getAlias(’@foo/qux/index.php’); 15 16 // 9M@foo/bar/qux¯Sé path/to/qux/2/index.php 17 Yii::getAlias(’@foo/bar/qux/2/index.php’); 18 19 // 9M@foo/bar¯Sé path/to/bar/2/2/index.php 20 Yii::getAlias(’@foo/bar/2/index.php’); 3.1.3 ✏✏✏ ””” • +((MöI‡d⇢8e≤öI+îS(>(î(ÑÀ6µ⇥ • +≈6 @ S4⇥ • +ÑöIÔÂ(KMÚœöI«Ñ+⇥ • +(®XˆÛ⇢Í⌃⇣$ß,ßÑ./9+⇥ ,åß+Ñ./åtÑ+/ ªd9+i↵Ñ@◆Ñ“åß”+⇥ • Yii⇢«⌃BÑ⌘”Ñe›X+;Å/:ÿH¿"\∆⌥⇥ • à⇢0πÔÂÙ•(+(⇤( Yii::getAlias() lb⇣ûÑÔÑURL⇥ • +„êˆ⇠H9MÉÑ+⇥ • YiiÑöIÜ∏⇢8(Ñ+õ↵ˆ(⇥ • (+ˆÅ⌃+>(Mb˝>(-Ù⇥ 3.2 MMM nnn yyy configuration (TBD) Ù0Mny˚⇧↵ÀÏ,Õî//YiiÑMnሟ/µMnáˆÑ„: 1 ’db’ => [ 2 ’class’ => ’yii\db\Connection’, 3 ’dsn’ => ’mysql:host=localhost;dbname=mylocaldb’, 4 ’username’ => ’username’, 5 ’password’ => ’password’, 6 ’charset’ => ’utf8’, 7 ’tablePrefix’ => ’tbl_’, 8 ], 9 ’mail’ => [ 42 Chapter 3. Yii ¶¶¶ ööö ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 10 ’class’ => ’yii\swiftmailer\Mailer’, 11 ’viewPath’ => ’@common/mail’, 12 ’useFileTransport’ => true, 13 ], ⌦b$*Mny˝÷ÍûEyÓ*÷Ñ/ components ÑMnÖπ⇥ Yii-∏⇢0π˝Å(0Mnyî(åv÷‡N⌥{˘aÑ˙ÀMn˝Å(0Mn y⇥Mny/à˘˘aÑ_1/ÙMnyö/(éMn–*˘a(éÀMn˘a Ñ ^' ⇥ 3.2.1 MMM nnn yyy ÑÑÑ <<< ✏✏✏ ÄUe≤*Mny«(↵bÑ<✏: 1 [ 2 ’class’ => ’path\to\ClassName’, 3 ’propertyName’ => ’propertyValue’, 4 ’on eventName’ => $eventHandler, 5 ’as behaviorName’ => $behaviorConfig, 6 ] \:Mny⇢ • MnyÂpƒ€Lƒ« • class pƒC h:⌃Å˙Ñ˘aÑåt{ • propertyName pƒC h:⌥ö: propertyName ^'ÑÀ< • on eventName pƒC h:⌃ $eventHandler —ö0˘aÑ eventName ãˆ- • as behaviorName pƒC h:( $behaviorConfig ˙*L:vËe0˘a-⇥ Ÿ ÃÑ $behaviroConfig _/*Mny • MnyÔÂLW⇥ 3.2.2 MMM nnn yyy ÑÑÑ üüü ⌃⌃⌃ Yii⇤( Yii::createObject() å Yii::configure() ⌃ eÑMny\(é˘aM⇧(é ˙*˘a⇧(éMn*˘a⇥ Yii::createObject() Ñ„( BaseYii::createObject() -: 1 public static function createObject($type, array $params = []) 2 { 3 if (is_string($type)) { 4 return static::$container->get($type, $params); 5 } elseif (is_array($type) && isset($type[’class’])) { 6 $class = $type[’class’]; 7 unset($type[’class’]); 8 return static::$container->get($class, $params, $type); 9 } elseif (is_callable($type, true)) { 10 return call_user_func($type, $params); 11 } elseif (is_array($type)) { 12 throw new InvalidConfigException(’Object configuration must be an array containing a "class" element.’); 13 } else { 14 throw new InvalidConfigException("Unsupported configuration type: " . gettype($type)); 15 } 16 } 3.2. MMM nnn yyy configuration (TBD) 43 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 3.3 Yiiîîî ((( ÑÑÑÓÓÓUUU ””” ÑÑÑ Â↵/*⇢«ÿß!Hâ≈xãÑYiiî(ÑÓU”Ñ: 1 . 2 ⌧-- backend 3 ⌧-- common 4 ⌧-- composer.json 5 ⌧ composer.lock 6 ⌧-- console 7 ⌧-- environments 8 ⌧-- frontend 9 ⌧-- init 10 ⌧-- init.bat 11 ⌧-- LICENSE.md 12 ⌧-- README.md 13 ⌧-- requirements.php 14 ⌧-- vendor 15 -- yii.bat ˘éÿßî(¯Sé backend frontend console *ÏÀÑYiiî(⇥1é console {Ñî(‘Éyä⌘Ïç≤⇥ŸÃ≤xãÑWebî(ÑÓU”ú⇥ 3.3.1 lllqqqÓÓÓ UUU ŸÃÑlqÓUÔb common ÓUŸŒWbe↵° ˚UëI1/lqÓU⇥ common Ñ \(h:ŸÓU↵Ñ⌧˘é,ÿßî(Ñ˚ÏÀÑî(˝/Ô¡Ô(Ñ⇥,≈µ ↵ common w Â↵”Ñ: 1 . 2 ⌧-- codeception.yml 3 ⌧-- config 4 ⌧-- mail 5 ⌧-- models 6 -- tests v-⇢ • config 1/⇢(ÑMnŸõMn⌃\(éMå}‰L⇥ • mail 1/î(ÑMå}‰LÑƈ¯sÑ⇤@áˆI⇥ • models 1/Må}‰L˝Ô˝(0Ñpn!ã⇥Ÿ_/ common -;ÅÑË⌃⇥ • tests 1/î(Ñ⇢(K’ÑÖπ⇥ dÜ common Kÿ *àÕÅÑlqÓU vendor Ÿ*ÓUŒWbÑ✏↵1/⌅Õ, πÑ↵è⇥Ÿ/Composerâ≈Ñv÷↵èÑX>ÓU⇧+YiiF∂,´_>(Ÿ*ÓU↵b⇥Ç ú`⌘ composer.json ÓUû†Ü∞ÑÅâ≈Ñ↵è£H↵!⇤(Composerш⇡⌃⇢ä∞ â≈ÑÓU_â≈(Ÿ* vendor ↵b⇥ }Ü∞(ÓòeÜ⇥˘é frontend backend console IÏÀÑî(÷ÏÑÖπ>(⌅ ÍÑÓU↵b÷ÏÑ–\≈6(0YiiF∂I vendor -Ñ↵è⇥÷Ï/ÇUsTweÑ⇥Ÿ*ÿ∆ ⇧Ùt*Yiiî(ÑÓU”ÑÑÿ∆1⇧+(* Ù-Ñ:e„áˆÑ0π⇥ F/(Ü„e„áˆindex.phpKM ≈ÅH↵↵¯Ç frontend IÏÀî(ÑÓU”Ñ⇥Ÿ‘w t*Yiiî(ÑÓU”ÑbÙ:ÕÅ⇥‡:`ÄÄ/( frontend IÓU↵ô„F/'⇢ ( path\to\yii-application ÓU↵ô„⇥ 44 Chapter 3. Yii ¶¶¶ ööö ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 3.3.2 frontend îîî ((( ÑÑÑÓÓÓUUU ””” ÑÑÑ xãÑ frontend w Ç↵Ñ*ÓU”Ñ: TODO gzèe≤⇢ • assets ÓU(éX>MÔDê⇧PHP{⇥ŸÃÅÜ„¿H/MÔDê⇧ÍÅ'ÙÂS/( é°⌃CSSjsIMbDêÑ1ÔÂe⇥ • config (éX(X>,î(ÑMnáˆ⇧+;Mnሠmain.php åh@-¬pMnሠparams.php ⇥ • models views controllers ÓU⌃+(éX>pn!ã{∆˛áˆß6h{⇥Ÿ*/⌘ ÏÑ8√_/⌘ÏÂ\⇢ÑÓU⇥ • widgets ÓU(éX>õ8(Ñ✏⇥ˆÑ{áˆ⇥ • tests ÓU(éX>K’{⇥ • web ÓUŒWÔÂ↵˙Ÿ/*˘éWeb°hÔÂøÓÑÓU⇥dÜŸÓUv÷@ ÑÓUî˘Web(7¥2˙e⇥Ÿ/âhÑÅ⇥ • runtime Ÿ*ÓU/ÅB/ chmod 777 ŸÅBWeb°hw åhÑCP‡:Ô˝⇢â  0ôe4ˆáˆI⇥F/*ÓUv*˘Web(7Ô¡⇥_1/ÙCPŸÜF/v/Web( 7ÔÂøÓ0Ñ⇥ backend ÓU frontend Ñ”ÑÖπ/77Ñ⇥@◆ÑMåÍ/∫:Œ;ë ⌦˘Webî(Ñü˝✓⌃ÓÑ(éMNî(у!åB↵¶øéÙ§å(⇥Œ„“¶e ≤Yiiã91§óÍ*/MÍ*/⇥ Mb–0Ñ Ù-Ñe„ሠindex.php 1Mé web ÓU↵b⇥ 3.3.3 eee „„„ ááá ˆˆˆ index.php ñHe↵↵ index.php áˆÑÖπ: 1 run(); M$L/$* define ÌÂöISMÑ–L!✏åØÉ⇥ÇúöIÜ YII_DEBUG £Hh:S M:⇤’∂î((–L«↵-⇢ õ⇤’·oÑì˙(õ˙⇥8ˆ_⇢ *Ê∆Ñ⇤ (Ñ>:⇥ÿ§≈µ↵ YII_DEBUG : false ⇥F(—«↵-} ⌦bôÑ£7öI: true Ÿ7øéÂ~å⌃ê⇡Ô⇥ ÇúöIÜ YII_ENV £H1/⌥öÜSMî(Ñ–LØÉ⇥⌦bÑ„>:î(⌃–Lé dev ØÉ⇥ÿ§≈µ↵ YII_ENV : prod ⇥ŸõØÉÍ/*wSÑÖπÅ↵ØÉÑöI⇥F/ dev prod /â≈ÿ§Ñ$*ØÉ⌃+h:—ØÉå»Ñ⇣¡ØÉ⇥dÿ * test Ø Éh:K’ØÉ⇥ 3.3. Yiiîîî ((( ÑÑÑÓÓÓUUU ””” ÑÑÑ 45 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 ØÉ!✏Ñ\(⇥ØÉ(„-;Å/qÕMnáˆ⇥ YII_ENV Ñ dev prod test Õ ØÉ⇢⌃+ YII_ENV_DEV YII_ENV_PROD YII_ENV_TEST Ñ<: true ⇥Ÿ7(î(ÑMn -y+/(¯Ñ*Mnáˆ-Ô¢ØÉ\˙ÑMn⇥ ‘Ç`(K’ØÉ↵(Ê*pnì⇥(—ØÉ↵/(⇤’Â\aII⇥£H ÔŸHZ: 1 $config = [...]; 2 3 if (!YII_ENV_TEST) { 4 // configuration adjustments for ’test’ environment 5 $config[’bootstrap’][] = ’debug’; 6 $config[’modules’][’debug’] = ’yii\debug\Module’; 7 $config[’modules’][’gii’] = ’yii\gii\Module’; 8 } vûŸ* YII_ENV ÑöIÍ/* init ⇢,ØÉ⌥bÑ*¯íeE⇥Çú⌅ØÉ‘É p( init e⌥b⌅ÕØÉÑMnåhÜ⇥≈(⇢,-ç Ç YII_ENV_TEST K{Ñ$≠Ì Â⇥ '•@$* define ÌÂK1/ * require ÌÂ⇥ *ÌÂ-˝(0ܯ˘éSMÓU Ñv÷ÓU-Ñphpáˆ⇥ __DIR__ h:SMሠindex.php @(ÑÓU⇥ /../../ h:Ñ/SM ÓUÑ77ÓU index.php ÑSMÓU/ /path/to/yii-application/frontend/web 8 8ÓU1/ frontend 77ÓU1/ yii-application Ü⇥ ,*ÓUeÜ /path/to/yii-application/vendor ↵bÑ autoload.php ⇥Ÿ* /composerÑ{Í®†}:6Ëåáˆ⇥eŸ*áˆÔÂ(composerÑ{Í®†}ü˝⇥ ,å*ÓUeÜ vendor ↵bÑ yiisoft/yii2/Yii.php Ÿ/YiiÑ{áˆ⇥eÜŸ*{ áˆM˝(YiiÑ⌅Õ{⇥ , *ÓUeÜ /path/to/yii-application/common ↵bÑ config/aliases.php Ÿ* áˆöIÜ˚⌫ÑÔÑ+: 1 (bn›v“eÑautoloader⇢>(Mb 9 require(__DIR__ . ’/../../vendor/yiisoft/yii2/Yii.php’); 10 // bîç autoloaderÜ 11 12 require(__DIR__ . ’/../../common/config/aliases.php’); 13 14 $config = yii\helpers\ArrayHelper::merge( 15 require(__DIR__ . ’/../../common/config/main.php’), 16 require(__DIR__ . ’/../../common/config/main-local.php’), 17 require(__DIR__ . ’/../config/main.php’), 18 require(__DIR__ . ’/../config/main-local.php’) 19 ); 20 21 $application = new yii\web\Application($config); 22 $application->run(); Ÿ*áˆ;Å↵π(é, πautoloaderYii û∞ÑautoloaderÑzè⇥°, πÑ„/ÇU ( spa_autoload_register() eËåÍÒÑautoloaderÑÍÅYii Ñ„(b1ÔÂn›v ÔÂ⌃ÍÒÑautoloader“e0t*autoloder ÑMbŒ(ňH´⇤(⇥ •↵e↵↵Yii/ÇU⇤( spl_autoload_register() ËåautoloaderÑ ŸÅ↵ Yii.php ×Üõ¿H: 1 YII2_PATH . ’/base/Action.php’, 3 ’yii\base\ActionEvent’ => YII2_PATH . ’/base/ActionEvent.php’, 4 5 ... ... 6 7 ’yii\widgets\PjaxAsset’ => YII2_PATH . ’/widgets/PjaxAsset.php’, 8 ’yii\widgets\Spaceless’ => YII2_PATH . ’/widgets/Spaceless.php’, 9 ]; Ÿ* ⌅hÂ{:.ÂûE{áˆ: : ⇥ ⌥ > : L b ‡ s Ñ ⌧  ˝ î  ˙∞(ViewÃb⇥‡dView-,⇢˙∞BÑ$≠ÌÂ⇢˙∞BÑ–ó«↵⇥˘ éPHPÑWebî(·ëÓHTML/View-Ñ;ÅÖπ⇥Ÿ/séViewч*ü⇡⇢ 51 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 • #>:LbÂHTML:; • ,° BÑ$≠Ì–ó«↵Ô ÄUÑ™ØÌÂ<✏ÌÂ⇥‘ÇZ¢ñuÑ á‡⌫h1/Õ™Ø • Œ⇤(ModelÑôπ’⇥_1/ÙViewÍŒModel-∑÷pnŒ9ômodel@Â⌘Ï Ù÷Ï{¯Äe⇥ • ,° ˚U∆⌥pn⌅⌃ÑÖπÇ‚pnìI⇥Ÿõ,>(ControllerÃbvÂÿœÑ b✏ Ÿ∆˛⇥_1/Ù∆˛ÃbÅ(0Ñpn1/*ÿœ⇥ ˘éModel;Å1/›XãiÑ·ohÅãiÑL:å˘÷Ô€LÑÕ\⇥‘ ÇPost{≈6 *(é›XZ¢á‡⌥òÑtitle^'≈6 * dÑÕ\Ÿ˝/ModelÑ Öπ⇥Â↵/séModelч*ü⇡⇢ • pnL:π’/ModelÑ;ÅÖπ • ûEÂ\-Model/MVC-„œ';ëBÑ0π‡:séî(Ñ⇢°;ë_Å( ŸÃbh:⇥ • Ë✏Controller:⌃⇥Model/⌅⌃⇢°πbÑ;ëControllerÍ/ÄUÑO⇤ModelåViewK Ù Ñ s ˚ ⇥ Í Å / ⇢° s Ñ 1  > (Modelà b ⇥ } Ñ æ° î S / ÷Model &Controller⇥ ˘éController;Å/Õî(7˜B≥ö(¿H∆˛Å∆⌥¿Hpn(e>:⇥Â↵/ sControllerÑæ°ü⇡⇢ • (é⌅⌃(7˜B‡d˘éreqeustÑøÓ„îÂ>(ControllerÃb‘Ç $_GET $_POST I⇥F≈Pé∑÷(7˜Bpnî¢pn ˚UÕ\Ñ⌅⌃ŸîÂ>(modelsÃb⇥ • ⇤(modelsÑ{π’˘models€LôÕ\⇥ • ⇤(∆˛2”˝pb⇣˘(7reqeustÑresponse⇥ • ,Å HTML„Iv÷h∞BÑ⌧,Ÿ^éViewÑÖπ⇥ 4.1.2 Yii--- ÑÑÑ MVCÑÑÑ MMM ÔÔÔ MMM ŒMVCÑwêe≤/ŒLbî(Ñ—-—UweÑ⇥Œ,(e≤Ÿ/Õ„≥ÓòÑÔå û’⇥Œûıe≤Ÿ/ÕEœ⇤åÑ Hπ✏⇥F/Ç4⌘Ï≤ÑYiiÙ⇢Ñ/ßÕéÔ⇥‡ d⇧+Yii(ÖÑ∏⇢Web—F∂ÍÅ/(MVCÑ!✏1≈6ÅË✏Â↵Ÿ$π⇢ • YiiÑview° û’ÂS(7ÑÕ\Ç ⌥.ÿÑÕ\⇥˘éŸË˝Í/ù`MÔ Ä /JavaScriptI ⇥ ⇢ «JavaScriptU ∑ ( 7 Õ \ € L ¯ î G ⌃ ⇢ — fi Ô € L ⌅ ⌃ ÇajaxI Ù•(MÔ⌅⌃Ç(backbone.jsIMÔF∂ ⇥ • Yii1.1-° *ViewÑ{ViewáˆÍ/Controllerш8⇥° c✏I⌦Ñ∆˛⇥0ÜYii2 »é ÜView{eÏÀhÅ∆˛Œ;ë⌦ViewÚœ˙,ÏÀ˙eÜ⇥(∆˛áˆ-øÓ $this ⌥⌘Ñç/Yii1.1-SMcontroller/⌥⌘SMÑ∆˛⇥ 4.2 Yii--- ÑÑÑ °°° ööö MMM hhh ùùù VVV ËËË eee (TBD) :ÜMN„&↵¶–ÿyÓÑÔÙ§'Yii«(⇢∏⇢S↵AL»¯˘⇣üÑæ °!✏⇧ÏÜùVËeDenpdency Injection, DI å°öMhService Locator $Õ!✏⇥ s é ù V Ë e ° ö M h Inversion of Control Containers and the Dependency Injection pattern Ÿ˙ÜàÊ∆Ñ≤„ŸÃ”Webî(åYiiwSû∞ €L¢®Â†Òpaå⌃„⇥Ÿõæ°!✏˘é–ÿÍ´Ñæ°4sà .©Ÿ_/f`YiiÑ *ÕÅ˙—π⇥ 52 Chapter 4. Yii !!! ✏✏✏ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 4.2.1 sss ÇÇÇ ııı (Ü„Service Locator å Dependency Injection KM ≈ÅHeÜ„õÿ'⌦ÑÇı⇥+≈√ `ÍÅ *'ÙÜ„1OKÜÇúUeÙŸõ⌧ÔÂUÏô*v•J⇢ ùùù VVV ✓✓✓ nnn üüü ⇡⇡⇡ Dependence Inversion Principle, DIP DIP/Õoˆæ°Ñ⌥¸Û⇥ floˆæ° -⌦B„ùVé↵B„S↵B˙∞ÿ®ˆ ⌦B„_ůîÿÙ§⇣,Éÿ⇥ DIPÑ8√Û/⌦BöI•„↵Bû∞Ÿ*•„ Œó↵BùVé⌦BMN& ¶–ÿt*˚flÑ9'⇥Ÿ/Õœûı¡Ñ HVe⇥ ßßß 666 ÕÕÕ lll Inversion of Control, IoC IoC1 /DIPÑ Õ w S Ô DIPÍ / Õ ⌃ ı Û IoC/Õû∞DIPÑπ’⇥ IoCÑ8√/⌃{⌦B @ùVÑUC↵B Ñûã«↵ §1, πeû∞⇥ *ÄUÑyÅ1/{-˘@ùVÑUC ¯Ç $component = new yii\component\SomeClass ÑûãÌÂ⇥ ùùù VVV ËËË eee Dependence Inversion, DI DI/IoCÑÕæ°!✏/ÕWÔ gDIÑWÔ1Ô Âû∞IoC1˝&DIPü⇡⇥ DIÑ8√/ä{@ùVÑUCÑûã«↵>0{Ñbªû ∞⇥ • ßßß 666 ÕÕÕ lll πππ hhh IoC Container SyÓ‘É'ˆùVs˚Ô˝⇢àB⇥ IoC Container– õÜ®0˙ËeùVUC ⌅ùVs˚Iü˝œ⌘Ü∏⇢„œ⇥ Yii û∞Ü * yii\di\Container û∞Ü* DI Container⇥ • °°° ööö MMM hhh Service Locator Service Locator/IoCÑÊÕû∞π✏ v8√/ä@ Ô˝( 0ÑùVUC§1Service Locator€Lûãå˙Mn ä{˘ùVUCÑùVl b⇣{˘Service LocatorÑùV⇥ DI Service Locatorv≤Å$⇧Ô”û(⇥Yiiä ŸDIåService LocatorŸ$*⌧”we(⇥ 4.2.2 ùùù VVV ËËË eee ñH≤≤DI⇥(Webî(-à8¡Ñ/(⌅Õ, πWeb Serviceû∞yöÑü˝‘Ç—Æ ˆ®ÆZI⇥ GæÅû∞Sø¢(Z¢⌦—hƒ∫⌘ZáÑ\⇧—EmailÑü˝⇢8„ ⇢/Ÿ7: 1 // :ƈ°öIΩaB 2 interface EmailSenderInterface 3 { 4 public function send(...); 5 } 6 7 // öIGmailƈ° 8 class GmailSender implements EmailSenderInterface 9 { 10 ... 11 12 public function send(...) 13 { 14 ... 15 } 16 } 17 18 // öIƒ∫{ 19 class Comment extend yii\db\ActiveRecord 20 { 21 // (é(—ƈÑì 22 private $_eMailSender; 23 24 // Àˆûã $_eMailSender 25 public function init() 26 { 27 ... 28 // ŸÃGæ(GmailÑƈ° 4.2. Yii--- ÑÑÑ °°° ööö MMM hhh ùùù VVV ËËË eee (TBD) 53 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 29 $this->_eMailSender = GmailSender::getInstance(); 30 ... 31 } 32 33 // S ∞у˜ˆÊ— 34 public function afterInsert() 35 { 36 ... 37 // 38 $this->_eMailSender->send(...); 39 ... 40 } 41 } ⌦bÑ„Í/*:✏'Ù/ŸH*A↵⇥ £HŸÕ8¡Ñæ°π’ ¿HÓòb ;ÅÓò(é Comment ˘é GmailSender ÑùV˘ éEmailSenderInterfaceÑùVÔM Gæ )Å6(Gmail–õÑ°Ü9(YahooÍ ˙Ñƈ°Ü⇥ £H`óÓ9 Comment::init() Ãb˘ $_eMailSender ÑûãÌÂ: $this->_eMailSender = MyEmailSender::getInstance(); Ÿ*ÓòÑ,((é` )ôåŸ*CommentÍ˝(éŸ*yÓÍ)`—+ÑyÓÅ û∞{<Ñü˝ `ÿÅ˘à˘∞yÓ(Ñƈ°Ó9Ÿ*Comment⇥„Ñ('ÿ@⇥ ¿Hû’ÔÂ9ÿCommentÑ„1˝iU⇣˘⌅Õƈ°˝/H b›٠û’ ⌃CommentåGmailSender„&H û’–ÿCommentÑn⇥'('H ùVËe1/:Ü„≥Ÿ*ÓòÑS6DI_//„≥ÓòÑû’’flaa'Ô⇢W l⇥ Service Locator_/ÔÂû∞„&Ñ⇥ (Yii-(DI„& 2ÕËeπ✏⇢Ñ ˝pËe^'Ëe⇥ ÑÑÑ ˝˝˝ ppp ËËË eee Ñ ˝pËe⇢«:Ñ ˝pÑb¬:{ÖËÑΩaUC–õûã⇥ wSÑÑ ˝p⇤(„ 1Ë„≥ö⇥wSãPÇ↵: 1 // Ÿ/Ñ ˝pËeÑãP 2 class Comment extend yii\db\ActiveRecord 3 { 4 // (é(—ƈÑì 5 private $_eMailSender; 6 7 // Ñ ˝pËe 8 public function __construct($emailSender) 9 { 10 ... 11 $this->_eMailSender = $emailSender; 12 ... 13 } 14 15 // S ∞у˜ˆÊ— 16 public function afterInsert() 17 { 18 ... 19 // 20 $this->_eMailSender->send(...); 21 ... 22 } 23 } 24 25 // ûã$ÕÑƈ°S6÷Ï˝û∞ÜEmailSenderInterface 54 Chapter 4. Yii !!! ✏✏✏ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 26 sender1 = new GmailSender(); 27 sender2 = new MyEmailSender(); 28 29 // (Ñ ˝p⌃GmailSenderËe 30 $comment1 = new Comment(sender1); 31 // (Gmail—ƈ 32 $comment1.save(); 33 34 // (Ñ ˝p⌃MyEmailSenderËe 35 $comment2 = new Comment(sender2); 36 // (MyEmailSender—ƈ 37 $comment2.save(); ^^^ ''' ËËË eee Ñ ˝pËe{<⇢«setterpublic⇣Xÿœ⌃@ùVÑUCËe0{ÖË⇥ wSÑ^'ô e1Ë„≥ö⇥wSãPÇ↵: 1 // Ÿ/^'ËeÑãP 2 class Comment extend yii\db\ActiveRecord 3 { 4 // (é(—ƈÑì 5 private $_eMailSender; 6 7 public function setEmailSender($value) 8 { 9 $this->_eMailSender = $value; 10 } 11 12 // S ∞у˜ˆÊ— 13 public function afterInsert() 14 { 15 ... 16 // 17 $this->_eMailSender->send(...); 18 ... 19 } 20 } 21 22 // ûã$ÕÑƈ°S6÷Ï˝û∞ÜEmailSenderInterface 23 sender1 = new GmailSender(); 24 sender2 = new MyEmailSender(); 25 26 $comment1 = new Comment; 27 // (^'Ëe 28 $comment1->eMailSender = sender1; 29 // (Gmail—ƈ 30 $comment1.save(); 31 32 $comment2 = new Comment; 33 // (^'Ëe 34 $comment2->eMailSender = sender2; 35 // (MyEmailSender—ƈ 36 $comment2.save(); ⌦bÑCommentÇú⌃ private $_eMailSender 9⇣ public $eMailSender v d setter˝ p _/ÔÂæ07ÑHúÑ⇥ 4.2. Yii--- ÑÑÑ °°° ööö MMM hhh ùùù VVV ËËË eee (TBD) 55 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 4.2.3 DIπππ hhh Œ⌦bDI$ÕËeπ✏e↵ùVUCÑûã„/*ÕA⇣Ñ«↵⇥ ÔÂÛœ *Webî(Ñ–ƒˆ⇢ùVéÂrUCŸõUC» Ô˝ùVéÙNBßÑUC Œb⇣ùVL WÑ≈b⇥£HŸõùVUCÑûãËe«↵Ñ„Ô˝⇢‘ÉMs˚_ÅË✏⇥ Ÿû (/ˆ‚°Ä/+œ»⇤õ˙⇣úÑÂ\⇥ DIπh1/:Ü„≥Ÿæòæ°˙eÑ⇥ YiiÑDIπh/ yii\di\Container ÔÂû∞˘ ˘aÑûãåMnvÍ®ûãåMnÅ(0ÑùVUC⇥ ËËË ååå ùùù VVV Å(DIπhñHÅJ…πh{ {KÙÑùVs˚⇥ ( yii\di\Container::set() å yii\di\Container::setSinglton() ÔÂËåùV⇥ DIπh/H°⌃ùVÑbÅH↵↵ yii\di\Container::set() å yii\Container::setSinglton 1 public function set($class, $definition = [], array $params = []) 2 { 3 // ƒ⇤ $definition vôe $_definitions[$class] 4 $this->_definitions[$class] = $this->normalizeDefinition($class, $definition); 5 6 // ⌃Ñ ˝p¬pôe $_params[$class] 7 $this->_params[$class] = $params; 8 9 // d$_singletons[$class] 10 unset($this->_singletons[$class]); 11 return $this; 12 } 13 14 public function setSingleton($class, $definition = [], array $params = []) 15 { 16 // ƒ⇤ $definition vôe $_definitions[$class] 17 $this->_definitions[$class] = $this->normalizeDefinition($class, $definition); 18 19 // ⌃Ñ ˝p¬pôe $_params[$class] 20 $this->_params[$class] = $params; 21 22 // ⌃$_singleton[$class]n:nullh:ÿ*ûã 23 $this->_singletons[$class] = null; 24 return $this; 25 } Ÿ$*˝pü˝{<° *':+Í/ set() (é(œ!˜BˆÑ ∞Ñûã‘fi setSingleton() ÍÙ§*Uãœ!˜Bˆ˝‘fi˘a⇥ Œb¬e↵Ÿ$*˝pÑ $class •◊*{•„*+\:ùVÑ⇥ $definition h:ùVÑöIÔÂ/*{Mnpƒ*PHP callable⇥ Ÿ$*˝p,(⌦Í/⌃ùVÑ s·oôe0πhѯîpƒ-ª⇥(DIπh-Ù§Ü5* pƒ: 1 // (é›XUãSingleton˘a¢a{ã:. 2 private $_singletons = []; 3 // (é›XùVÑöI¢a{ã:. 4 private $_definitions = []; 5 // (é›XÑ ˝pѬp¢a{ã:. 6 private $_params = []; 7 // (é◆XReflectionClass˘aÂ{•„:. 8 private $_reflections = []; 9 // (é◆XùVÂ{•„:. 10 private $_dependencies = []; 56 Chapter 4. Yii !!! ✏✏✏ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 ( set() å setSingleton() -ñH⇤( yii\di\Container::normalizeDefinition() ˘ùVÑöIwƒ⇤⌅⌃v„Ç↵: 1 protected function normalizeDefinition($class, $definition) 2 { 3 // $definition /zÑ 4 if (empty($definition)) { 5 return [’class’ => $class]; 6 7 // $definition /W&2 8 } elseif (is_string($definition)) { 9 return [’class’ => $definition]; 10 11 // $definition /PHP callable ˘a 12 } elseif (is_callable($definition, true) || is_object($definition)) { 13 return $definition; 14 // $definition /pƒ 15 } elseif (is_array($definition)) { 16 if (!isset($definition[’class’])) { 17 if (strpos($class, ’\\’) !== false){ 18 $definition[’class’] = $class; 19 } else { 20 throw new InvalidConfigException("A class definition requires a \"class\" member."); 21 } 22 } 23 return $definition; 24 } else { 25 throw new InvalidConfigException("Unsupported definition type for \"$class\": " . gettype($definition)); 26 } 27 } ƒ⇤⌅⌃ÑA↵Ç↵⇢ • Çú $definition /zÑÙ•‘fipƒ [’class’ => $class] • Çú $definition /W&2£HŸ*W&21/@ùVÑ{•„ £HÙ•‘fipƒ [’class’ => $definition] • Çú $definition /*PHP callable/*˘a£HÙ•‘fi $definition • Çú $definition /*pƒ£HvîS/*⇧+ÜC $definition[’class’] ÑM npƒ⇥ ÇúÂpƒ*öI $definition[’class’] £H⌃ eÑ $class \:ÂC Ñ <‘fiÂpƒ⇥ • Çú definition[’class’] *öI $class /* HÑ{£Hõ˙⇥8⇥ • Çú $definition ^é⌦Ñ⌅Õ≈µ_õ˙⇥8⇥ (⇤( normalizeDefinition() €Lƒ⇤⌅⌃ set() å setSingleton()  eÑ $class :. ⌃öI›X€ $_definition[] -⌃ eÑ $param ›X€ $_params[] -⇥ ˘é set() ÿÅ d $_singleton[] -ÑùV⇥ ˘é setSingleton() ⇡ Å⌃ $_singleton[] -ÑùVæ: null ⇥ ŸH≤Ô˝}⌃„>‡*wSÑùVöI:ã†Ò⌃„: 1 $container = new \yii\di\Container; 2 3 // Ù•Â{Ëå*ùV}6ŸHZ°¿H✏IF⇢ó $_definition[’yii\db\Connetcion’] = ’yii\db\Connetcion’ 4 $container->set(’yii\db\Connection’); 5 6 // Ëå*•„S*{ùVé*•„ˆ⌥öÑ{⇢Í®´ûã 7 $container->set(’yii\mail\MailInterface’, ’yii\swiftmailer\Mailer’); 8 9 // Ëå*+S⇤($container->get(’foo’)ˆÔÂó0* yii\db\Connection ûã 10 $container->set(’foo’, ’yii\db\Connection’); 4.2. Yii--- ÑÑÑ °°° ööö MMM hhh ùùù VVV ËËË eee (TBD) 57 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 11 12 // (*MnpƒeËå*{ÅŸ*{Ñû㈟*Mnpƒ⇢—\( 13 $container->set(’yii\db\Connection’,[ 14 ’dsn’ => ’mysql:host=127.0.0.1;dbname=demo’, 15 ’username’ => ’root’, 16 ’password’ => ’’, 17 ’charset’ => ’utf8’, 18 ]); 19 20 // (*MnpƒeËå*+1é+Ñ{ãʇdMnpƒ-Å class C 21 $container->set(’db’,[ 22 ’class’ => ’yii\db\Connection’, 23 ’dsn’ => ’mysql:host=127.0.0.1;dbname=demo’, 24 ’username’ => ’root’, 25 ’password’ => ’’, 26 ’charset’ => ’utf8’, 27 ]); 28 29 // (*PHP callableeËå*+œ!(Ÿ*+ˆŸ*callable˝⇢´⇤(⇥ 30 $container->set(’db’, function ($container, $params, $config){ 31 return new \yii\db\Connection($config); 32 }); 33 34 // (*˘aeËå*+œ!(Ÿ*+ˆŸ*˘a˝⇢´(⇥ 35 $container->set(’pageCache’, new FileCache); Œ set() å setSingleton() e↵Ô˝ÿπ◆⌃„DIπh↵b猄êùVÑ“¶≤ ≤⇥ ùùù VVV ÑÑÑ „„„ êêê Ë å ù V ˆ  ( set() å setSingleton() ˘ î „ ê ù V  ( yii\di\Container::get() v„Ç↵: 1 public function get($class, $params = [], $config = []) 2 { 3 // Úœ *å⇣ûãÑUãÙ•(Ÿ*Uã 4 if (isset($this->_singletons[$class])) { 5 return $this->_singletons[$class]; 6 // /*⇢*Ëå«ÑùV9n eѬp˙*ûã 7 } elseif (!isset($this->_definitions[$class])) { 8 return $this->build($class, $params, $config); 9 } 10 11 $definition = $this->_definitions[$class]; 12 13 if (is_callable($definition, true)) { 14 $params = $this->resolveDependencies($this->mergeParams($class, $params)); 15 $object = call_user_func($definition, $this, $params, $config); 16 } elseif (is_array($definition)) { 17 $concrete = $definition[’class’]; 18 unset($definition[’class’]); 19 20 $config = array_merge($definition, $config); 21 $params = $this->mergeParams($class, $params); 22 23 if ($concrete === $class){ 24 $object = $this->build($class, $params, $config); 25 } else { 26 $object = $this->get($concrete, $params, $config); 27 } 28 } elseif (is_object($definition)) { 58 Chapter 4. Yii !!! ✏✏✏ ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 29 return $this->_singletons[$class] = $definition; 30 } else { 31 throw new InvalidConfigException("Unexpected object definition type: " . gettype($definition)); 32 } 33 34 if (array_key_exists($class, $this->_singletons)) { 35 $this->_singletons[$class] = $object; 36 } 37 38 return $object; 39 } 4.2.4 °°° ööö MMM hhh Service Locator_/Õæ°!✏ûE- õ↵À§:Ÿ/ÕÕ!✏⇥F⌘Ѭπ/(ó SŸ_/Õ}!✏⇥ ŸÃw‡s;òÑâ∫Ü⇥ eService LocatorÓÑ(é„&⇥ ∏⇢⇣üÑæ°!✏_/(é„&F(Webî(⌦ Service Locator` -K0⇥Ÿ!✏Ñ⇠π ⇢ • Service LocatorESÜ*–LˆÑ˛•hÑ“rÔÂ(–Lˆ®0Ó9*{@Å (Ñ ° ≈˘{\˚UÑÓ9⇥ • *{ÔÂ(–Lˆ à˘'0ûœˇb@Å(0Ñ°Œó0ö↵¶Ñ⇠⇥ • û∞°–õπ°(πåhÑ„&øéÏÀK’å„ËF∂(⇥ 4.2. Yii--- ÑÑÑ °°° ööö MMM hhh ùùù VVV ËËË eee (TBD) 59 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 60 Chapter 4. Yii !!! ✏✏✏ CHAPTER FIVE âââ ≈≈≈ YII ˜Y✏YiiÅPHP5.4.0Â⌦H,⇥ $Õπ’ÔÂâ≈YiiÕ/(ComposerÊÕ/Ù•↵}ã)⇧⇥ 5.1  ((( Composerâââ ≈≈≈ Yii ®P(Composerâ≈Yii⇥Ÿ7ÙπøÙ§ÇúÅ˚†∞ÑiU⇧GßYiiÍÅ }‰1OKÜ⇥ («Yii1.1Ñ˚⇧Ô˝ÿ paâ≈YiiåÑ˙î(/$*e§⇢â≈Yii–LYii-˙î(F∂⇥ F/Çú(Composerπ’â≈Yiiâ≈åÑ˙î(/eå⇣Ü⇥ ,Y↵vSóÀÕPHPåComposerÑπ’ŸπbÑÖπ⇢«òπác⇧⌧"Œ˝ÔÂ~ 0⇥s/(Composerâ≈Yii_ $Õ È⇢(˙,!H⇧ÿß!H⇥Ÿ$⇧;ÅÑ:+( éÿß!H–õÜØÉ⌥båM⌃ª⇥1éYiiBlog/ÅM⌃ªÑ‡d⌘Ï(ÿß!H e˙î(⇢ php composer.phar create-project --prefer-dist --stability=dev yiisoft/yii2-app-advanced /path/to/yii-application Çú(˙,!H_/ÔÂÑ⇥Í«ÅKÂ˚†MÚ⇥ŸÃ1HÀÕÜ⇥TODO bчÇ⇢≤0⇥(˙,!H_/ÂÌÂö composer create-project --prefer-dist yiisoft/yii2-app-basic /path/to/yii-application ÇúÛ(∞Ñ—H,ÑYii_Ôœ(ÿß!H£7†e --stability=dev Ѭp⇥ 5.2 ŒŒŒ ããã ))) ⇧⇧⇧ âââ ≈≈≈ Çú(ã)⇧â≈π✏˜ Â↵e§⇢ 1. Œyiiframework.com↵}∞Ñã)⇧⇥ 2. ⌃ã)⇧„ã)0˚✏Ñ /path/to/yii-application ÓU⇥ 3. Ó9 config/web.php áˆìe cookieValidationKey Mny∆•⇥Çú(Composerâ ≈⇡Composer⇢Í®æn*∆•: // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation ’cookieValidationKey’ => ’enter your secret key here’, 5.3 æææ nnn Web °°° hhh 8(ÑWeb°h nginx↵php-fpmåApache⇥Œãø⌦↵M⇧Ñ‘Õc≠–ÿ⇥ 61 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 ,Y↵SóÀÕnginxåApacheÑâ≈ŸõŒòπácå⌧"Œ˝ÔÂ~0¯sÖπ⇥ŸÃ ÍÀÕÇUMnWeb°hv˝©Yii—we⇥1éYiiBlogw Må⇥,e≤Må ⌃ªÔÂ(Ñ;:Ô„⇧(ÑÔÑ⇥(Ñ;:ÑÇ: http://frontend.example.com/ http://backend.example.com/ (ÑÔÑÑÇ: http://www.example.com/frontend/ http://www.example.com/backend/ ÓÑ˝/⌃ªMå⇥˘éYiiBlog1é/(,0—⌘Ï(ÑÔ„e⌃ªMå ⇥S∞(°h⌦;:Ô„Ñ⌃ªπ✏✏s@ÑZfl;:⇢Û/Ñi⌃° h⇥ÑÔÑÑ⇡h∞:;:ÓU⇥ŸÃ⌘Ï(ÑÔ„e:+M F(i⌃⌦MÔåÔb˝Ër(°h⌦_1/(Zfl;:⇥ (NginxÑMnÇ↵⇢ # for frontend server { charset utf-8; client_max_body_size 128M; listen 80; ## listen for ipv4 #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 server_name localhost; root /path/to/application/frontend/web; index index.php; access_log /path/to/application/frontend/log/access.log main; error_log /path/to/application/frontend/log/error.log; location / { # Redirect everything that isn’t a real file to index.php try_files $uri $uri/ /index.php?$args; } # uncomment to avoid processing of calls to non-existing static files by Yii #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { # try_files $uri =404; #} #error_page 404 /404.html; location ~ \.php$ { include fastcgi.conf; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/var/run/php5-fpm.sock; } location ~ /\.(ht|svn|git) { deny all; } } # for backend server { # ...other settings... listen 81; server_name localhost; root /path/to/application/backend/web; index php; # ...other settings... 62 Chapter 5. âââ ≈≈≈ Yii ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 } Çú(Apache_/⌃MåMÑMnÂéM⇢ # Set document root to be "path/to/application/frontend/web" DocumentRoot "path/to/application/frontend/web" RewriteEngine on # If a directory or a file exists, use the request directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward the request to index.php RewriteRule . index.php # ...other settings... ˘é_/æn*Zfl:ÔÑ9: path/to/application/backend/web sÔ⇥ 5.4 Yii--- ÑÑÑ MMM  YiiŒe§ó¿H/M¿H/⇥Œ,(e≤Må˝/î(⇥b›Ù`Ô H(˙,!—˙*î(w MÑhËü˝6Ër⇥Ç’Æ6*w hËü˝Ñî (⇥ Ÿ(ü⌃⌦/77Ñ⇥Í/ g⌘ÏÑœåMåŒ;ë⌦*≤ƒ⇣î(‘É& ⌘ÏѧÂ⇥Œ„(Ñ“¶e≤⌘ÏÙMåÑ„ÔÂí⇢í(=œÅ Õ nP⇥F/˘éYiie≤M1/*å⌥Ñî(»/Ê*î(⇥Ÿπ:+˜˚⇧↵ ÀÏY✏⇥ £HYiiÑÿß!H/ÇUû∞ä$*î(t⇣*⌘ϧ∆⌦Ñî(Ñb⇥⌘Ϭfl↵ /path/to/application/ ÓU↵↵Ÿ*ÿß!H/ÇUƒ«„Ñ⇥æ—∞v- å*ÓU frontend backend ⌃+„hÜM⇥ vû`ä frontend backend -Ñ˚✏1*ÓU d/qÕi↵ÑÓUÑc8–lÑ⇥_1/ Ù÷ϯíÙ/ÏÀÑ⇥Í«„ƒ«↵÷Ï˝>(Ü /path/to/application/ ÓU↵b⇥ ÇúÒe↵ª`⇢—∞I frontend backend vû console _/*å⌥ÑYiiî(É1 ⇢8/î(ÑÙ§/*}‰L⇥ ;ÑÙYiiÑM¿HÑ/⌘Ï}ÑÇı÷Ï˝/ÏÀå⌥Ñî(⇥÷Ï»˝w öÑT˚ŸõT˚Å˙S∞(Ü common ÓU⌦⇥Ÿ*ÓUÑ✏/⇢(˘éƒ«0wÑ frontend backend console v-ÑÖπ÷Ï˝ÔÂ(⇥Ÿ/Yii-û∞„(ÑÄÁ@(⇥ 5.5 MMM nnn îîî ((( ØØØ ÉÉÉ ÿÓe1å⇣YiiÑâ≈Ü⇥Ÿe1/ænî(ØÉ⇥˘éYiiÑÿß!HeÜ ØÉÑÇı⇥:¿HÅeØÉÑÇıb ©⌘ÏGæŸÕ^8xãÑ≈b⇢`Ï/*—‚⇣X Alice, Bob, CharlieIà⇢∫ ÷Ï(ÍÒÑ°ó:⌦€L—⇥v-Alice(LinuxÕ\˚flBob(Mac OSCharlie (Windowsœ*∫(ÑIDE_=¯⇥Ÿ˝°ÓòPHPÔÂ(s⌦—óà}⇥F/ ÓòeÜœ*⇣X(—ш-˙ÜÍÒÑK’ØÉœ*∫(éK’Ñpnì(7 ∆˝/9nÍÒÑú}}Ñ⇥é/œ!‚⇣XŒ‚”ì-∑÷∞„,ˆã˝/ÅÙ 9:,0ØÉÑpnìå∆⇥(–§„M»Å⌃v9fiª⇥ é/`ÏÛ0*„≥û’⌃ØÉMnáˆíd(„ìK⇥Ÿ7œ!pullåpush„ˆ˝ (Ó9ØÉÜ⇥Ÿnû„≥Ü‹↵”ìÔå,0ÔØÉÑÓò⇥FóÙ`ÏÑ„ì/å 5.4. Yii--- ÑÑÑ MMM  63 ÒÒÒ eee ⌃⌃⌃ „„„ Yii2.0, Release 1 tÑÃb:⌘*ØÉMnáˆ⇥Ù:Å}Ñã–)`ÏÑ‚œ«®∫§:Å(Mnáˆ- †e∞ÑMnyé/`Ïó⇢Â@ ⇣XÍÒK®Ù∞⇥‡:`Ï*˝⌃ÂMnáˆ≥eH, °⌃⇥ ÊÕ≈µ/⇢`ÏÑ‚/*ÿHÑÎÌ„Ñ‚⇥`Ïœ)˝⇢˘„€LÙ∞⇥1é —Ôåß¡ÔØÉØö/Ñ⇥é/œ!Ù∞ˆ`˝Å⌃MnáˆÕ∞ g°h€LMn⇥ ·ëÓŸõefieª–LØÉÑ⌥böÊ✏`Ü⇥é/4√ÑYiieÜØÉÑÇıe„≥ Ÿ*Óò⇥vûYii1.1-ÿ° Ÿ*y'Ÿ/∞H,-MeÑÄ/⇥=°(ûı-÷Úœà⇣ üÜ⇥ @◆ØÉ1/ƒ–LØɯsÑMnáˆ⇥Yii˘éØÉÑ(/Ÿ7*ü⌃⇢«(*Í ®Ñ⇢,œ!–L⇢,ˆnöÅ«(UÕØÉ6⌃˘îØÉÑ@ Mnáˆ˝Ü÷SMÑM náˆ⇥˘éYiiØɯsÑáˆvû1Í $*⇢*/e„⇢, index.php Ê*1/⌅ {Mnáˆ⇥Yiihh0Û0Ü( .gitignore áˆe„≥Mnሀe€eH,ìÑÓò⇥ ‡d(⌥bØɈÍÅL}‰1hËö: php /path/to/yii-application/init ŸL}‰⇢–:` ÈUÕ—ØÉvn§/&Ü÷SMÑMnáˆ⇥ÇúÛÙ†ÿHÔÂÙ •⌥ö¯sѬp: php /path/to/yii-application/init --env=Production overwrite=All ,åÕπ✏Ù•(}‰L-⌥(ÑØÉvÅÅhËÜ÷SMMnáˆ⇥ 5.6 ØØØ ÉÉÉ ÑÑÑ MMM nnn üüü ⇡⇡⇡ ñHÜ„Yii⌅ØÉáˆ⇥Mb⌘Ï≤0œ*YiiØÉ1/ƒMnáˆ⇧+Ü index.php å ⌅{Mnáˆ⇥vû÷Ï1>( /path/to/yii-application/enviroments ÓU↵b⌘Ï↵↵ Ÿ*ÓU˝ Íõ⌧⇢ • ሠindex.php • ÓU dev • ÓU prod v- index.php öIÜÔÂ(ÑØÉSŸ*áˆ↵<(Òeª⌃„_ÔÂ'Ù ⌧0ÉöIÜ Development Production $*ØÉjÇ`Øö(⇢˝˝⌧ó˙e⇥ÓU dev å prod ˝⇧+Ü7Ñ”Ñ⇢ • ሠyii • ÓU common • ÓU frontend • ÓU backend • ÓU console Ÿ‡*ÓUÑ /path/to/yii-application/ /77Ñ⇥ç↵↵ frontend backend ÑÖπÃbÖπ/7Ñ⇢ config/main-local.php config/params-local.php web/index.php ⇥‡d*ØÉ1/ƒ index.php e„áˆå *-local.php Mnáˆ⇥Ë ✏ common å console -v° web ÓU_1/° e„⇢,⇥Ÿ/‡: common /*å⌥Ñ î(÷Í/–õÜ⇢(ÑMn° ÙM⇢(*e„⇢,Ñ£{ï⇣Ü*î(Ü console /}‰L/webî(‡d° webe„⇢,ÙF/⌦b–0Ñ yii áˆ1/ }‰LÑe„áˆ÷X(éÓU dev å prod -_1/ÙÑØɢîÜÑ}‰Le„⇢ ,⇥ ÙÜŸH⇢∞(2we↵⇥–L init ⇢,1⇢⌃–ØÉÑ˚⌫áˆ60SMÑá ˆ-Ÿõáˆ1/ index.php e„áˆå *-local.php Mnáˆ⇥60Íb60Ü 64 Chapter 5. âââ ≈≈≈ Yii /path/to/yii-application/ ↵bÑ frontend backend console common -˘îÑ config ÓUå index.php ⇥ ŸŸŸ H \⇢Ü ÷ S M Ñ M n ⇥ £ H S M M n / ⌥ Í õ á ˆ b 1 / /path/to/yii-application/ ↵bÑ frontend backend console common -˘îÑ config Ó U-Ñ@ *.php å index.php ⇥ *.php á ˆ Í õ b h : Ô Ñ + Ñ aliases.php h : ; M n Ñ main.php main-local.php h:h@¬pÑ params.php params-local.php ⇥ v-@ Ñ *-local.php ˝eÍé` (ÑØÉh:,0MnÑ✏⇥÷Ï⇢´ôe0„ ”ì-⇥S6ŸõØÉ_1/t* /path/to/yii-application/enviroments ÓU˝⇢´ ôe„”ì⇥ @ & *-local.php ÑmainåparamsMnáˆ˝/ØÉÑÖπ⇥F(»Ñ–LØ É-÷Ï/w\(Ñ⇥‘Ǣ闂Ñ@ ⇣Xî(ÑID/⇢ÿÑœ,Y↵ Ñ YiiBlog Ÿ*ÔÂô( main.php áˆ-⇥F/œ*⇣XÑK’ØÉ/ÑÇpnì (7∆IŸ*ÅôeÍÒÑ /path/to/yii-application/enviroments ÓU-Ñ main-local.php áˆ⇥Ÿ7S–§„ˆ*∫ØÉÑMnv⇢ôe‚Ñ„ì-⇥…÷ „ˆÍÅ–LL init }‰1ÔÂå⇣*∫ØÉÑ⌥b⇥àÄU/H ⌦b≤0ÑMnሠà⇢ M}‰LåCommonÑ &localÑ&localÑ aliases, params, mainI↵we}BÑ7P⇥£H*ØÉ—\(ˆŸõáˆ/H*zè b ñHMå}‰LÑMnáˆÙírp⌅°⌅Ñ⇥° HzèÙ⇥‡:Yii(˚ ✏ˆÙÅH/(—MÅH/(—∞óH÷Ï/Ñî(÷Ï/ÏÀÑ⇥F/Íà *Common⇢(⇥£Hzè/Ÿ7ÑcommonÑÖπ´vYÑÜ÷⇥ v!localå&localÑ⇥>ÑlocalÑ/,0Mnáˆ&localÑ/‚Ù⇢(ÑMn⇥‡ dlocalÑÜ÷&localÑ⇥ aliases, params, main⇥Ÿ {áˆh:ÑMnÖπvÕ‡÷Ï;ë⌦X(Ü÷ ÑÓò⇥Çú↵↵ê„Ô—∞aliases/(PHPÑ require ô€ªÑ˘éh@w\(⇥v !paramsÍ/mainMnÑË⌃⇥mainÑÖπ/\:¬p ✓Ÿî(ÑÑ ˝p⇥Ÿ ⇧Œl[ ¯ X(Ü÷ÑÓò⇥ ✏”↵Yiiî(MnáˆHÑzè/⇢ • M}‰LíqÕ • M}‰LÜ÷common • aliasesåmainíqÕ • params/mainÑË˝ • &localÑÜ÷&localÑ⇥ 5.7 ¿¿¿ ååå âââ ≈≈≈ ≈≈≈ µµµ Mb`Úœå⇣ÜYiiÑâ≈Ô˝(ÜComposer_Ô˝(Üã)⇧⇥•@`Mn} ÜWeb°h⇥`–LÜ init }‰⇥£HYiiBlogÑF∂Úœ-˙}Ü⇥`Ô(`ÑO»h- ’’Hú⇥
还剩68页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享pdf获得金币 ] 0 人已下载

下载pdf

pdf贡献者

50480350

贡献于2014-11-28

下载需要 8 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf