{"id":23128,"date":"2024-11-21T14:56:15","date_gmt":"2024-11-21T06:56:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23128\/"},"modified":"2024-11-21T14:56:15","modified_gmt":"2024-11-21T06:56:15","slug":"thinkphp%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e8%b7%b3%e8%bd%ac%e9%a1%b5%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23128\/","title":{"rendered":"thinkphp\u600e\u4e48\u5b9e\u73b0\u8df3\u8f6c\u9875\u9762"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001\u4f7f\u7528redirect\u51fd\u6570\u8df3\u8f6c\u9875\u9762<\/strong><\/p>\n<p>\u5728\u4e2d\uff0c\u8df3\u8f6c\u5230\u5176\u4ed6\u9875\u9762\u53ef\u4ee5\u4f7f\u7528redirect\u51fd\u6570\u3002\u8be5\u51fd\u6570\u7684\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>public&nbsp;function&nbsp;redirect($url,&nbsp;$params&nbsp;=&nbsp;[],&nbsp;$code&nbsp;=&nbsp;302,&nbsp;$withPrefix&nbsp;=&nbsp;false)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c$url\u8868\u793a\u8981\u8df3\u8f6c\u7684\u9875\u9762\u8def\u5f84\uff0c$params\u8868\u793a\u8df3\u8f6c\u65f6\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570\uff0c$code\u8868\u793a\u8df3\u8f6c\u7684HTTP\u72b6\u6001\u7801\uff0c$withPrefix\u8868\u793a\u662f\u5426\u5e26\u4e0a\u57df\u540d\u524d\u7f00\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<p>1.\u8df3\u8f6c\u5230\u5176\u4ed6\u63a7\u5236\u5668\u7684\u65b9\u6cd5<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/&nbsp;\u8df3\u8f6c\u5230Home\u63a7\u5236\u5668\u7684index\u65b9\u6cd5\nreturn&nbsp;$this-&amp;gt;redirect('home\/index');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.\u8df3\u8f6c\u5230\u5916\u90e8URL<\/p>\n<pre>return&nbsp;$this-&amp;gt;redirect('http:\/\/www.example.com');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>3.\u5e26\u53c2\u6570\u8df3\u8f6c<\/p>\n<pre>\/\/&nbsp;\u8df3\u8f6c\u5230Home\u63a7\u5236\u5668\u7684detail\u65b9\u6cd5\uff0c\u5e76\u4f20\u9012id\u53c2\u6570\nreturn&nbsp;$this-&amp;gt;redirect('home\/detail',&nbsp;['id'&nbsp;=&amp;gt;&nbsp;1]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001\u4f7f\u7528url\u51fd\u6570\u751f\u6210\u8df3\u8f6c\u8def\u5f84<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528redirect\u51fd\u6570\u8df3\u8f6c\u9875\u9762\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528url\u51fd\u6570\u751f\u6210\u8df3\u8f6c\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528redirect\u51fd\u6570\u8fdb\u884c\u8df3\u8f6c\u3002url\u51fd\u6570\u7684\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>public&nbsp;function&nbsp;url($url&nbsp;=&nbsp;'',&nbsp;$vars&nbsp;=&nbsp;'',&nbsp;$suffix&nbsp;=&nbsp;true,&nbsp;$domain&nbsp;=&nbsp;false)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c$url\u8868\u793a\u8981\u751f\u6210\u7684URL\u5730\u5740\uff0c$vars\u8868\u793a\u8981\u4f20\u9012\u7684\u53c2\u6570\uff0c$suffix\u8868\u793a\u662f\u5426\u542f\u7528URL\u540e\u7f00\uff0c$domain\u8868\u793a\u662f\u5426\u5e26\u4e0a\u57df\u540d\u524d\u7f00\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528url\u51fd\u6570\u751f\u6210\u8df3\u8f6c\u8def\u5f84\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/&nbsp;\u751f\u6210Home\u63a7\u5236\u5668\u7684index\u65b9\u6cd5\u7684URL\n$url&nbsp;=&nbsp;$this-&amp;gt;url('home\/index');\nreturn&nbsp;$this-&amp;gt;redirect($url);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9e\u73b0\u8df3\u8f6c\u9875\u9762\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>\u4e00\u3001\u4f7f\u7528redirect\u51fd\u6570\u8df3\u8f6c\u9875\u9762 \u5728\u4e2d\uff0c\u8df3\u8f6c\u5230\u5176\u4ed6\u9875\u9762\u53ef\u4ee5\u4f7f\u7528redirect\u51fd\u6570\u3002\u8be5\u51fd\u6570\u7684\u7528\u6cd5\u5982\u4e0b\uff1a public&nbsp;function&nbsp;redirect($url,&nbsp;$params&nbsp;=&nbsp;[],&nbsp;$code&nbsp;=&nbsp;302,&nbsp;$withPrefix&nbsp;=&nbsp;false) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$url\u8868\u793a\u8981\u8df3\u8f6c\u7684\u9875\u9762\u8def\u5f84\uff0c$params\u8868\u793a\u8df3\u8f6c\u65f6\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570\uff0c$code\u8868\u793a\u8df3\u8f6c\u7684HTTP\u72b6\u6001\u7801\uff0c$withPrefix\u8868\u793a\u662f\u5426\u5e26\u4e0a\u57df\u540d\u524d\u7f00\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u4f7f\u7528\u8be5\u51fd\u6570\u7684\u793a\u4f8b\uff1a 1.\u8df3\u8f6c\u5230\u5176\u4ed6\u63a7\u5236\u5668\u7684\u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/&nbsp;\u8df3\u8f6c\u5230Home\u63a7\u5236\u5668\u7684index\u65b9\u6cd5 return&nbsp;$this-&amp;gt;redirect(&#8216;home\/index&#8217;); \u767b\u5f55\u540e\u590d\u5236 2.\u8df3\u8f6c\u5230\u5916\u90e8URL return&nbsp;$this-&amp;gt;redirect(&#8216;http:\/\/www.example.com&#8217;); \u767b\u5f55\u540e\u590d\u5236 3.\u5e26\u53c2\u6570\u8df3\u8f6c \/\/&nbsp;\u8df3\u8f6c\u5230Home\u63a7\u5236\u5668\u7684detail\u65b9\u6cd5\uff0c\u5e76\u4f20\u9012id\u53c2\u6570 return&nbsp;$this-&amp;gt;redirect(&#8216;home\/detail&#8217;,&nbsp;[&#8216;id&#8217;&nbsp;=&amp;gt;&nbsp;1]); \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u4f7f\u7528url\u51fd\u6570\u751f\u6210\u8df3\u8f6c\u8def\u5f84 \u9664\u4e86\u4f7f\u7528redirect\u51fd\u6570\u8df3\u8f6c\u9875\u9762\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528url\u51fd\u6570\u751f\u6210\u8df3\u8f6c\u8def\u5f84\uff0c\u7136\u540e\u4f7f\u7528redirect\u51fd\u6570\u8fdb\u884c\u8df3\u8f6c\u3002url\u51fd\u6570\u7684\u7528\u6cd5\u5982\u4e0b\uff1a public&nbsp;function&nbsp;url($url&nbsp;=&nbsp;&#8221;,&nbsp;$vars&nbsp;=&nbsp;&#8221;,&nbsp;$suffix&nbsp;=&nbsp;true,&nbsp;$domain&nbsp;=&nbsp;false) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0c$url\u8868\u793a\u8981\u751f\u6210\u7684URL\u5730\u5740\uff0c$vars\u8868\u793a\u8981\u4f20\u9012\u7684\u53c2\u6570\uff0c$suffix\u8868\u793a\u662f\u5426\u542f\u7528URL\u540e\u7f00\uff0c$domain\u8868\u793a\u662f\u5426\u5e26\u4e0a\u57df\u540d\u524d\u7f00\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528url\u51fd\u6570\u751f\u6210\u8df3\u8f6c\u8def\u5f84\u7684\u793a\u4f8b\uff1a \/\/&nbsp;\u751f\u6210Home\u63a7\u5236\u5668\u7684index\u65b9\u6cd5\u7684URL $url&nbsp;=&nbsp;$this-&amp;gt;url(&#8216;home\/index&#8217;); return&nbsp;$this-&amp;gt;redirect($url); \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5b9e\u73b0\u8df3\u8f6c\u9875\u9762\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-23128","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23128","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=23128"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23128\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}