{"id":1031,"date":"2015-02-02T14:55:08","date_gmt":"2015-02-02T06:55:08","guid":{"rendered":"http:\/\/www.poloo.org\/?p=1031"},"modified":"2015-02-10T16:14:54","modified_gmt":"2015-02-10T08:14:54","slug":"%e5%9c%a8laravel%e4%b8%ad%e4%bd%bf%e7%94%a8%e8%87%aa%e5%b7%b1%e7%9a%84%e7%b1%bb%e5%ba%93%e4%b8%89%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=1031","title":{"rendered":"\u5728Laravel\u4e2d\u4f7f\u7528\u81ea\u5df1\u7684\u7c7b\u5e93\u4e09\u79cd\u65b9\u5f0f"},"content":{"rendered":"<h2><a href=\"http:\/\/www.poloo.org\/uploads\/\/2015\/02\/laravel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1032\" src=\"http:\/\/www.poloo.org\/uploads\/\/2015\/02\/laravel.png\" alt=\"laravel\" width=\"319\" height=\"209\" srcset=\"https:\/\/www.poloo.org\/uploads\/2015\/02\/laravel.png 319w, https:\/\/www.poloo.org\/uploads\/2015\/02\/laravel-300x196.png 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/a><\/h2>\n<h2 id=\"title0\">\u589e\u52a0\u53ef\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684\u7c7b<\/h2>\n<p><!--more--><\/p>\n<p>\u6709\u4e9b\u9700\u8981\u76f4\u63a5\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u589e\u52a0\u5230Laravel\u4e2d<\/p>\n<ol>\n<li>\u521b\u5efa\u7c7b\u5e93\u6587\u4ef6<code>app\/libraries\/class\/myClass.php<\/code><\/li>\n<li>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/li>\n<\/ol>\n<pre><code>&lt;?php\r\nclass Message {\r\n    public static function display() {\r\n\r\n    }\r\n}\r\n?&gt;\r\n\r\n<\/code><\/pre>\n<ol>\n<li>\u5728<code>app\/start\/globals.php<\/code>\u4e2d\u589e\u52a0\u7c7b\u5bfc\u5165\u8def\u5f84<\/li>\n<\/ol>\n<pre><code>&lt;?php \r\nClassLoader::addDirectories(array(\r\n\r\n    app_path().'\/commands',\r\n    app_path().'\/controllers',\r\n    app_path().'\/models',\r\n    app_path().'\/database\/seeds',\r\n    app_path().'\/libaries\/class', \/\/ \u5728\u8fd9\u91cc\u589e\u52a0\r\n\r\n));\r\n?&gt;<\/code><\/pre>\n<ol>\n<li>\u5728<code>composer.json<\/code>\u4e2d\u589e\u52a0autoload\u76ee\u5f55<\/li>\n<\/ol>\n<pre><code>\"autoload\": {\r\n    \"classmap\": [\r\n        \"app\/commands\",\r\n        \"app\/controllers\",\r\n        \"app\/models\",\r\n        \"app\/database\/migrations\",\r\n        \"app\/database\/seeds\",\r\n        \"app\/tests\/TestCase.php\",\r\n        \"app\/libraries\/class\"   \/\/\u5728\u8fd9\u91cc\u589e\u52a0\r\n    ]\r\n},<\/code><\/pre>\n<ol>\n<li>\u6267\u884c<code>composer dump-autoload<\/code>\u6765\u521b\u5efa\u5bfc\u5165\u6620\u5c04<\/li>\n<li>\u4f7f\u7528\u81ea\u5df1\u5bfc\u5165\u7684\u7c7b\u76f4\u63a5\u8c03\u7528<code>Message::display()<\/code>\u5373\u53ef<\/li>\n<\/ol>\n<blockquote><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u4e5f\u662f\u589e\u52a0\u961f\u5217\u7c7b\u7684\u65b9\u6cd5\uff0c\u5f88\u591a\u4eba\u4e0d\u77e5\u9053Laravel\u4e2d\u961f\u5217\u5904\u7406\u7c7b\u5e94\u8be5\u653e\u5728\u54ea\u91cc\uff0c\u5176\u5b9e\u6309\u7167\u4e0a\u9762\u7684\u65b9\u6cd5\uff0c\u5728<code>app<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>queues<\/code>\u76ee\u5f55\uff0c\u7136\u540e\u8ba9\u5176\u53ef\u4ee5\u76f4\u63a5\u5b9e\u4f8b\u5316\u5373\u53ef<\/p><\/blockquote>\n<h2 id=\"title1\">\u589e\u52a0\u53ef\u76f4\u63a5\u8c03\u7528\u7684\u51fd\u6570<\/h2>\n<p>\u6709\u4eba\u559c\u6b22\u7528<code>v()<\/code>\u6765\u4ee3\u66ff<code>var_dump()<\/code>\uff0c\u60f3\u8981\u5728Laravel\u4e2d\u8fd9\u4e48\u505a\u4e5f\u975e\u5e38\u5bb9\u6613<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6587\u4ef6<code>app\/libraries\/function\/helper.php<\/code><\/li>\n<li>\u5199\u5165\u6587\u4ef6\u5185\u5bb9<\/li>\n<\/ol>\n<pre><code>&lt;?php \r\nfunction v($msg){\r\n    var_dump($msg);\r\n}\r\n?&gt;<\/code><\/pre>\n<ol>\n<li>\u628a\u6587\u4ef6\u589e\u52a0\u5230composer\u81ea\u52a8\u5bfc\u5165\u5217\u8868\u4e2d<\/li>\n<\/ol>\n<pre><code>\"autoload\": {\r\n   \"classmap\": [\r\n       ...\r\n   ],\r\n   \"files\": [\r\n       \"app\/libraries\/function\/helper.php\"\r\n   ],\r\n},<\/code><\/pre>\n<p>\u6216\u8005\u5728\u9879\u76ee\u4e2d\u663e\u793a<code>require<\/code>\u8fd9\u4e2a\u6587\u4ef6\u3002\u6253\u5f00<code>app\/start\/global.php<\/code>\uff0c\u5728\u672b\u5c3e\u589e\u52a0\uff1a<\/p>\n<pre><code>require app_path().'\/libraries\/function\/helper.php';<\/code><\/pre>\n<p>\u4e2a\u4eba\u611f\u89c9\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fdOK\uff0c\u5982\u679c\u60f3\u8981\u63a7\u5236\u8fd9\u4e2a\u6587\u4ef6\u52a0\u8f7d\u7684\u65f6\u95f4\uff0c\u751a\u81f3\u53ef\u4ee5\u5728<code>filter.php<\/code>\u6587\u4ef6\u4e2d\u589e\u52a0\u4ee5\u4e0b\u5185\u5bb9<\/p>\n<pre><code>App::before( function( $request ) {\r\n    require( \"{$GLOBALS['app']['path.base']}\/app\/libraries\/function\/helper.php\" );\r\n});<\/code><\/pre>\n<ol>\n<li>\u5728\u9879\u76ee\u4e2d\u76f4\u63a5\u4f7f\u7528\u51fd\u6570<code>v('hello world')<\/code>;<\/li>\n<\/ol>\n<h2 id=\"title2\">\u589e\u52a0\u7a0d\u5fae\u590d\u6742\u7684\u7c7b\u5e93<\/h2>\n<p>\u6709\u7684\u65f6\u5019\u4e00\u4e2a\u7c7b\u5e93\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6587\u4ef6\u90a3\u4e48\u7b80\u5355\uff0c\u56e0\u6b64\u4e0b\u9762\u7684\u65b9\u5f0f\u66f4\u52a0\u9002\u5408\u6709\u591a\u4e2a\u6587\u4ef6\u591a\u4e2a\u7ed3\u6784\u7684\u7c7b\u5e93\u3002<\/p>\n<ol>\n<li>\u521b\u5efa<a href=\"https:\/\/github.com\/php-fig\/fig-standards\/blob\/master\/accepted\/PSR-0.md\">psr0<\/a>\u6216\u8005<a href=\"https:\/\/github.com\/php-fig\/fig-standards\/blob\/master\/accepted\/PSR-4-autoloader.md\">psr4<\/a>\u6807\u51c6\u7684\u76ee\u5f55\u7ed3\u6784\u3002<\/li>\n<\/ol>\n<pre><code>libraries\r\n    Myapp\r\n        Search (note directory is capitalized)\r\n            Search.php\r\n            SearchFacade.php\r\n            SearchServiceProvider.php\r\n        AnotherLib<\/code><\/pre>\n<p><code>Myapp\/Search\/Search.php<\/code>\u4e2d<code>Search<\/code>\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\u4e3a<code>Myapp\\Search<\/code>\u3002<\/p>\n<ol>\n<li>\u4fee\u6539composer\u4e2dautoload<\/li>\n<\/ol>\n<pre><code>\"autoload\": {\r\n    \"classmap\": [\r\n        \"app\/commands\",\r\n        \"app\/controllers\",\r\n        \"app\/models\",\r\n        \"app\/libraries\",\r\n        \"app\/database\/migrations\",\r\n        \"app\/database\/seeds\",\r\n        \"app\/tests\/TestCase.php\"\r\n    ]\r\n    ,\r\n    \"psr-0\": {\r\n         \"Myapp\": \"app\/libraries\"\r\n    }\r\n},<\/code><\/pre>\n<ol>\n<li>\u5728\u9879\u76ee\u4e2d\u4f7f\u7528<code>new Myapp\\Search\\Search()<\/code>\u6765\u5b9e\u4f8b\u5316\u67d0\u4e00\u7c7b<\/li>\n<\/ol>\n<h2 id=\"title3\">\u603b\u7ed3<\/h2>\n<p>\u867d\u7136Laravel\u6ca1\u6709\u5f3a\u5236\u54ea\u79cd\u65b9\u5f0f\u6700\u597d\uff0c\u4f46\u662f\u6709\u4e00\u5b9a\u7684\u6807\u51c6\u53ef\u4ee5\u4f7f\u5f97\u9879\u76ee\u7ed3\u6784\u6e05\u6670\uff0c\u591a\u4eba\u5408\u4f5c\u5f00\u53d1\u65f6\u7701\u53bb\u5f88\u591a\u4ea4\u6d41\u6210\u672c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u589e\u52a0\u53ef\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684\u7c7b<\/p>\n","protected":false},"author":1,"featured_media":1032,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18],"tags":[167,36],"class_list":["post-1031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-Note","category-Php","tag-laravel","tag-php-2"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1031"}],"version-history":[{"count":4,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1035,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/1031\/revisions\/1035"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/media\/1032"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}