{"id":24309,"date":"2024-11-21T09:51:10","date_gmt":"2024-11-21T01:51:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24309\/"},"modified":"2024-11-21T09:51:10","modified_gmt":"2024-11-21T01:51:10","slug":"thinkphp6%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1%e8%b0%83%e5%ba%a6%ef%bc%9a%e5%ae%9a%e6%97%b6%e6%89%a7%e8%a1%8c%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24309\/","title":{"rendered":"ThinkPHP6\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff1a\u5b9a\u65f6\u6267\u884c\u4efb\u52a1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169182532517739.jpg\" class=\"aligncenter\" title=\"ThinkPHP6\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff1a\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u63d2\u56fe\" alt=\"ThinkPHP6\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff1a\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u63d2\u56fe\" \/><\/p>\n<p>ThinkPHP6\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff1a\u5b9a\u65f6\u6267\u884c\u4efb\u52a1<\/p>\n<p>\u4e00\u3001\u7b80\u4ecb<br \/>\u5728Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5b9a\u671f\u6267\u884c\u67d0\u4e9b\u91cd\u590d\u6027\u4efb\u52a1\u7684\u60c5\u51b5\u3002ThinkPHP6\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\uff0c\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u7684\u9700\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff0c\u4ee5\u53ca\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<p>\u4e8c\u3001\u914d\u7f6e\u5b9a\u65f6\u4efb\u52a1<\/p>\n<ol>\n<li>\u521b\u5efa\u5b9a\u65f6\u4efb\u52a1\u6587\u4ef6<br \/>\u5728\u9879\u76ee\u7684app\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2acommand\u76ee\u5f55\uff0c\u5e76\u5728\u8be5\u76ee\u5f55\u4e0b\u518d\u521b\u5efa\u4e00\u4e2aCron\u76ee\u5f55\u3002\u5728Cron\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2aTest.php\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c06\u4f5c\u4e3a\u6211\u4eec\u7684\u5b9a\u65f6\u4efb\u52a1\u6587\u4ef6\u3002<\/li>\n<li>\u7f16\u5199\u5b9a\u65f6\u4efb\u52a1\u4ee3\u7801<br \/>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9a\u65f6\u4efb\u52a1\u4ee3\u7801\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>namespace appcommandCron;\n\nuse thinkconsoleCommand;\nuse thinkconsoleInput;\nuse thinkconsoleOutput;\n\nclass Test extends Command\n{\n    protected function configure()\n    {\n        $this-&amp;gt;setName('cron:test')-&amp;gt;setDescription('This is a test command.');\n    }\n\n    protected function execute(Input $input, Output $output)\n    {\n        \/\/ \u8fd9\u91cc\u662f\u5b9a\u65f6\u4efb\u52a1\u8981\u6267\u884c\u7684\u4ee3\u7801\n        $output-&amp;gt;writeln('This is a test command.');\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\u4e2a\u7ee7\u627f\u81eaCommand\u7c7b\u7684Test\u7c7b\uff0c\u91cd\u5199\u4e86configure\u548cexecute\u65b9\u6cd5\u3002\u5728configure\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4e3a\u8fd9\u4e2a\u547d\u4ee4\u6307\u5b9a\u4e86\u4e00\u4e2a\u540d\u79f0cron:test\u548c\u4e00\u4e2a\u63cf\u8ff0&#8221;This is a test command.&#8221;\u3002\u5728execute\u65b9\u6cd5\u4e2d\uff0c\u7f16\u5199\u4e86\u5b9a\u65f6\u4efb\u52a1\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0c\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u5730\u6253\u5370\u4e86\u4e00\u6761\u6d88\u606f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u6ce8\u518c\u5b9a\u65f6\u4efb\u52a1\u547d\u4ee4<br \/>\u5728\u9879\u76ee\u7684console\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2aCron.php\u6587\u4ef6\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u6ce8\u518c\u6211\u4eec\u7684\u5b9a\u65f6\u4efb\u52a1\u547d\u4ee4\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>namespace appconsole;\n\nuse thinkconsoleCommand;\nuse thinkconsoleInput;\nuse thinkconsoleOutput;\nuse thinkconsoleSchedule;\n\nclass Cron extends Command\n{\n    protected function configure()\n    {\n        $this-&amp;gt;setName('cron')-&amp;gt;setDescription('Task schedule for ThinkPHP6.');\n    }\n\n    protected function execute(Input $input, Output $output)\n    {\n        $schedule = new Schedule();\n\n        \/\/ \u8fd9\u91cc\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\n        $schedule-&amp;gt;command('cron:test')-&amp;gt;everyMinute();\n\n        $schedule-&amp;gt;run();\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\u4e2a\u7ee7\u627f\u81eaCommand\u7c7b\u7684Cron\u7c7b\uff0c\u91cd\u5199\u4e86configure\u548cexecute\u65b9\u6cd5\u3002\u5728configure\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4e3a\u8fd9\u4e2a\u547d\u4ee4\u6307\u5b9a\u4e86\u4e00\u4e2a\u540d\u79f0cron\u548c\u4e00\u4e2a\u63cf\u8ff0&#8221;Task schedule for ThinkPHP6.&#8221;\u3002\u5728execute\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aSchedule\u5b9e\u4f8b\uff0c\u5e76\u4f7f\u7528\u5176command\u65b9\u6cd5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u5b9a\u5b9a\u65f6\u4efb\u52a1\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p>\u4e09\u3001\u8fd0\u884c\u5b9a\u65f6\u4efb\u52a1<br \/>\u8981\u8fd0\u884c\u5b9a\u65f6\u4efb\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a<\/p>\n<pre>php think cron<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u4f1a\u6267\u884c\u6211\u4eec\u5728Cron.php\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n<p>\u56db\u3001\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u793a\u4f8b<br \/>\u4e0b\u9762\u662f\u4e00\u4e9b\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u7684\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\n<p>\u6bcf\u5206\u949f\u6267\u884c\u4e00\u6b21\u4efb\u52a1<\/p>\n<pre>$schedule-&amp;gt;command('cron:test')-&amp;gt;everyMinute();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u6bcf\u5929\u51cc\u66680\u70b9\u6267\u884c\u4efb\u52a1<\/p>\n<pre>$schedule-&amp;gt;command('cron:test')-&amp;gt;daily();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u6bcf\u5468\u65e5\u51cc\u66680\u70b9\u6267\u884c\u4efb\u52a1<\/p>\n<pre>$schedule-&amp;gt;command('cron:test')-&amp;gt;weekly()-&amp;gt;sundays();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u6bcf\u67081\u53f7\u51cc\u66680\u70b9\u6267\u884c\u4efb\u52a1<\/p>\n<pre>$schedule-&amp;gt;command('cron:test')-&amp;gt;monthly();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\u3002\u901a\u8fc7\u914d\u7f6e\u5b9a\u65f6\u4efb\u52a1\u6587\u4ef6\u548c\u6ce8\u518c\u5b9a\u65f6\u4efb\u52a1\u547d\u4ee4\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u66f4\u8be6\u7ec6\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\u8bf7\u53c2\u8003ThinkPHP6\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff1a\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\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>ThinkPHP6\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff1a\u5b9a\u65f6\u6267\u884c\u4efb\u52a1 \u4e00\u3001\u7b80\u4ecb\u5728Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5b9a\u671f\u6267\u884c\u67d0\u4e9b\u91cd\u590d\u6027\u4efb\u52a1\u7684\u60c5\u51b5\u3002ThinkPHP6\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\uff0c\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u7684\u9700\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\uff0c\u4ee5\u53ca\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u5e2e\u52a9\u7406\u89e3\u3002 \u4e8c\u3001\u914d\u7f6e\u5b9a\u65f6\u4efb\u52a1 \u521b\u5efa\u5b9a\u65f6\u4efb\u52a1\u6587\u4ef6\u5728\u9879\u76ee\u7684app\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2acommand\u76ee\u5f55\uff0c\u5e76\u5728\u8be5\u76ee\u5f55\u4e0b\u518d\u521b\u5efa\u4e00\u4e2aCron\u76ee\u5f55\u3002\u5728Cron\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2aTest.php\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c06\u4f5c\u4e3a\u6211\u4eec\u7684\u5b9a\u65f6\u4efb\u52a1\u6587\u4ef6\u3002 \u7f16\u5199\u5b9a\u65f6\u4efb\u52a1\u4ee3\u7801\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9a\u65f6\u4efb\u52a1\u4ee3\u7801\u793a\u4f8b\uff1a namespace appcommandCron; use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleOutput; class Test extends Command { protected function configure() { $this-&amp;gt;setName(&#8216;cron:test&#8217;)-&amp;gt;setDescription(&#8216;This is a test command.&#8217;); } protected function execute(Input $input, Output $output) { \/\/ \u8fd9\u91cc\u662f\u5b9a\u65f6\u4efb\u52a1\u8981\u6267\u884c\u7684\u4ee3\u7801 $output-&amp;gt;writeln(&#8216;This is a test command.&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ee7\u627f\u81eaCommand\u7c7b\u7684Test\u7c7b\uff0c\u91cd\u5199\u4e86configure\u548cexecute\u65b9\u6cd5\u3002\u5728configure\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4e3a\u8fd9\u4e2a\u547d\u4ee4\u6307\u5b9a\u4e86\u4e00\u4e2a\u540d\u79f0cron:test\u548c\u4e00\u4e2a\u63cf\u8ff0&#8221;This is a test command.&#8221;\u3002\u5728execute\u65b9\u6cd5\u4e2d\uff0c\u7f16\u5199\u4e86\u5b9a\u65f6\u4efb\u52a1\u8981\u6267\u884c\u7684\u4ee3\u7801\uff0c\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u5730\u6253\u5370\u4e86\u4e00\u6761\u6d88\u606f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6ce8\u518c\u5b9a\u65f6\u4efb\u52a1\u547d\u4ee4\u5728\u9879\u76ee\u7684console\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2aCron.php\u6587\u4ef6\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u6ce8\u518c\u6211\u4eec\u7684\u5b9a\u65f6\u4efb\u52a1\u547d\u4ee4\u3002\u4ee3\u7801\u5982\u4e0b\uff1a namespace appconsole; use [&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-24309","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24309","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=24309"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24309\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}