{"id":23102,"date":"2024-11-21T08:00:05","date_gmt":"2024-11-21T00:00:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23102\/"},"modified":"2024-11-21T08:00:05","modified_gmt":"2024-11-21T00:00:05","slug":"laravel%e6%96%b0%e5%bb%ba%e6%a8%a1%e5%9d%97%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23102\/","title":{"rendered":"laravel\u65b0\u5efa\u6a21\u5757\u6d41\u7a0b"},"content":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u73b0\u4ee3\u5316\u7684php\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5f00\u53d1web\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\uff0c\u6a21\u5757\u5316\u7f16\u7a0b\u662f\u4e00\u79cd\u6781\u4e3a\u91cd\u8981\u7684\u7f16\u7a0b\u601d\u60f3\uff0c\u5728laravel\u4e2d\u4e5f\u6709\u7740\u5f88\u597d\u7684\u4f53\u73b0\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u5728laravel\u4e2d\u65b0\u5efa\u6a21\u5757\u7684\u6d41\u7a0b\u548c\u6b65\u9aa4\u3002<\/p>\n<p>\u4e00\u3001\u521b\u5efa\u6a21\u5757\u76ee\u5f55\u7ed3\u6784<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u6a21\u5757\u662f\u7531\u4e00\u4e9b\u76f8\u5173\u7684\u63a7\u5236\u5668\u3001\u6a21\u578b\u3001\u89c6\u56fe\u3001\u8bed\u8a00\u5305\u7b49\u6784\u6210\u7684\u96c6\u5408\uff0c\u56e0\u6b64\uff0c\u5728\u65b0\u5efa\u6a21\u5757\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u6a21\u5757\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5728Laravel\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u540d\u4e3a&#8221;Modules&#8221;\u7684\u76ee\u5f55\uff0c\u5e76\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u521b\u5efa&#8221;Example&#8221;\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>app\/Modules\/Example\n\u251c\u2500\u2500 Config\n\u2502   \u2514\u2500\u2500 config.php\n\u251c\u2500\u2500 Controllers\n\u2502   \u2514\u2500\u2500 ExampleController.php\n\u251c\u2500\u2500 Models\n\u2502   \u251c\u2500\u2500 Example.php\n\u2502   \u2514\u2500\u2500 ExampleRepository.php\n\u251c\u2500\u2500 Resources\n\u2502   \u251c\u2500\u2500 Lang\n\u2502   \u2502   \u251c\u2500\u2500 en\n\u2502   \u2502   \u2502   \u2514\u2500\u2500 example.php\n\u2502   \u2502   \u2514\u2500\u2500 zh\n\u2502   \u2502       \u2514\u2500\u2500 example.php\n\u2502   \u251c\u2500\u2500 Views\n\u2502   \u2502   \u2514\u2500\u2500 index.blade.php\n\u2502   \u2514\u2500\u2500 assets\n\u251c\u2500\u2500 Routes\n\u2502   \u251c\u2500\u2500 api.php\n\u2502   \u2514\u2500\u2500 web.php\n\u2514\u2500\u2500 Providers\n    \u2514\u2500\u2500 ExampleServiceProvider.php<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u76ee\u5f55\u7ed3\u6784\u4e2d\uff0cConfig\u76ee\u5f55\u7528\u6765\u5b58\u653e\u6a21\u5757\u7684\u914d\u7f6e\u6587\u4ef6\uff0cControllers\u76ee\u5f55\u5b58\u653e\u63a7\u5236\u5668\u6587\u4ef6\uff0cModels\u76ee\u5f55\u5b58\u653e\u6a21\u578b\u6587\u4ef6\uff0cResources\u76ee\u5f55\u5b58\u653e\u5176\u4ed6\u8d44\u6e90\u6587\u4ef6\uff0c\u5982\u89c6\u56fe\u3001\u8bed\u8a00\u5305\u3001\u9759\u6001\u6587\u4ef6\u7b49\uff0cRoutes\u76ee\u5f55\u7528\u6765\u5b58\u653e\u8be5\u6a21\u5757\u7684\u8def\u7531\u6587\u4ef6\uff0cProviders\u76ee\u5f55\u7528\u6765\u5b58\u653e\u8be5\u6a21\u5757\u7684\u670d\u52a1\u63d0\u4f9b\u8005\u3002\u5176\u4e2d\uff0cExample\u4e3a\u8be5\u6a21\u5757\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u547d\u540d\u3002<\/p>\n<p>\u4e8c\u3001\u914d\u7f6e\u8def\u7531\u6587\u4ef6<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u8def\u7531\u662f\u5c06URI\u6620\u5c04\u5230\u7279\u5b9a\u7684\u64cd\u4f5c\u7684\u5730\u65b9\u3002\u56e0\u6b64\uff0c\u5728\u65b0\u5efa\u6a21\u5757\u65f6\uff0c\u9700\u8981\u914d\u7f6e\u8be5\u6a21\u5757\u7684\u8def\u7531\u6587\u4ef6\u3002\u5728Example\u6a21\u5757\u7684Routes\u76ee\u5f55\u4e0b\uff0c\u65b0\u5efaweb.php\u548capi.php\u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u5728\u8fd9\u4e24\u4e2a\u6587\u4ef6\u4e2d\u5206\u522b\u5b9a\u4e49\u8be5\u6a21\u5757\u7684\u524d\u53f0\u548c\u540e\u53f0\u8def\u7531\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/web.php\nRoute::group(['module' =&amp;gt; 'Example', 'middleware' =&amp;gt; ['web']], function() {\n    Route::get('\/', 'ExampleController@index');\n});\n\n\/\/api.php\nRoute::group(['module' =&amp;gt; 'Example', 'namespace' =&amp;gt; 'AppModulesExampleControllersApi', 'prefix' =&amp;gt; 'api\/v1', 'middleware' =&amp;gt; ['api']], function() {\n    Route::get('\/', 'ExampleApiController@index');\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cweb.php\u6587\u4ef6\u5b9a\u4e49\u4e86\u4e00\u4e2aget\u8bf7\u6c42\uff0c\u5c06\u6839\u76ee\u5f55&#8221;\/&#8221;\u6620\u5c04\u5230Example\u63a7\u5236\u5668\u7684index\u65b9\u6cd5\u4e0a\u3002\u800capi.php\u6587\u4ef6\u5b9a\u4e49\u4e86\u4e00\u4e2aget\u8bf7\u6c42\uff0c\u5c06&#8221;\/api\/v1&#8243;\u6620\u5c04\u5230ExampleApiController\u63a7\u5236\u5668\u7684index\u65b9\u6cd5\u4e0a\u3002\u5176\u4e2d\uff0c&#8217;module&#8217;\u53c2\u6570\u6307\u5b9a\u4e86\u8be5\u8def\u7531\u5c5e\u4e8eExample\u6a21\u5757\uff0c&#8217;middleware&#8217;\u53c2\u6570\u6307\u5b9a\u4e86\u8be5\u8def\u7531\u4f9d\u8d56\u4e8eweb\u4e2d\u95f4\u4ef6\u6216api\u4e2d\u95f4\u4ef6\uff0c\u8fd9\u4e9b\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<p>\u4e09\u3001\u521b\u5efa\u63a7\u5236\u5668<\/p>\n<p>\u63a7\u5236\u5668\u662fLaravel\u4e2d\u7528\u4e8e\u5904\u7406\u7528\u6237\u8bf7\u6c42\u7684\u4e00\u79cd\u673a\u5236\u3002\u5728Example\u6a21\u5757\u7684Controllers\u76ee\u5f55\u4e0b\uff0c\u65b0\u5efaExampleController.php\u6587\u4ef6\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2aindex\u65b9\u6cd5\u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>namespace AppModulesExampleControllers;\n\nuse AppHttpControllersController;\n\nclass ExampleController extends Controller {\n\n    public function index() {\n        return view('example::index');\n    }\n\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cExampleController\u7c7b\u7ee7\u627f\u81eaLaravel\u6846\u67b6\u63d0\u4f9b\u7684Controller\u7c7b\uff0c\u5b83\u5305\u542b\u4e00\u4e2aindex\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5c06\u6a21\u677f\u6587\u4ef6example::index\u8fd4\u56de\u3002<\/p>\n<p>\u56db\u3001\u521b\u5efa\u89c6\u56fe\u6587\u4ef6<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u89c6\u56fe\u7528\u4e8e\u663e\u793aHTML\u9875\u9762\u6216\u5176\u4ed6\u54cd\u5e94\u6570\u636e\u3002\u5728Example\u6a21\u5757\u7684Resources\u76ee\u5f55\u4e0b\uff0c\u65b0\u5efaViews\u76ee\u5f55\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efaindex.blade.php\uff0c\u8be5\u6587\u4ef6\u662f\u7528\u6765\u4f5c\u4e3a\u4f8b\u5b50\u7684\u89c6\u56fe\u6587\u4ef6\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;Example&lt;\/title&gt;&lt;h1&gt;Welcome to Example Module!&lt;\/h1&gt;\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cindex.blade.php\u6587\u4ef6\u53ea\u5305\u542b\u4e00\u4e2a\u7b80\u5355\u7684\u6b22\u8fce\u8bed\u53e5\uff0c\u7528\u6765\u8868\u793a\u8fd9\u662f\u8be5\u6a21\u5757\u7684\u9996\u9875\u3002<\/p>\n<p>\u4e94\u3001\u521b\u5efa\u6a21\u578b\u548c\u670d\u52a1\u63d0\u4f9b\u8005<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u6a21\u578b\u7528\u4e8e\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\uff0c\u901a\u5e38\u7528\u6765\u5c01\u88c5\u4e00\u5f20\u8868\u3002\u5728Example\u6a21\u5757\u7684Models\u76ee\u5f55\u4e0b\uff0c\u521b\u5efaExample.php\u548cExampleRepository.php\u4e24\u4e2a\u6587\u4ef6\uff0c\u5206\u522b\u4ee3\u8868\u6a21\u578b\u548c\u4ed3\u50a8\u7c7b\uff0c\u5b83\u4eec\u7684\u4f5c\u7528\u5206\u522b\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u548c\u5c01\u88c5\u4e00\u4e9b\u67e5\u8be2\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u521b\u5efa\u5b8c\u6210\u6a21\u578b\u548c\u4ed3\u50a8\u7c7b\u4e4b\u540e\uff0c\u9700\u8981\u5728Providers\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u63d0\u4f9b\u8005\uff0c\u7528\u4e8e\u6ce8\u518c\u6a21\u578b\u548c\u4ed3\u50a8\u7c7b\uff0c\u540c\u65f6\u5b9e\u73b0\u670d\u52a1\u63d0\u4f9b\u8005\u7684register\u65b9\u6cd5\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>namespace AppModulesExampleProviders;\n\nuse IlluminateSupportServiceProvider;\n\nclass ExampleServiceProvider extends ServiceProvider {\n\n    public function boot() {\n        \n    }\n\n    public function register() {\n        $this-&amp;gt;app-&amp;gt;bind('AppModulesExampleModelsExampleRepositoryInterface', 'AppModulesExampleModelsExampleRepository');\n    }\n\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cExampleServiceProvider\u7c7b\u7ee7\u627f\u81eaLaravel\u6846\u67b6\u63d0\u4f9b\u7684ServiceProvider\u7c7b\uff0c\u901a\u8fc7register\u65b9\u6cd5\u6ce8\u518c\u4e86ExampleRepository\u63a5\u53e3\u548cExampleRepository\u5b9e\u73b0\u7c7b\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bb9\u5668\u83b7\u53d6ExampleRepository\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528\u5176\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u529f\u80fd\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u6a21\u5757\u5316\u7f16\u7a0b\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u7f16\u7a0b\u601d\u60f3\uff0c\u5728Laravel\u4e2d\u4e5f\u6709\u7740\u826f\u597d\u7684\u4f53\u73b0\u3002\u901a\u8fc7\u4e0a\u8ff0\u7684\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Laravel\u4e2d\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\uff0c\u5e76\u5b9e\u73b0\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u548c\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u5f53\u7136\uff0c\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6a21\u5757\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\u4e0d\u6b62\u4e8e\u6b64\uff0c\u9700\u8981\u7ed3\u5408\u5177\u4f53\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u5408\u7406\u8bbe\u8ba1\u548c\u5f00\u53d1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u65b0\u5efa\u6a21\u5757\u6d41\u7a0b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>laravel\u662f\u4e00\u6b3e\u73b0\u4ee3\u5316\u7684php\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u9ad8\u6548\u5730\u5f00\u53d1web\u5e94\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\uff0c\u6a21\u5757\u5316\u7f16\u7a0b\u662f\u4e00\u79cd\u6781\u4e3a\u91cd\u8981\u7684\u7f16\u7a0b\u601d\u60f3\uff0c\u5728laravel\u4e2d\u4e5f\u6709\u7740\u5f88\u597d\u7684\u4f53\u73b0\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u5728laravel\u4e2d\u65b0\u5efa\u6a21\u5757\u7684\u6d41\u7a0b\u548c\u6b65\u9aa4\u3002 \u4e00\u3001\u521b\u5efa\u6a21\u5757\u76ee\u5f55\u7ed3\u6784 \u5728Laravel\u4e2d\uff0c\u6a21\u5757\u662f\u7531\u4e00\u4e9b\u76f8\u5173\u7684\u63a7\u5236\u5668\u3001\u6a21\u578b\u3001\u89c6\u56fe\u3001\u8bed\u8a00\u5305\u7b49\u6784\u6210\u7684\u96c6\u5408\uff0c\u56e0\u6b64\uff0c\u5728\u65b0\u5efa\u6a21\u5757\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u6a21\u5757\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5728Laravel\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u540d\u4e3a&#8221;Modules&#8221;\u7684\u76ee\u5f55\uff0c\u5e76\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u521b\u5efa&#8221;Example&#8221;\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a app\/Modules\/Example \u251c\u2500\u2500 Config \u2502 \u2514\u2500\u2500 config.php \u251c\u2500\u2500 Controllers \u2502 \u2514\u2500\u2500 ExampleController.php \u251c\u2500\u2500 Models \u2502 \u251c\u2500\u2500 Example.php \u2502 \u2514\u2500\u2500 ExampleRepository.php \u251c\u2500\u2500 Resources \u2502 \u251c\u2500\u2500 Lang \u2502 \u2502 \u251c\u2500\u2500 en \u2502 \u2502 \u2502 \u2514\u2500\u2500 example.php \u2502 \u2502 \u2514\u2500\u2500 zh \u2502 \u2502 \u2514\u2500\u2500 example.php \u2502 \u251c\u2500\u2500 Views \u2502 \u2502 \u2514\u2500\u2500 index.blade.php \u2502 \u2514\u2500\u2500 assets \u251c\u2500\u2500 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23102","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23102"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23102\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}