{"id":24286,"date":"2024-11-21T12:40:10","date_gmt":"2024-11-21T04:40:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24286\/"},"modified":"2024-11-21T12:40:10","modified_gmt":"2024-11-21T04:40:10","slug":"laravel%e7%9a%84%e6%a8%a1%e5%9e%8b-%e8%a7%86%e5%9b%be-%e6%8e%a7%e5%88%b6%e5%99%a8%ef%bc%88mvc%ef%bc%89%e6%9e%b6%e6%9e%84%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24286\/","title":{"rendered":"Laravel\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u6784\u89e3\u6790"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169181454666911.jpg\" class=\"aligncenter\" title=\"Laravel\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u6784\u89e3\u6790\u63d2\u56fe\" alt=\"Laravel\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u6784\u89e3\u6790\u63d2\u56fe\" \/><\/p>\n<p>Laravel\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u6784\u89e3\u6790<\/p>\n<p>MVC\uff08Model-View-Controller\uff09\u662f\u4e00\u79cd\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5e7f\u6cdb\u5e94\u7528\u7684\u67b6\u6784\u6a21\u5f0f\u3002Laravel\u6846\u67b6\u4e5f\u91c7\u7528\u4e86MVC\u67b6\u6784\uff0c\u5b83\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u5c06\u5e94\u7528\u7a0b\u5e8f\u5206\u4e3a\u6a21\u578b\uff08Model\uff09\u3001\u89c6\u56fe\uff08View\uff09\u548c\u63a7\u5236\u5668\uff08Controller\uff09\u4e09\u5c42\uff0c\u901a\u8fc7\u5404\u81ea\u7684\u804c\u8d23\u534f\u540c\u5de5\u4f5c\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u9ad8\u5185\u805a\u548c\u4f4e\u8026\u5408\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Laravel\u4e2d\u7684MVC\u67b6\u6784\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u6765\u7406\u89e3\u5176\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<ol>\n<li>\u6a21\u578b\uff08Model\uff09<br \/>\u6a21\u578b\u5c42\u662fLaravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u5904\u7406\u6570\u636e\u7684\u90e8\u5206\u3002\u901a\u5e38\uff0c\u6a21\u578b\u5c42\u5305\u542b\u4e86\u4e0e\u6570\u636e\u4ea4\u4e92\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u64cd\u4f5c\u3002\u5728Laravel\u4e2d\uff0c\u6a21\u578b\u7ee7\u627f\u81eaEloquent\u7c7b\uff0cEloquent\u662fLaravel\u7684ORM\uff08Object Relational Mapping\uff09\u7cfb\u7edf\uff0c\u901a\u8fc7\u5b83\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u636e\u5e93\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u6a21\u578b\u793a\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4e00\u4e2a\u7528\u6237\u8868\u6765\u5b58\u50a8\u7528\u6237\u4fe1\u606f\uff1a<\/p>\n<pre>&lt;?php namespace AppModels;\n\nuse IlluminateDatabaseEloquentModel;\n\nclass User extends Model\n{\n    protected $table = 'users';\n    protected $fillable = ['name', 'email', 'password'];\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aUser\u6a21\u578b\uff0c\u7ee7\u627f\u81eaIlluminateDatabaseEloquentModel\u7c7b\u3002$table\u5c5e\u6027\u6307\u5b9a\u4e86\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8868\u540d\uff0c$fillable\u5c5e\u6027\u5b9a\u4e49\u4e86\u53ef\u88ab\u6279\u91cf\u8d4b\u503c\u7684\u5b57\u6bb5\u3002<\/p>\n<ol>\n<li>\u89c6\u56fe\uff08View\uff09<br \/>\u89c6\u56fe\u5c42\u662fLaravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u7528\u6237\u754c\u9762\u5c55\u793a\u7684\u90e8\u5206\u3002\u5728MVC\u67b6\u6784\u4e2d\uff0c\u89c6\u56fe\u5c42\u8d1f\u8d23\u5c06\u6a21\u578b\u5c42\u7684\u6570\u636e\u5448\u73b0\u7ed9\u7528\u6237\u3002\u5728Laravel\u4e2d\uff0c\u89c6\u56fe\u901a\u5e38\u4f7f\u7528Blade\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u89c6\u56fe\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u7528\u6237\u5217\u8868\u7684\u754c\u9762\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;User List&lt;\/title&gt;&lt;h1&gt;User List&lt;\/h1&gt;\n    <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n  @foreach($users as $user) <\/p>\n<li>{{ $user-&gt;name }}<\/li>\n<p> @endforeach\n<\/ul>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Blade\u6a21\u677f\u5f15\u64ce\u7684\u8bed\u6cd5\u3002{{ $user-&gt;name }}\u8868\u793a\u8f93\u51fa$user\u6a21\u578b\u7684name\u5b57\u6bb5\u3002<\/p>\n<ol>\n<li>\u63a7\u5236\u5668\uff08Controller\uff09<br \/>\u63a7\u5236\u5668\u5c42\u662fLaravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u5904\u7406\u7528\u6237\u8bf7\u6c42\u548c\u63a7\u5236\u4e1a\u52a1\u903b\u8f91\u7684\u90e8\u5206\u3002\u5728Laravel\u4e2d\uff0c\u63a7\u5236\u5668\u901a\u5e38\u662f\u4e00\u4e2a\u7c7b\uff0c\u5305\u542b\u591a\u4e2a\u52a8\u4f5c\uff08Action\uff09\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u63a7\u5236\u5668\u793a\u4f8b\uff0c\u7528\u4e8e\u5904\u7406\u7528\u6237\u5217\u8868\u7684\u8bf7\u6c42\uff1a<\/p>\n<pre>&lt;?php namespace AppHttpControllers;\n\nuse IlluminateHttpRequest;\nuse AppModelsUser;\n\nclass UserController extends Controller\n{\n    public function index()\n    {\n        $users = User::all();\n        return view('user.list', ['users' =&gt; $users]);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aUserController\u63a7\u5236\u5668\u7c7b\uff0c\u5176\u4e2d\u7684index\u65b9\u6cd5\u7528\u4e8e\u5904\u7406\u7528\u6237\u5217\u8868\u7684\u8bf7\u6c42\u3002\u5728index\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528User\u6a21\u578b\u7684all\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u7528\u6237\u6570\u636e\uff0c\u5e76\u901a\u8fc7view\u65b9\u6cd5\u8fd4\u56deuser.list\u89c6\u56fe\uff0c\u5e76\u4f20\u9012\u4e86\u4e00\u4e2a\u540d\u4e3ausers\u7684\u6570\u7ec4\u53c2\u6570\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u4e2d\u5c06\u8bf7\u6c42\u4e0e\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u5173\u8054\u8d77\u6765\uff1a<\/p>\n<pre>Route::get('\/users', 'AppHttpControllersUserController@index');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u5f53\u8bbf\u95ee\/users\u8def\u5f84\u65f6\uff0c\u5c06\u8c03\u7528UserController\u7684index\u65b9\u6cd5\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Laravel\u7684MVC\u67b6\u6784\u7684\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u3002\u6a21\u578b\u5c42\u8d1f\u8d23\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u89c6\u56fe\u5c42\u5c55\u793a\u6570\u636e\u7ed9\u7528\u6237\uff0c\u63a7\u5236\u5668\u5c42\u5904\u7406\u7528\u6237\u8bf7\u6c42\u548c\u8c03\u5ea6\u4e1a\u52a1\u903b\u8f91\u3002\u8fd9\u79cd\u5206\u5c42\u7684\u65b9\u5f0f\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u7684\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u7b80\u6d01\uff0c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u901a\u8fc7\u4e0a\u8ff0\u5bf9Laravel\u7684MVC\u67b6\u6784\u89e3\u6790\uff0c\u6211\u4eec\u4e86\u89e3\u5230\u4e86\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\u7684\u5206\u5c42\u67b6\u6784\u6a21\u5f0f\u3002MVC\u67b6\u6784\u7684\u4f18\u70b9\u5728\u4e8e\u4ee3\u7801\u7684\u9ad8\u5185\u805a\u548c\u4f4e\u8026\u5408\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u66f4\u52a0\u9ad8\u6548\u548c\u53ef\u6269\u5c55\u3002\u540c\u65f6\uff0cLaravel\u6846\u67b6\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u548c\u7ea6\u5b9a\uff0c\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u66f4\u52a0\u4fbf\u6377\u5730\u5b9e\u73b0MVC\u67b6\u6784\u3002<\/p>\n<p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u8ba9\u8bfb\u8005\u5bf9Laravel\u7684MVC\u67b6\u6784\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u80fd\u591f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u597d\u5730\u8fd0\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u6784\u89e3\u6790\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\u7684\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff08MVC\uff09\u67b6\u6784\u89e3\u6790 MVC\uff08Model-View-Controller\uff09\u662f\u4e00\u79cd\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5e7f\u6cdb\u5e94\u7528\u7684\u67b6\u6784\u6a21\u5f0f\u3002Laravel\u6846\u67b6\u4e5f\u91c7\u7528\u4e86MVC\u67b6\u6784\uff0c\u5b83\u7684\u6838\u5fc3\u6982\u5ff5\u662f\u5c06\u5e94\u7528\u7a0b\u5e8f\u5206\u4e3a\u6a21\u578b\uff08Model\uff09\u3001\u89c6\u56fe\uff08View\uff09\u548c\u63a7\u5236\u5668\uff08Controller\uff09\u4e09\u5c42\uff0c\u901a\u8fc7\u5404\u81ea\u7684\u804c\u8d23\u534f\u540c\u5de5\u4f5c\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u9ad8\u5185\u805a\u548c\u4f4e\u8026\u5408\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002 \u4e0b\u9762\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Laravel\u4e2d\u7684MVC\u67b6\u6784\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u6765\u7406\u89e3\u5176\u5b9e\u73b0\u65b9\u5f0f\u3002 \u6a21\u578b\uff08Model\uff09\u6a21\u578b\u5c42\u662fLaravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u5904\u7406\u6570\u636e\u7684\u90e8\u5206\u3002\u901a\u5e38\uff0c\u6a21\u578b\u5c42\u5305\u542b\u4e86\u4e0e\u6570\u636e\u4ea4\u4e92\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u64cd\u4f5c\u3002\u5728Laravel\u4e2d\uff0c\u6a21\u578b\u7ee7\u627f\u81eaEloquent\u7c7b\uff0cEloquent\u662fLaravel\u7684ORM\uff08Object Relational Mapping\uff09\u7cfb\u7edf\uff0c\u901a\u8fc7\u5b83\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u636e\u5e93\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u6a21\u578b\u793a\u4f8b\uff0c\u5047\u8bbe\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4e00\u4e2a\u7528\u6237\u8868\u6765\u5b58\u50a8\u7528\u6237\u4fe1\u606f\uff1a &lt;?php namespace AppModels; use IlluminateDatabaseEloquentModel; class User extends Model { protected $table = &#8216;users&#8217;; protected $fillable = [&#8216;name&#8217;, &#8217;email&#8217;, &#8216;password&#8217;]; } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aUser\u6a21\u578b\uff0c\u7ee7\u627f\u81eaIlluminateDatabaseEloquentModel\u7c7b\u3002$table\u5c5e\u6027\u6307\u5b9a\u4e86\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8868\u540d\uff0c$fillable\u5c5e\u6027\u5b9a\u4e49\u4e86\u53ef\u88ab\u6279\u91cf\u8d4b\u503c\u7684\u5b57\u6bb5\u3002 \u89c6\u56fe\uff08View\uff09\u89c6\u56fe\u5c42\u662fLaravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u7528\u6237\u754c\u9762\u5c55\u793a\u7684\u90e8\u5206\u3002\u5728MVC\u67b6\u6784\u4e2d\uff0c\u89c6\u56fe\u5c42\u8d1f\u8d23\u5c06\u6a21\u578b\u5c42\u7684\u6570\u636e\u5448\u73b0\u7ed9\u7528\u6237\u3002\u5728Laravel\u4e2d\uff0c\u89c6\u56fe\u901a\u5e38\u4f7f\u7528Blade\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u89c6\u56fe\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u7528\u6237\u5217\u8868\u7684\u754c\u9762\uff1a &lt;title&gt;User List&lt;\/title&gt;&lt;h1&gt;User List&lt;\/h1&gt; \u767b\u5f55\u540e\u590d\u5236 @foreach($users as $user) {{ $user-&gt;name }} @endforeach \u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Blade\u6a21\u677f\u5f15\u64ce\u7684\u8bed\u6cd5\u3002{{ $user-&gt;name }}\u8868\u793a\u8f93\u51fa$user\u6a21\u578b\u7684name\u5b57\u6bb5\u3002 \u63a7\u5236\u5668\uff08Controller\uff09\u63a7\u5236\u5668\u5c42\u662fLaravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u5904\u7406\u7528\u6237\u8bf7\u6c42\u548c\u63a7\u5236\u4e1a\u52a1\u903b\u8f91\u7684\u90e8\u5206\u3002\u5728Laravel\u4e2d\uff0c\u63a7\u5236\u5668\u901a\u5e38\u662f\u4e00\u4e2a\u7c7b\uff0c\u5305\u542b\u591a\u4e2a\u52a8\u4f5c\uff08Action\uff09\u65b9\u6cd5\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u63a7\u5236\u5668\u793a\u4f8b\uff0c\u7528\u4e8e\u5904\u7406\u7528\u6237\u5217\u8868\u7684\u8bf7\u6c42\uff1a &lt;?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; [&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-24286","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24286","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=24286"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24286\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}