{"id":441506,"date":"2025-04-27T15:32:23","date_gmt":"2025-04-27T07:32:23","guid":{"rendered":"https:\/\/www.idc.net\/help\/?p=441506"},"modified":"2025-04-27T15:32:23","modified_gmt":"2025-04-27T07:32:23","slug":"php%e4%b8%ad%e8%87%aa%e5%ae%9a%e4%b9%89%e8%b7%af%e7%94%b1%e5%ae%9e%e7%8e%b0%e8%af%a6%e8%a7%a3%ef%bc%9a%e6%8f%90%e5%8d%87%e7%bd%91%e7%ab%99%e7%81%b5%e6%b4%bb%e6%80%a7%e4%b8%8e%e6%89%a9%e5%b1%95","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/441506\/","title":{"rendered":"PHP\u4e2d\u81ea\u5b9a\u4e49\u8def\u7531\u5b9e\u73b0\u8be6\u89e3\uff1a\u63d0\u5347\u7f51\u7ad9\u7075\u6d3b\u6027\u4e0e\u6269\u5c55\u6027"},"content":{"rendered":"<h2>1. \u8def\u7531\u7cfb\u7edf\u7684\u57fa\u7840\u6982\u5ff5<\/h2>\n<p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u8def\u7531\uff08Routing\uff09\u662f\u6307\u5c06\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684URL\u6620\u5c04\u5230\u670d\u52a1\u5668\u7aef\u5bf9\u5e94\u5904\u7406\u903b\u8f91\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u8bbf\u95ee <code>\/about<\/code> \u9875\u9762\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u8c03\u7528 <code>AboutController<\/code> \u5904\u7406\u8bf7\u6c42\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u8def\u7531\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u4e3b\u5b9a\u4e49\u6e05\u6670\u3001\u53cb\u597d\u7684URL\u89c4\u5219\uff0c\u800c\u975e\u4ec5\u4f9d\u8d56\u670d\u52a1\u5668\u9ed8\u8ba4\u8def\u5f84\uff0c\u5927\u5e45\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u4e0e\u641c\u7d22\u5f15\u64ce\u53cb\u597d\u5ea6\u3002<\/p>\n<hr \/>\n<h2>2. \u81ea\u5b9a\u4e49\u8def\u7531\u7684\u91cd\u8981\u6027<\/h2>\n<h3>2.1 \u4f18\u5316URL\u7ed3\u6784\u4e0e\u63d0\u5347SEO\u6392\u540d<\/h3>\n<p>\u5c06 <code>\/product?id=123<\/code> \u4f18\u5316\u4e3a <code>\/product\/123<\/code>\uff0c\u4e0d\u4ec5\u8ba9URL\u66f4\u76f4\u89c2\uff0c\u4e5f\u6781\u5927\u6539\u5584\u4e86\u641c\u7d22\u5f15\u64ce\u4f18\u5316\uff08SEO\uff09\uff0c\u5bf9\u63d0\u5347\u7f51\u7ad9\u81ea\u7136\u6d41\u91cf\u5177\u6709\u663e\u8457\u6548\u679c\u3002<\/p>\n<h3>2.2 \u5f3a\u5316\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027<\/h3>\n<p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u8def\u7531\u5b9e\u73b0\u4e1a\u52a1\u903b\u8f91\u4e0eURL\u7684\u5206\u79bb\uff0c\u4f7f\u5f97\u4fee\u6539\u8def\u5f84\u65e0\u9700\u66f4\u6539\u6838\u5fc3\u7a0b\u5e8f\uff0c\u6781\u5927\u63d0\u5347\u9879\u76ee\u7684\u53ef\u6269\u5c55\u6027\u548c\u540e\u671f\u7ef4\u62a4\u7684\u6548\u7387\u3002<\/p>\n<h3>2.3 \u652f\u6301RESTful API\u5f00\u53d1<\/h3>\n<p>\u81ea\u5b9a\u4e49\u8def\u7531\u662f\u5b9e\u73b0RESTful API\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u73af\uff0c\u901a\u8fc7\u4e0d\u540cHTTP\u65b9\u6cd5\uff08GET\u3001POST\u3001PUT\u3001DELETE\uff09\u4e0eURL\u7ec4\u5408\uff0c\u53ef\u4ee5\u5feb\u901f\u642d\u5efa\u9ad8\u6548\u7684API\u7cfb\u7edf\u3002<\/p>\n<hr \/>\n<h2>3. \u624b\u52a8\u5b9e\u73b0PHP\u81ea\u5b9a\u4e49\u8def\u7531<\/h2>\n<h3>3.1 \u89e3\u6790\u7528\u6237\u8bf7\u6c42<\/h3>\n<p>\u9996\u5148\uff0c\u4ece\u670d\u52a1\u5668\u53d8\u91cf\u4e2d\u63d0\u53d6\u7528\u6237\u8bf7\u6c42\u7684URL\u8def\u5f84\uff1a<\/p>\n<pre><code class=\"language-php\">$requestUri = $_SERVER['REQUEST_URI'];\r\n$parsedUrl = parse_url($requestUri);\r\n$path = $parsedUrl['path'];\r\n<\/code><\/pre>\n<hr \/>\n<h3>3.2 \u5b9a\u4e49\u8def\u7531\u89c4\u5219<\/h3>\n<p>\u4f7f\u7528\u6570\u7ec4\u5b9a\u4e49\u6e05\u6670\u7684\u8def\u7531\u89c4\u5219\uff1a<\/p>\n<pre><code class=\"language-php\">$routes = [\r\n    'GET \/about' =&gt; 'AboutController@index',\r\n    'GET \/product\/{id}' =&gt; 'ProductController@show',\r\n    'POST \/product' =&gt; 'ProductController@store',\r\n];\r\n<\/code><\/pre>\n<hr \/>\n<h3>3.3 \u5339\u914d\u5e76\u8c03\u5ea6\u8bf7\u6c42<\/h3>\n<p>\u901a\u8fc7\u904d\u5386\u8def\u7531\u89c4\u5219\u5339\u914d\u5f53\u524d\u8bf7\u6c42\uff0c\u8c03\u7528\u76f8\u5e94\u5904\u7406\u903b\u8f91\uff1a<\/p>\n<pre><code class=\"language-php\">foreach ($routes as $route =&gt; $handler) {\r\n    list($method, $pattern) = explode(' ', $route, 2);\r\n\r\n    \/\/ \u7b80\u5355\u7684\u6b63\u5219\u5339\u914d\r\n    if ($_SERVER['REQUEST_METHOD'] === $method &amp;&amp; preg_match('#^' . preg_replace('#\\{[^\\}]+\\}#', '([^\/]+)', $pattern) . '$#', $path, $matches)) {\r\n        list($controller, $action) = explode('@', $handler);\r\n        call_user_func_array([new $controller, $action], array_slice($matches, 1));\r\n        return;\r\n    }\r\n}\r\n\r\n\/\/ \u5339\u914d\u5931\u8d25\u65f6\u8fd4\u56de404\r\nheader(\"HTTP\/1.0 404 Not Found\");\r\necho '404 Not Found';\r\n<\/code><\/pre>\n<hr \/>\n<h3>3.4 \u5904\u7406\u52a8\u6001\u53c2\u6570<\/h3>\n<p>\u52a8\u6001URL\u4e2d\u7684\u53c2\u6570\u5982 <code>{id}<\/code> \u53ef\u76f4\u63a5\u63d0\u53d6\u5e76\u4f20\u9012\u7ed9\u63a7\u5236\u5668\uff1a<\/p>\n<pre><code class=\"language-php\">class ProductController {\r\n    public function show($id) {\r\n        echo \"\u6b63\u5728\u663e\u793a\u4ea7\u54c1\u7f16\u53f7\uff1a$id\";\r\n    }\r\n}\r\n<\/code><\/pre>\n<hr \/>\n<h2>4. \u62d3\u5c55\u529f\u80fd\uff1a\u8ba9\u8def\u7531\u66f4\u52a0\u5f3a\u5927<\/h2>\n<h3>4.1 \u8def\u7531\u5206\u7ec4\u7ba1\u7406<\/h3>\n<p>\u5c06\u529f\u80fd\u76f8\u4f3c\u7684\u8def\u7531\u5f52\u7c7b\uff0c\u4fbf\u4e8e\u7ef4\u62a4\uff1a<\/p>\n<pre><code class=\"language-php\">$userRoutes = [\r\n    'GET \/user\/profile' =&gt; 'UserController@profile',\r\n    'POST \/user\/update' =&gt; 'UserController@update',\r\n];\r\n<\/code><\/pre>\n<hr \/>\n<h3>4.2 \u96c6\u6210\u4e2d\u95f4\u4ef6<\/h3>\n<p>\u4e2d\u95f4\u4ef6\u5982\u8ba4\u8bc1\u3001\u65e5\u5fd7\u3001\u9650\u6d41\u7b49\u529f\u80fd\u53ef\u4ee5\u5728\u8def\u7531\u524d\u540e\u6267\u884c\uff1a<\/p>\n<pre><code class=\"language-php\">$routes = [\r\n    'GET \/admin' =&gt; ['AdminController@index', 'AuthMiddleware'],\r\n];\r\n<\/code><\/pre>\n<hr \/>\n<h3>4.3 \u8def\u7531\u7f13\u5b58\u63d0\u5347\u6027\u80fd<\/h3>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u8bbf\u95ee\u901f\u5ea6\uff0c\u53ef\u5c06\u89e3\u6790\u597d\u7684\u8def\u7531\u5199\u5165\u7f13\u5b58\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-php\">if (file_exists('routes.cache')) {\r\n    $routes = include 'routes.cache';\r\n} else {\r\n    file_put_contents('routes.cache', '&lt;?php return ' . var_export($routes, true) . ';');\r\n}\r\n<\/code><\/pre>\n<hr \/>\n<h2>5. \u603b\u7ed3\u4e0e\u63a8\u8350<\/h2>\n<p>\u901a\u8fc7\u5b66\u4e60\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u4e0d\u4ec5\u80fd\u63d0\u9ad8PHP\u9879\u76ee\u7684\u7075\u6d3b\u6027\uff0c\u8fd8\u80fd\u638c\u63e1\u642d\u5efaRESTful API\u4e0e\u4e2d\u95f4\u4ef6\u67b6\u6784\u7684\u6838\u5fc3\u6280\u672f\u3002\u867d\u7136\u50cfLaravel\u3001Symfony\u7b49PHP\u6846\u67b6\u5df2\u7ecf\u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u8def\u7531\u7cfb\u7edf\uff0c\u4f46\u638c\u63e1\u5e95\u5c42\u673a\u5236\u5bf9\u4e8e\u63d0\u5347\u4e2a\u4eba\u6280\u672f\u6df1\u5ea6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<p>\u4e3a\u4e86\u66f4\u597d\u5730\u90e8\u7f72\u60a8\u7684PHP\u5e94\u7528\uff0c\u9009\u62e9\u4e00\u53f0\u7a33\u5b9a\u9ad8\u6548\u7684\u670d\u52a1\u5668\u5c24\u4e3a\u5173\u952e\u3002\u63a8\u8350\u4f7f\u7528<strong>\u9999\u6e2f\u670d\u52a1\u5668<\/strong>\u3001<strong>\u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/strong>\u3001<strong>\u9999\u6e2fVPS<\/strong>\u4ea7\u54c1\uff0c\u4f4e\u5ef6\u8fdf\u3001\u9ad8\u53ef\u7528\uff0c\u4e3a\u60a8\u7684\u4e1a\u52a1\u62d3\u5c55\u4fdd\u9a7e\u62a4\u822a\uff01<\/p>\n<hr \/>\n<p>\u2705 <strong>\u5173\u952e\u8bcd\u603b\u7ed3<\/strong>\uff1a\u9999\u6e2f\u670d\u52a1\u5668\u3001\u9999\u6e2fVPS\u3001\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u3001\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668\u3001PHP\u81ea\u5b9a\u4e49\u8def\u7531\u3001RESTful API\u5f00\u53d1\u3001\u670d\u52a1\u5668\u79df\u7528<\/p>\n<p>\u5982\u679c\u4f60\u9700\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u9999\u6e2f\u670d\u52a1\u5668\u7684\u65b9\u6848\u4e0e\u4ef7\u683c\uff0c\u6b22\u8fce\u8bbf\u95ee \u6211\u4eec<a href=\"https:\/\/idc.net\/\">\u5b98\u7f51<\/a>\uff0c\u4f53\u9a8c\u6781\u901f\u7a33\u5b9a\u7684\u7f51\u7edc\u73af\u5883\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u8def\u7531\u7cfb\u7edf\u7684\u57fa\u7840\u6982\u5ff5 \u5728Web\u5f00\u53d1\u4e2d\uff0c\u8def\u7531\uff08Routing\uff09\u662f\u6307\u5c06\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684URL\u6620\u5c04\u5230\u670d\u52a1\u5668\u7aef\u5bf9\u5e94\u5904\u7406 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[197851],"tags":[],"class_list":["post-441506","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=441506"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441506\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=441506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=441506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=441506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}