{"id":158,"date":"2016-01-23T15:38:59","date_gmt":"2016-01-23T07:38:59","guid":{"rendered":"http:\/\/blog.inforere.com\/?p=158"},"modified":"2016-02-29T14:45:50","modified_gmt":"2016-02-29T06:45:50","slug":"yii2%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/blog.inforere.com\/?p=158","title":{"rendered":"Yii2\u8bb0\u5f55"},"content":{"rendered":"<h3>\u6838\u5fc3\u6982\u5ff5<\/h3>\n<ol>\n<li>Object\u57fa\u7c7b\uff0c\u6784\u9020\u51fd\u6570()\u521d\u59cb\u5316\u5c5e\u6027<\/li>\n<li>Behavior\u884c\u4e3a, \u4e00\u822c\u6ce8\u518cComponent\u7684\u4e8b\u4ef6\uff0c\u516c\u7528\u65b9\u6cd5\u53ca\u5c5e\u6027\u4f9bComponent\u8c03\u7528<\/li>\n<li>Component\u7ec4\u4ef6\uff0c \u4e8b\u4ef6\u7ba1\u7406(on, off, trigger, attach\u7ed1\u5b9a\u4e8b\u4ef6 )\u3001\u52a8\u6001\u5c5e\u6027\u7ba1\u7406(__get)\u3001\u52a8\u6001\u65b9\u6cd5\u7ba1\u7406(__call)<\/li>\n<li>ServiceLocator\u5bb9\u5668, \u73b0\u5728\u6846\u67b6\u6807\u914d<\/li>\n<li>Module\u6a21\u5757\uff0c\u6709\u81ea\u5df1\u7684controller, view, widget\u6216\u9759\u6001\u8d44\u6e90<\/li>\n<li>Application\u5e94\u7528(web, console), \u7ee7\u627f\u6a21\u5757, \u5f15\u5bfc\u6269\u5c55\u3001\u7ec4\u4ef6; handle\u8bf7\u6c42\uff0c\u5e76\u54cd\u5e94\u7ed3\u679c(MVC\u6267\u884c\u6d41\u7a0b)<\/li>\n<li>Yii\u5168\u5c40\u5bf9\u8c61\uff0c \u5927\u91cf\u5e38\u7528\u7684\u9759\u6001\u65b9\u6cd5(\u81ea\u52a8\u52a0\u8f7d\u51fd\u6570\u3001t\u51fd\u6570\u3001\u8bbe\u7f6e\u5c5e\u6027\u51fd\u6570\u3001\u65e5\u5fd7\u51fd\u6570)\uff0c \u9759\u6001\u5c5e\u6027($app, \u5bb9\u5668\uff0c\u522b\u540d\uff0c\u7c7b\u540d\u6620\u5c04)<\/li>\n<li>ActionFilter\u8fc7\u6ee4\u5668, \u5728\u6267\u884caction\u4e4b\u524d\u8fdb\u884c\u67d0\u79cd\u64cd\u4f5c, \u76d1\u542cController::EVENT_BEFORE_ACTION\u4e8b\u4ef6, \u8bbe\u7f6eonly\u5c5e\u6027\u6570\u7ec4\uff0c\u53ef\u53ea\u5bf9\u67d0\u4e9baction\u8d77\u4f5c\u7528 <span style=\"color: #999999;\">@see\u00a0ActionFilter::isActive()<\/span><\/li>\n<li>Authentication \u8eab\u4efd\u8ba4\u8bc1\uff0c@see Yii\\web\\User,\u00a0IdentityInterface<\/li>\n<li>Authorization \u6388\u6743, \u00a0@see\u00a0yii\\filters\\AccessControl,\u00a0AccessRule<\/li>\n<\/ol>\n<h3>MVC\u7684\u8fd0\u884c<\/h3>\n<p>\u6839\u636eid\u627e\u5230\u6a21\u5757\u6216\u8bbe\u7f6ectrl\u8def\u5f84\u7684<strong>Controller<\/strong>\u5bf9\u8c61\uff0c<br \/>\n\u8fd0\u884cCtrl-&gt;runAction() \u65b9\u6cd5<br \/>\n-a)\u6267\u884cberforAction(), \u00a0\u5305\u62ec\u6a21\u5757\u53ca\u81ea\u8eab\u7684<br \/>\n-b)\u6267\u884c$action-&gt;runWithParams(), \u6267\u884caction::berforeRun(), run(), afterRtun()<br \/>\n-c)\u6267\u884cafterAction(), \u5305\u62ec\u81ea\u8eab\u53ca\u6a21\u5757\u7684<\/p>\n<p><strong>\u6267\u884c\u6d41\u7a0b\u62e6\u622a\u70b9<\/strong>\uff1a<span style=\"color: #999999;\">@see\u00a0ActionFilter<\/span><br \/>\n-1) (module, ctrl)::berforeAction()\u8fd4\u56defalse<br \/>\n-2) (module, ctrl)::EVENT_BEFORE_ACTION, \u4e8b\u4ef6\u76d1\u542c<br \/>\n-3) action-&gt;beforeRun() \u8fd4\u56de false<\/p>\n<p><strong>\u8fc7\u6ee4\u7ed3\u679c\u62e6\u622a\u70b9\uff1a<\/strong><br \/>\n-1&gt; (module, ctrl)::afterAction()<br \/>\n-2&gt; (module, ctrl)::EVENT_AFTER_ACTION, \u4e8b\u4ef6\u76d1\u542c<\/p>\n<p><strong>\u6539\u53d8\u9ed8\u8ba4\u6267\u884c\u6d41\u7a0b\uff1a<br \/>\n<\/strong>-1\uff09\u6539\u53d8action, \u8bbe\u7f6ectrl-&gt;actions(), \u6620\u5c04\u65b0\u7684Action\u7c7b\uff0c <span style=\"color: #999999;\">@see ctrl::createAction(id)<\/span><br \/>\n-2) \u00a0\u6539\u53d8ctrl\u52a0\u8f7d\uff0c\u8bbe\u7f6eapp|module\u7684controllerMap\u5c5e\u6027\uff0c<span style=\"color: #999999;\">@see Module::createController<\/span><\/p>\n<p><strong>\u89c6\u56fe\u8def\u5f84\u7684\u6e32\u67d3\uff1a<br \/>\n<\/strong>-1) layout\u8def\u5f84\u7684\u67e5\u8be2\uff0c \u4f18\u5148 module-&gt;layoutPath, \u540eapp-&gt;layoutPath, <span style=\"color: #999999;\">@see ctrl::findLayoutFile()<\/span><br \/>\n-2) \u89c6\u56fe\u7684\u8def\u5f84\u67e5\u8be2, <span style=\"color: #999999;\">@see View::findViewFile()<\/span><br \/>\na. @path\u522b\u540d\u8def\u5f84;<br \/>\nb. \/\/path App::getViewPath();<br \/>\nc. \/path Module::getViewPath();<br \/>\nd.\u00a0ViewContextInterface, \u5982widget,\u00a0ViewContextInterface::getViewPath()<br \/>\ne. $this-&gt;getViewFile(), \u00a0\u6700\u540e\u7684\u9632\u7ebf<\/p>\n<p><strong>\u9759\u6001\u8d44\u6e90\u7684\u53d1\u5e03\uff1a<br \/>\n<\/strong>a. View\u8fd0\u884cAssetBundle::register($view) \uff0c<br \/>\nb. \u8c03\u7528View::getAssetManager()-&gt;getBundle($name)\uff0c \u5f97\u5230AssetBundle<br \/>\nc. \u8fd0\u884c AssetBundle-&gt;publish($am) \u8fdb\u884c\u53d1\u5e03\u5230\u7f51\u7ad9\u6839\u76ee\u5f55\u5e76\u8bbe\u7f6e css, js\u5c5e\u6027<br \/>\nd. \u5728\u89c6\u56fe\u6587\u4ef6\u8bbe\u7f6e\u70b9\uff0c\u4e00\u822c\u662flayout\u6587\u4ef6, \u5982View::head(), begeinBody(), endBody()<br \/>\ne. \u5728View::endBody() \u4e2d\u8c03\u7528AssetBundle::registerAssetFiles($view) \u83b7\u53d6\u4e4b\u524d\u8bbe\u7f6e\u53d1\u5e03\u540e\u7684js, css<\/p>\n<p>\u53c2\u8003\uff1ahttp:\/\/www.yiiframework.com\/doc-2.0\/index.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6838\u5fc3\u6982\u5ff5 Object\u57fa\u7c7b\uff0c\u6784\u9020\u51fd\u6570()\u521d\u59cb\u5316\u5c5e\u6027 Behavior\u884c\u4e3a, \u4e00\u822c\u6ce8\u518cComponent\u7684\u4e8b\u4ef6\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/158"}],"collection":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158"}],"version-history":[{"count":18,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}