{"id":22404,"date":"2024-11-21T12:37:07","date_gmt":"2024-11-21T04:37:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22404\/"},"modified":"2024-11-21T12:37:07","modified_gmt":"2024-11-21T04:37:07","slug":"%e8%af%a6%e7%bb%86%e4%bb%8b%e7%bb%8dlaravel-action%e7%9a%84%e7%bc%96%e5%86%99%e6%ad%a5%e9%aa%a4%e5%92%8c%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22404\/","title":{"rendered":"\u8be6\u7ec6\u4ecb\u7ecdLaravel Action\u7684\u7f16\u5199\u6b65\u9aa4\u548c\u5b9e\u8df5"},"content":{"rendered":"<p>laravel\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684php\u5f00\u53d1\u6846\u67b6\uff0c\u5176\u51ed\u501f\u5176\u7b80\u6d01\u800c\u5f3a\u5927\u7684api\u548c\u5168\u9762\u7684\u5de5\u5177\u5305\u800c\u5907\u53d7\u6b22\u8fce\u3002\u5728laravel\u4e2d\uff0c\u201cactions\u201d\u662f\u4e00\u79cd\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u66f4\u52a0\u8f7b\u677e\u5730\u6784\u5efa\u63a7\u5236\u5668\u548c\u7b80\u5316\u4f60\u7684\u903b\u8f91\u5c42\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u4ecb\u7ecdlaravel action\u5982\u4f55\u7f16\u5199\u7684\u6b65\u9aa4\u548c\u5b9e\u8df5\u3002<\/p>\n<ol>\n<li>\u521b\u5efaAction\u7c7b<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2aAction\u7c7b\u3002\u5728Laravel 8\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Artisan\u547d\u4ee4\u6765\u521b\u5efa\uff1a<\/p>\n<pre>php artisan make:action MyAction<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728app\/Action\u76ee\u5f55\u4e0b\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u6587\u4ef6\u201cMyAction.php\u201d\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199Action\u7684\u903b\u8f91\u4e86\u3002<\/p>\n<ol>\n<li>\u7f16\u5199Action\u7684\u903b\u8f91<\/li>\n<\/ol>\n<p>\u5728\u4f60\u7684Action\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4e3a\u63a7\u5236\u5668\u4e2d\u7684\u6bcf\u4e2a\u65b9\u6cd5\u5b9a\u4e49\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u6765\u5904\u7406\u7528\u6237\u767b\u5f55\u7684\u903b\u8f91\u3002<\/p>\n<pre>&lt;?php\n\nnamespace AppActions;\n\nuse IlluminateSupportFacadesAuth;\n\nclass LoginUserAction\n{\n    public function execute(array $credentials)\n    {\n        $attempt = Auth::attempt($credentials);\n\n        if (!$attempt) {\n            throw new Exception('Invalid login credentials');\n        }\n\n        return Auth::user();\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aLoginUserAction\u7684Action\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u5305\u542b\u7528\u6237\u767b\u5f55\u51ed\u636e\u7684\u6570\u7ec4\uff0c\u6267\u884c\u767b\u5f55\u5c1d\u8bd5\uff0c\u5982\u679c\u6709\u9519\u8bef\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u5426\u5219\u8fd4\u56de\u8ba4\u8bc1\u6210\u529f\u7684\u7528\u6237\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4f7f\u7528Laravel\u7684Auth\u95e8\u9762\u6765\u6267\u884c\u767b\u5f55\u64cd\u4f5c\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u8f7b\u677e\u5730\u5229\u7528Laravel\u7684\u8ba4\u8bc1\u7cfb\u7edf\u3002<\/p>\n<ol>\n<li>\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528Action<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2aAction\u7c7b\u5e76\u5b9a\u4e49\u4e86\u5176\u903b\u8f91\u3002\u4e0b\u4e00\u6b65\u662f\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n<p>\u4f7f\u7528Action\u7684\u7b2c\u4e00\u6b65\u662f\u6253\u5f00\u63a7\u5236\u5668\u5e76\u6dfb\u52a0\u4e00\u4e2ause\u8bed\u53e5\uff0c\u4ee5\u5f15\u5165Action\u7c7b\uff1a<\/p>\n<pre>&lt;?php\n\nnamespace AppHttpControllers;\n\nuse AppActionsLoginUserAction;\nuse IlluminateHttpRequest;\n\nclass AuthController extends Controller\n{\n    public function login(Request $request, LoginUserAction $loginAction)\n    {\n        $credentials = $request-&gt;only(['email', 'password']);\n\n        try {\n            $user = $loginAction-&gt;execute($credentials);\n        } catch (Exception $e) {\n            return response()-&gt;json([\n                'message' =&gt; $e-&gt;getMessage()\n            ], 401);\n        }\n\n        return response()-&gt;json(compact('user'));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4ece\u6211\u4eec\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u5f15\u7528\u4e86 LoginUserAction\u3002\u7136\u540e\u6211\u4eec\u6dfb\u52a0\u4e86 login() \u65b9\u6cd5\uff0c\u5176\u9996\u4e2a\u53c2\u6570\u662f Laravel \u7684 Request \u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6211\u4eec\u4e0a\u9762\u521b\u5efa\u7684 LoginUserAction \u5b9e\u4f8b\u3002<\/p>\n<p>\u5728 login() \u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4e5f\u6839\u636e\u8bf7\u6c42\u7684\u5185\u5bb9\u63d0\u53d6\u51fa email \u548c password\uff0c\u63a5\u7740\u8c03\u7528\u6211\u4eec\u7684 Action \u901a\u8fc7 $credentials \u7684\u53c2\u6570\u6765\u6267\u884c\u903b\u8f91\u3002\u5982\u679c\u6210\u529f\uff0c\u6211\u4eec\u5c06\u7528\u6237\u4ee5 JSON \u683c\u5f0f\u8fd4\u56de\uff0c\u5426\u5219\u6211\u4eec\u5c06\u9519\u8bef\u4fe1\u606f\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<ol>\n<li>\u8c03\u7528Action<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u6211\u4eec\u7684login()\u65b9\u6cd5\uff0c\u5e76\u8c03\u7528LoginUserAction\u7c7b\u4e2d\u7684execute\u65b9\u6cd5\u3002\u6211\u4eec\u5c06\u5728HTTP\u8bf7\u6c42\u4e2d\u4f20\u9012\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u4e86\u4e00\u4e9b\u767b\u5f55\u51ed\u8bc1\u4fe1\u606f\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p>\u5728\u8def\u7531\u4e2d\u5b9a\u4e49\u5bf9\u5e94\u7684\u767b\u5f55\u8def\u7531\uff1a<\/p>\n<pre>&lt;?php\n\nuse IlluminateSupportFacadesRoute;\nuse AppHttpControllersAuthController;\n\nRoute::post('\/login', [AuthController::class, 'login'])-&gt;name('login');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u6211\u4eec\u7684laravel\u5e94\u7528\u7684\/login\u8def\u7531\uff0c\u4f1a\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>POST \/login HTTP\/1.1\nHost: localhost:8000\nContent-Type: application\/json\n\n{\n    \"email\": \"user@example.com\",\n    \"password\": \"my_password\"\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u89e6\u53d1\u6211\u4eec\u5728\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u7684 login() \u65b9\u6cd5\uff0c\u8c03\u7528 LoginUserAction\uff0c\u4f20\u5165 email \u548c password \u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>Laravel Action \u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u3001\u5e72\u51c0\u7684\u65b9\u6cd5\u6765\u7ec4\u7ec7\u4f60\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u901a\u8fc7\u5c06\u4e1a\u52a1\u903b\u8f91\u4ece\u63a7\u5236\u5668\u4e2d\u79fb\u51fa\u5e76\u653e\u5165 Action \u7c7b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5e26\u6765\u5f88\u591a\u4ee3\u7801\u91cd\u7528\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u63d0\u5347\u3002<\/p>\n<p>\u5728\u4f7f\u7528 Laravel Action \u65f6\uff0c\u4f60\u5e94\u8be5\u9075\u5faa\u4ee5\u4e0b\u4e24\u6761\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u65b9\u6cd5\u5e94\u8be5\u5c3d\u91cf\u53ea\u505a\u4e00\u4ef6\u4e8b\u60c5\u3002<\/li>\n<li>\u907f\u514d\u5728 Action \u7c7b\u4e2d\u76f4\u63a5\u8bbf\u95ee\u5916\u90e8\u7c7b\u6216\u5bf9\u8c61\uff0c\u4f7f\u7528\u4f20\u9012\u53d8\u91cf\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5171\u901a\u548c\u91cd\u590d\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728 Laravel 8 \u4e2d\u5f15\u5165\u4e86 Action \u529f\u80fd\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u8f83\u8001\u7248\u672c\u7684 Laravel\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u521b\u5efa Action \u7c7b\uff0c\u5e76\u5c06\u7c7b\u6587\u4ef6\u5b58\u50a8\u5728\u4e00\u4e2a\u5408\u9002\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u5728\u63a7\u5236\u5668\u4e2d\u624b\u52a8\u5b9e\u4f8b\u5316\u548c\u8c03\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8be6\u7ec6\u4ecb\u7ecdLaravel Action\u7684\u7f16\u5199\u6b65\u9aa4\u548c\u5b9e\u8df5\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\u4e2a\u975e\u5e38\u6d41\u884c\u7684php\u5f00\u53d1\u6846\u67b6\uff0c\u5176\u51ed\u501f\u5176\u7b80\u6d01\u800c\u5f3a\u5927\u7684api\u548c\u5168\u9762\u7684\u5de5\u5177\u5305\u800c\u5907\u53d7\u6b22\u8fce\u3002\u5728laravel\u4e2d\uff0c\u201cactions\u201d\u662f\u4e00\u79cd\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u66f4\u52a0\u8f7b\u677e\u5730\u6784\u5efa\u63a7\u5236\u5668\u548c\u7b80\u5316\u4f60\u7684\u903b\u8f91\u5c42\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u4ecb\u7ecdlaravel action\u5982\u4f55\u7f16\u5199\u7684\u6b65\u9aa4\u548c\u5b9e\u8df5\u3002 \u521b\u5efaAction\u7c7b \u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2aAction\u7c7b\u3002\u5728Laravel 8\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Artisan\u547d\u4ee4\u6765\u521b\u5efa\uff1a php artisan make:action MyAction \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u5728app\/Action\u76ee\u5f55\u4e0b\u4e3a\u4f60\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7c7b\u6587\u4ef6\u201cMyAction.php\u201d\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199Action\u7684\u903b\u8f91\u4e86\u3002 \u7f16\u5199Action\u7684\u903b\u8f91 \u5728\u4f60\u7684Action\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4e3a\u63a7\u5236\u5668\u4e2d\u7684\u6bcf\u4e2a\u65b9\u6cd5\u5b9a\u4e49\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u6765\u5904\u7406\u7528\u6237\u767b\u5f55\u7684\u903b\u8f91\u3002 &lt;?php namespace AppActions; use IlluminateSupportFacadesAuth; class LoginUserAction { public function execute(array $credentials) { $attempt = Auth::attempt($credentials); if (!$attempt) { throw new Exception(&#8216;Invalid login credentials&#8217;); } return Auth::user(); } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aLoginUserAction\u7684Action\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u5305\u542b\u7528\u6237\u767b\u5f55\u51ed\u636e\u7684\u6570\u7ec4\uff0c\u6267\u884c\u767b\u5f55\u5c1d\u8bd5\uff0c\u5982\u679c\u6709\u9519\u8bef\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u5426\u5219\u8fd4\u56de\u8ba4\u8bc1\u6210\u529f\u7684\u7528\u6237\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4f7f\u7528Laravel\u7684Auth\u95e8\u9762\u6765\u6267\u884c\u767b\u5f55\u64cd\u4f5c\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u8f7b\u677e\u5730\u5229\u7528Laravel\u7684\u8ba4\u8bc1\u7cfb\u7edf\u3002 \u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528Action \u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2aAction\u7c7b\u5e76\u5b9a\u4e49\u4e86\u5176\u903b\u8f91\u3002\u4e0b\u4e00\u6b65\u662f\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528\u5b83\u3002 \u4f7f\u7528Action\u7684\u7b2c\u4e00\u6b65\u662f\u6253\u5f00\u63a7\u5236\u5668\u5e76\u6dfb\u52a0\u4e00\u4e2ause\u8bed\u53e5\uff0c\u4ee5\u5f15\u5165Action\u7c7b\uff1a &lt;?php namespace AppHttpControllers; use AppActionsLoginUserAction; use IlluminateHttpRequest; [&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-22404","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22404","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=22404"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22404\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}