{"id":902,"date":"2016-08-15T13:42:28","date_gmt":"2016-08-15T05:42:28","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=902"},"modified":"2016-08-15T13:42:28","modified_gmt":"2016-08-15T05:42:28","slug":"yii%e6%89%a7%e8%a1%8c%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=902","title":{"rendered":"yii\u6267\u884c\u539f\u7406"},"content":{"rendered":"<p>\u5e94\u7528\u6267\u884c\u6d41\u7a0b\uff1a<\/p>\n<pre>\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u53d1\u9001 Http Request\r\n    |\r\n  \u63a7\u5236\u5668(protected\/controllers)\r\n    |\r\n    |---&gt; Action\r\n            |\r\n         \u521b\u5efa\u6a21\u578b (Model)\r\n            |\r\n         \u68c0\u67e5$_POST\u8f93\u5165\r\n            |\r\n         \u6e32\u67d3\u89c6\u56fe\r\n            |\r\n         render()\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f5c\u4e3a\u63a7\u5236\u5668\u4e0e\u89c6\u56fe\u63a5\u53e3\u53c2\u6570\r\n            |\r\n            |----&gt; View (protected\/views)\r\n                     |\r\n                   \u4f7f\u7528$this\u8bbf\u95ee\u63a7\u5236\u5668\u7684\u53d8\u91cf\uff08\u5305\u62eclayout, widget)\r\n\r\n\r\n\r\n-----------------------------------------------------------------\r\n\r\n<\/pre>\n<p>\u89c6\u56fe\u6e32\u67d3\u6d41\u7a0b\uff1a<\/p>\n<pre>render($view, $data, $return)\r\n    |\r\nbeforeRender()\r\n    |\r\n\u6e32\u67d3View\u6587\u4ef6\uff0c\u8c03\u7528renderPartial()\uff0c\u8981\u6c42\u5904\u7406\u8f93\u51fa\u7ed3\u679c\r\n    |\r\n    |----&gt; \u6839\u636e$view\u5f97\u5230viewFile\u6587\u4ef6\u540d\r\n                    |\r\n           renderFile()\uff0c\u8981\u6c42\u8fd4\u56de\u6e32\u67d3\u7ed3\u679c\uff0c\u505a\u4e0b\u4e00\u6b65\u5904\u7406\r\n                    |\r\n                    |-----------&gt; \u83b7\u53d6widget\u7684\u6570\u76ee\r\n                                         |\r\n                                  \u4eceYii::app()\u83b7\u5f97render\r\n                                  CWebApplication::getViewRenderer\r\n                                  \u67e5\u8be2component['viewRenderer']\uff0c\u9ed8\u8ba4\u6ca1\u6709\u914d\u7f6e\r\n                                         |\r\n                                  Then, \u8c03\u7528renderInternal()\r\n                                         |\r\n                                         |---------&gt; require View\u6587\u4ef6\uff0c\u6e32\u67d3\uff0c\u6839\u636e\u9700\u8981\u8fd4\u56de\u6e32\u67d3\u7ed3\u679c\r\n                                                          |\r\n                                         |&lt;---------------|\r\n                                         |\r\n                    |&lt;-------------------|\r\n                    |\r\n               \u5904\u7406\u8f93\u51fa\u7ed3\u679cprocessOutput()\r\n                    |\r\n               \u6309\u7167caller\u53c2\u6570\uff0c\u8fd4\u56de\u8f93\u51fa\uff0c\u800c\u4e0d\u662fecho\u8f93\u51fa\r\n    |&lt;--------------|\r\n    |\r\n\u6e32\u67d3layout\u6587\u4ef6\r\n    |\r\n\r\n----------------------------------------------------------------------<\/pre>\n<p>\u52a0\u8f7d\u63a7\u5236\u5668\u53ca\u5176\u65b9\u6cd5\uff1a<\/p>\n<pre>\u6839\u636eroute\u4fe1\u606f\uff0c\u83b7\u5f97\u5f53\u524d\u63a7\u5236\u5668\r\n      |\r\n\u521d\u59cb\u5316\u5f53\u524d\u63a7\u5236\u5668\uff0cCController::init()\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\r\n      |\r\n\u6267\u884c\u5f53\u524d\u63a7\u5236\u5668\uff0cCController::run()\r\n      |\r\n      |----&gt; \u521b\u5efaaction\uff0c\u4e3a\u7a7a\u5219\u9ed8\u8ba4\u4e3aindex\r\n                     |\r\n             \u5f97\u5230CInlineAction\u7684\u5b9e\u4f8b\r\n                     |\r\n             \u7528\u7236\u5bf9\u8c61\u6267\u884cbeforeControllerAction\uff1a\u9ed8\u8ba4\u662fCWebApplication\uff0c\u76f4\u63a5\u8fd4\u56deTRUE\r\n                     |\r\n                \u6267\u884caction\r\n                     |----&gt; \u5907\u4efd\u539f\u6765\u7684action\r\n                                 |\r\n                            \u6267\u884cbeforeAction()\r\n                                 |\r\n                            runWithParams()----&gt; \u5b9e\u9645\u4e0a\u662f\u6267\u884cCInlineAction-&gt;runWithParams()\r\n                                                             |\r\n                                                 \u5728\u5b9e\u4f8b\u4e2d\uff0c\u6267\u884cSiteController-&gt;actionIndex()\r\n                                                             |\r\n                                                 \u6e32\u67d3\u9875\u9762\uff1arender('index')\r\n                                                             |\r\n                                 |&lt;--------------------------|\r\n                                 |\r\n                            \u6267\u884cafterAction()\r\n                                 |\r\n                            \u6062\u590d\u539f\u6765action\r\n                                 |\r\n                     |&lt;----------|\r\n                     |\r\n             \u7528\u7236\u5bf9\u8c61\u6267\u884cafterControllerAction\uff1a\u9ed8\u8ba4\u662fCWebApplication\uff0c\u4e3a\u7a7a\r\n       |&lt;------------|\r\n     \u5b8c\u6210\r\n\r\n\r\n----------------------------------------------------------------\r\n\r\n\r\n<\/pre>\n<p>\u5e94\u7528\u6267\u884c\u6d41\u7a0b\uff1a<\/p>\n<pre>index.php\r\n    |\r\nrequire_once($yii)\r\n    |\r\n    |-------------&gt;yii.php\r\n                    |\r\n                  require(YiiBase.php)\r\n                    |\r\n                    |----------------&gt;YiiBase.php\r\n                                        |\r\n                                      Define YII_XXX global variable\r\n                                        |\r\n                                      Define Class YiiBase\r\n                                        |\r\n                                      Autload Class YiiBase \uff08\u81ea\u52a8\u52a0\u8f7d\u7c7b\u673a\u5236\uff09\r\n                                        |\r\n                                      require interface.php\r\n                                        |\r\n                    |&lt;------------------|\r\n                    |\r\n                   define null Class Yii\r\n                    |\r\n    |&lt;--------------|\r\n    |\r\ncreateWebApplication($config)----------&gt;|\r\n                                        |\r\n                                      YiiBase::createApplication('CWebApplication',$config)\r\n                                        |\r\n                                      Create Instance of CWebApplication\r\n                                        |\r\n                                        |---------&gt;CWebApplication\r\n                                                      |\r\n                                                   CApplication($config)\u6784\u9020\u51fd\u6570\r\n                                                      |\r\n                                                      |------&gt;|\r\n                                                           setBasePath\r\n                                                              |\r\n                                                           set path alias\r\n                                                              | \r\n                                                           preinit() \u7a7a\u65b9\u6cd5\r\n                                                              | \r\n                                                           initSystemHandlers()\r\n                                                              | \r\n                                                           configure($config) \u5c06\u914d\u7f6e\u6587\u4ef6\u4fe1\u606f\u4fdd\u5b58\u5230Application\r\n                                                              |\r\n                                                           attachBehaviors()\r\n                                                              | \r\n                                                           preloadComponents() --&gt; \u88c5\u8f7d\u5728configure($config)\u4e2d\u914d\u7f6e\u9700\u8981preload\u7684components\r\n                                                              | \r\n                                                           init()                                                              | \r\n                                                      |&lt;------|\r\n                                                      |\r\n                                        |&lt;------------|\r\n                                        |\r\n    |&lt;----------------------------------|\r\n    |\r\napp-&gt;run()\r\n    |\r\n    |----&gt;CWebApplication::processRequest()\r\n                      |\r\n                      |----&gt; CWebApplication::runController($route)\r\n                                       |\r\n                                       |----&gt;createController($route)\r\n                                                    |\r\n                                                 \u5982\u679c$route\u662f\u7a7a\uff0c\u6dfb\u52a0\u9ed8\u8ba4controller\uff0c\u5bf9\u4e8eCWebApplication\u7684controller\u662f\"site\"\r\n                                                    |\r\n                                                 Controller\u7c7b\u662fSiteController\uff0crequire\u8be5\u7c7b\u6587\u4ef6\r\n                                                    |\r\n                                                 \u5982\u679c\u8be5\u7c7b\u662fCController\u7684\u5b50\u7c7b\uff0c\u4fee\u6539id[0]\u4e3a\u5927\u5199\uff0c\u521b\u5efa\u8be5\u7c7b\u7684\u5b9e\u4f8b\r\n                                                    |\r\n                                                    |----&gt;CSiteController\r\n                                                              |\r\n                                                          extends from Controller \u8fd9\u662f\u5ba2\u6237\u5316\u63a7\u5236\u5668\u7684\u57fa\u672c\u7c7b\uff0c\u5b58\u5728\u4e8ecomponents\u4e0b\r\n                                                          \u5b9a\u4e49\u4e86\u9875\u9762\u7684\u901a\u7528\u5e03\u5c40\r\n                                                              |\r\n                                                          \u4f7f\u7528CController\u6784\u9020\u51fd\u6570\u521b\u5efa\u5bf9\u8c61CSiteController\uff0c\u5177\u4f53\u521d\u59cb\u5316\u6570\u636e\u89c1yii-1.png\r\n                                                              |\r\n                                                    |&lt;--------|\r\n                                                \u5907\u4efd$this-&gt;_controller\r\n                                                $this-&gt;_controller = $controller\r\n                                                    |\r\n                                                 \u8c03\u7528\u63a7\u5236\u5668\u7c7b\u7684init()\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\r\n                                                    |\r\n                                                 \u8c03\u7528\u63a7\u5236\u5668\u7c7b\u7684run()\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4e3aCController\u7684run()\r\n                                                    |\r\n                                                    |----&gt;createAction()\r\n                                                              |\r\n                                                           if($actionID==='') $actionID\u8bbe\u7f6e\u4e3a$this-&gt;default (\"index\")\r\n                                                              |\r\n                                                              |Yes\r\n                                                              |----&gt; return CInlineAction($this, $actionID)\r\n                                                              |No              |\r\n                                                           \u4eceMap\u521b\u5efa           |\r\n                                                              |      \u6267\u884c\u5f53\u524d\u7c7bCInlineAction\u7684\u7236\u7c7bCAction\u7684\u6784\u9020\u51fd\u6570\r\n                                                              |      \u8bbe\u7f6e_controller\u548c$id\r\n                                                              |                |\r\n                                                              |&lt;---------------|\r\n                                                              |\r\n                                                              |\r\n                                                           \u8fd9\u91cc\u5f97\u5230\u4e00\u4e2aCAction\u7684\u5b9e\u4f8b\r\n                                                              |\r\n                                                           $this-&gt;getModule()\u4f5c\u4e3aparent\uff0c\u4e3a\u7a7a\u5219\u4f7f\u7528Yii::ap\r\n                                                              |\r\n                                                           $parent-&gt;beforeControllerAction() ??\r\n                                                              | \r\n                                                            $this-&gt;runActionWithFilters($action,$this-&gt;filters());\r\n                                                              | \r\n                                                            $parent-&gt;afterControllerAction($this,$action);\r\n                                                    |&lt;--------|\r\n                                                    |\r\n                                                  \u6062\u590d\u539f\u6765\u7684$oldController\r\n                                       |&lt;-----------| \r\n                                       |\r\n                       |&lt;--------------|\r\n                       |\r\n                  End of processRequest()\r\n                       |\r\n    |&lt;-----------------|\r\n    |\r\nEnd of app-&gt;run()\r\n\r\n------------------------------------------------------------\r\n\r\n\r\n<\/pre>\n<p>\u83b7\u53d6\u63a7\u5236\u5668\u548c\u65b9\u6cd5\u7684ID<\/p>\n<p>\u8f6c\u8f7d\uff1ahttp:\/\/code.dimilow.com\/how-to-get-current-controller-name-and-action-name-in-yii\/<\/p>\n<h2>\nHow to get current controller name and action name in Yii<\/h2>\n<p>By Dimi Low on July 7th, 2010 (6 responses)<\/p>\n<p>To get current controller name\/id inside your controller, or view<br \/>\n$controllerId = Yii::app()-&gt;controller-&gt;id;<br \/>\n\/\/or<br \/>\n$controllerId = $this-&gt;getId();<\/p>\n<p>To get current action name\/id being executed, if you are inside beforeAction() or afterAction(), use the received CAction argument<br \/>\n\/\/inside beforeAction or afterAction<br \/>\npublic function beforeAction($action)<br \/>\n{<br \/>\n$actionId = $action-&gt;id;<br \/>\n&#8230;<br \/>\nor just elsewhere inside your controller<br \/>\n$actionId = $this-&gt;getAction()-&gt;getId();<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528YiiMailMessage\u53d1\u9001\u90ae\u4ef6\uff1a<\/p>\n<p>\u5b89\u88c5yii-mail\u5230protected\/extension\u4e0b<\/p>\n<p>\u914d\u7f6eprotected\/config\/main.php\uff0c\u5982\u4e0b<br \/>\n&#8216;import&#8217; =&gt; array(<br \/>\n&#8230;&#8230;<br \/>\n&#8216;application.extensions.yii-mail.*&#8217;,<br \/>\n),<\/p>\n<p>&#8230;&#8230;<br \/>\n&#8216;components&#8217; =&gt; array(<br \/>\n&#8216;mail&#8217; =&gt; array(<br \/>\n&#8216;class&#8217; =&gt; &#8216;application.extensions.yii-mail.YiiMail&#8217;,<br \/>\n&#8216;transportType&#8217; =&gt; &#8216;smtp&#8217;, \/\/\/ case sensitive!<br \/>\n&#8216;transportOptions&#8217; =&gt; array(<br \/>\n&#8216;host&#8217; =&gt; &#8216;mail.syncomni.com&#8217;,<br \/>\n&#8216;username&#8217; =&gt; &#8216;huajian@syncomni.com&#8217;,<br \/>\n\/\/ or email@googleappsdomain.com<br \/>\n&#8216;password&#8217; =&gt; &#8216;Sitbwp4m2w&#8217;,<br \/>\n\/\/ &#8216;port&#8217; =&gt; &#8216;465&#8217;,<br \/>\n\/\/ &#8216;encryption&#8217; =&gt; &#8216;ssl&#8217;,<br \/>\n),<br \/>\n\/\/ &#8216;viewPath&#8217; =&gt; &#8216;application.views.mail&#8217;,<br \/>\n&#8216;logging&#8217; =&gt; true,<br \/>\n&#8216;dryRun&#8217; =&gt; false<br \/>\n),<br \/>\n),<br \/>\n\u53d1\u9001\u90ae\u4ef6<br \/>\n\/\/ \u53d1\u9001\u7535\u5b50\u90ae\u4ef6<br \/>\n$message = new YiiMailMessage;<br \/>\n$message-&gt;setSubject($model-&gt;notice_subject);<br \/>\n$message-&gt;setBody($model-&gt;notice);<br \/>\n$message-&gt;setTo(&#8216;kevin@syncomni.com&#8217;);<br \/>\n$message-&gt;from = Yii::app()-&gt;params[&#8216;adminEmail&#8217;];<br \/>\nYii::app()-&gt;mail-&gt;send($message);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e94\u7528\u6267\u884c\u6d41\u7a0b\uff1a \u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u53d1\u9001 Http Request | \u63a7\u5236\u5668(protected\/controll [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[48],"class_list":["post-902","post","type-post","status-publish","format-standard","hentry","category-php","tag-yii"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=902"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":903,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions\/903"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}