{"id":22305,"date":"2024-11-21T15:02:16","date_gmt":"2024-11-21T07:02:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22305\/"},"modified":"2024-11-21T15:02:16","modified_gmt":"2024-11-21T07:02:16","slug":"thinkphp%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e6%89%8b%e6%9c%ba%e8%ae%bf%e9%97%ae%e4%b8%8d%e5%90%8c%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22305\/","title":{"rendered":"thinkphp\u600e\u4e48\u5b9e\u73b0\u624b\u673a\u8bbf\u95ee\u4e0d\u540c\u8def\u5f84"},"content":{"rendered":"<p>\u968f\u7740\u79fb\u52a8\u8bbe\u5907\u7684\u666e\u53ca\uff0c\u8bbf\u95ee\u7f51\u7ad9\u7684\u65b9\u5f0f\u4e5f\u53d8\u5f97\u591a\u6837\u5316\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fthinkphp\u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\uff0c\u5e76\u5e0c\u671b\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u8bbf\u95ee\u4e0d\u540c\u7684\u8def\u5f84\uff0c\u672c\u7bc7\u6587\u7ae0\u5c06\u63d0\u4f9b\u4e00\u4e9b\u5e2e\u52a9\u3002<\/p>\n<p>ThinkPHP\u6846\u67b6\u652f\u6301\u8def\u7531\u529f\u80fd\uff0c\u4f7f\u5f97\u4e0d\u540c\u7684URL\u53ef\u4ee5\u6307\u5411\u76f8\u5e94\u7684\u63a7\u5236\u5668\u548c\u64cd\u4f5c\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u624b\u673a\u548cPC\u7aef\u7684\u4e0d\u540c\u8bbf\u95ee\u8def\u5f84\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u624b\u673a\u8bbf\u95ee\u7684\u8def\u7531\u89c4\u5219\u3002\u5728ThinkPHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Route\u7c7b\u6765\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728route.php\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>use thinkacadeRoute;\n\nRoute::rule('\/m\/:controller\/:action', 'ppmobilecontroller[:controller][:action]');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u8def\u7531\u89c4\u5219\u8868\u793a\uff0c\u8bbf\u95ee\u201c\/m\/\u63a7\u5236\u5668\u540d\/\u64cd\u4f5c\u65b9\u6cd5\u540d\u201d\u8def\u5f84\u65f6\uff0c\u5c06\u4f1a\u8c03\u7528appmobilecontroller\u63a7\u5236\u5668\u540d\u64cd\u4f5c\u65b9\u6cd5\u540d\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728mobile\u76ee\u5f55\u4e0b\u521b\u5efa\u76f8\u5e94\u7684\u63a7\u5236\u5668\u7c7b\u548c\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2aIndex\u63a7\u5236\u5668\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e2ahello\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>namespace appmobilecontroller;\n\nclass Index\n{\n    public function hello()\n    {\n        return 'Hello, mobile world!';\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u8bbf\u95ee\u201c\/m\/index\/hello\u201d\u8def\u5f84\uff0c\u5373\u53ef\u770b\u5230\u201cHello, mobile world!\u201d\u7684\u8f93\u51fa\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u5728PC\u7aef\u4e5f\u80fd\u591f\u8bbf\u95ee\u540c\u6837\u7684\u94fe\u63a5\uff0c\u53ef\u4ee5\u5728PC\u7aef\u589e\u52a0\u4e00\u4e2a\u8def\u7531\u89c4\u5219\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>use thinkacadeRoute;\n\n\/\/ PC\u7aef\u8def\u7531\u89c4\u5219\nRoute::rule('\/:controller\/:action', 'ppindexcontroller[:controller][:action]');\n\n\/\/ \u624b\u673a\u7aef\u8def\u7531\u89c4\u5219\nRoute::rule('\/m\/:controller\/:action', 'ppmobilecontroller[:controller][:action]');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\uff0c\u5f53\u8bbf\u95ee\u201c\/index\/hello\u201d\u8def\u5f84\u65f6\uff0c\u4e5f\u4f1a\u8c03\u7528Index\u63a7\u5236\u5668\u7684hello\u65b9\u6cd5\u3002<\/p>\n<p>\u9664\u4e86\u4f7f\u7528\u8def\u7531\u89c4\u5219\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5224\u65ad\u7528\u6237\u8bbf\u95ee\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u518d\u8fdb\u884c\u5206\u522b\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace appindexcontroller;\n\nclass Index\n{\n    public function __construct()\n    {\n        \/\/ \u68c0\u6d4b\u7528\u6237\u8bbe\u5907\u7c7b\u578b\n        if ($this-&gt;isMobile()) {\n            \/\/ \u624b\u673a\u8bbf\u95ee\n            $this-&gt;mobile();\n        } else {\n            \/\/ PC\u8bbf\u95ee\n            $this-&gt;pc();\n        }\n    }\n\n    protected function isMobile()\n    {\n        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);\n        \n        $is_mobile = (strpos($agent, 'mobile') !== false) || (strpos($agent, 'android') !== false);\n\n        return $is_mobile;\n    }\n\n    protected function mobile()\n    {\n        \/\/ \u624b\u673a\u8bbf\u95ee\u5904\u7406\n        echo 'Hello, mobile world!';\n    }\n\n    protected function pc()\n    {\n        \/\/ PC\u8bbf\u95ee\u5904\u7406\n        echo 'Hello, world!';\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u68c0\u6d4b\u7528\u6237\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u5982\u679c\u662f\u79fb\u52a8\u8bbe\u5907\u5219\u8c03\u7528mobile\u65b9\u6cd5\uff0c\u5426\u5219\u8c03\u7528pc\u65b9\u6cd5\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86PHP\u5185\u7f6e\u7684$_SERVER[&#8216;HTTP_USER_AGENT&#8217;]\u53d8\u91cf\u6765\u83b7\u53d6\u7528\u6237\u7684\u8bbe\u5907\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5e76\u4e0d\u591f\u51c6\u786e\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u53d8\u91cf\u53ef\u4ee5\u7531\u7528\u6237\u4f2a\u9020\u3002\u5982\u679c\u9700\u8981\u66f4\u51c6\u786e\u5730\u5224\u65ad\u8bbe\u5907\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216API\uff0c\u5982WURFL\u6216User-Agent-API\u3002<\/p>\n<p>\u603b\u7ed3\u8d77\u6765\uff0c\u4ee5\u4e0a\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5728ThinkPHP\u4e2d\u5b9e\u73b0\u624b\u673a\u8bbf\u95ee\u4e0d\u540c\u8def\u5f84\u7684\u65b9\u5f0f\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u5f0f\u3002\u540c\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4fdd\u62a4\u7528\u6237\u9690\u79c1\u548c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u4f8b\u5982\u4e0d\u6536\u96c6\u7528\u6237\u9690\u79c1\uff0c\u4f18\u5316\u79fb\u52a8\u8bbe\u5907\u8bbf\u95ee\u901f\u5ea6\u7b49\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u600e\u4e48\u5b9e\u73b0\u624b\u673a\u8bbf\u95ee\u4e0d\u540c\u8def\u5f84\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>\u968f\u7740\u79fb\u52a8\u8bbe\u5907\u7684\u666e\u53ca\uff0c\u8bbf\u95ee\u7f51\u7ad9\u7684\u65b9\u5f0f\u4e5f\u53d8\u5f97\u591a\u6837\u5316\u3002\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fthinkphp\u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\uff0c\u5e76\u5e0c\u671b\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u8bbf\u95ee\u4e0d\u540c\u7684\u8def\u5f84\uff0c\u672c\u7bc7\u6587\u7ae0\u5c06\u63d0\u4f9b\u4e00\u4e9b\u5e2e\u52a9\u3002 ThinkPHP\u6846\u67b6\u652f\u6301\u8def\u7531\u529f\u80fd\uff0c\u4f7f\u5f97\u4e0d\u540c\u7684URL\u53ef\u4ee5\u6307\u5411\u76f8\u5e94\u7684\u63a7\u5236\u5668\u548c\u64cd\u4f5c\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u624b\u673a\u548cPC\u7aef\u7684\u4e0d\u540c\u8bbf\u95ee\u8def\u5f84\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u624b\u673a\u8bbf\u95ee\u7684\u8def\u7531\u89c4\u5219\u3002\u5728ThinkPHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Route\u7c7b\u6765\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728route.php\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a use thinkacadeRoute; Route::rule(&#8216;\/m\/:controller\/:action&#8217;, &#8216;ppmobilecontroller[:controller][:action]&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u8def\u7531\u89c4\u5219\u8868\u793a\uff0c\u8bbf\u95ee\u201c\/m\/\u63a7\u5236\u5668\u540d\/\u64cd\u4f5c\u65b9\u6cd5\u540d\u201d\u8def\u5f84\u65f6\uff0c\u5c06\u4f1a\u8c03\u7528appmobilecontroller\u63a7\u5236\u5668\u540d\u64cd\u4f5c\u65b9\u6cd5\u540d\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728mobile\u76ee\u5f55\u4e0b\u521b\u5efa\u76f8\u5e94\u7684\u63a7\u5236\u5668\u7c7b\u548c\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2aIndex\u63a7\u5236\u5668\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e00\u4e2ahello\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b namespace appmobilecontroller; class Index { public function hello() { return &#8216;Hello, mobile world!&#8217;; } } \u767b\u5f55\u540e\u590d\u5236 \u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u79fb\u52a8\u8bbe\u5907\u4e0a\u8bbf\u95ee\u201c\/m\/index\/hello\u201d\u8def\u5f84\uff0c\u5373\u53ef\u770b\u5230\u201cHello, mobile world!\u201d\u7684\u8f93\u51fa\u3002 \u5982\u679c\u60a8\u5e0c\u671b\u5728PC\u7aef\u4e5f\u80fd\u591f\u8bbf\u95ee\u540c\u6837\u7684\u94fe\u63a5\uff0c\u53ef\u4ee5\u5728PC\u7aef\u589e\u52a0\u4e00\u4e2a\u8def\u7531\u89c4\u5219\uff0c\u5982\u4e0b\u6240\u793a\uff1a use thinkacadeRoute; \/\/ PC\u7aef\u8def\u7531\u89c4\u5219 Route::rule(&#8216;\/:controller\/:action&#8217;, &#8216;ppindexcontroller[:controller][:action]&#8217;); \/\/ \u624b\u673a\u7aef\u8def\u7531\u89c4\u5219 Route::rule(&#8216;\/m\/:controller\/:action&#8217;, &#8216;ppmobilecontroller[:controller][:action]&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\uff0c\u5f53\u8bbf\u95ee\u201c\/index\/hello\u201d\u8def\u5f84\u65f6\uff0c\u4e5f\u4f1a\u8c03\u7528Index\u63a7\u5236\u5668\u7684hello\u65b9\u6cd5\u3002 \u9664\u4e86\u4f7f\u7528\u8def\u7531\u89c4\u5219\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u63a7\u5236\u5668\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5224\u65ad\u7528\u6237\u8bbf\u95ee\u7684\u8bbe\u5907\u7c7b\u578b\uff0c\u518d\u8fdb\u884c\u5206\u522b\u5904\u7406\u3002\u4f8b\u5982\uff1a namespace appindexcontroller; class Index { public function __construct() { \/\/ \u68c0\u6d4b\u7528\u6237\u8bbe\u5907\u7c7b\u578b [&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-22305","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22305","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=22305"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22305\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}