{"id":985,"date":"2019-08-21T15:51:45","date_gmt":"2019-08-21T07:51:45","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=985"},"modified":"2023-12-04T16:42:22","modified_gmt":"2023-12-04T08:42:22","slug":"codeigniter7-%e8%b7%af%e7%94%b1%e7%b1%bbrouter-php","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=985","title":{"rendered":"CodeIgniter(7)\u2014\u2014\u8def\u7531\u7c7bRouter.php"},"content":{"rendered":"<p>CI\u6846\u67b6\u80fd\u6839\u636eURL\u5730\u5740\u8def\u7531\u5230\u6b63\u786e\u7684\u63a7\u5236\u5668\u548c\u65b9\u6cd5\uff0c\u4e3b\u8981\u662f\u9760URI\u548cRouter\u8fd9\u4e24\u4e2a\u7ec4\u4ef6\uff0cURI\u7ec4\u4ef6\u5bf9URL\u5730\u5740\u6240\u643a\u5e26\u7684\u53c2\u6570\u8fdb\u884c\u5206\u6bb5\uff0cRouter\u7ec4\u4ef6\u518d\u6839\u636eURI\u89e3\u6790\u51fa\u6765\u7684\u5206\u6bb5\u53c2\u6570\u627e\u5230\u5bf9\u5e94\u7684\u63a7\u5236\u5668\uff0c\u65b9\u6cd5\uff0c\u53ca\u8c03\u7528\u65b9\u6cd5\u6240\u9700\u7684\u53c2\u6570\uff0c\u5e76\u4e14Router\u8fd8\u80fd\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u81ea\u5b9a\u4e49\u8def\u7531\u65b9\u5f0f\u3002 \u5728\u9605\u8bfb\u8def\u7531\u7ec4\u4ef6\u7684\u6e90\u7801\u65f6\uff0c\u6211\u4eec\u9700\u8981\u601d\u8003\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a 1.Router\u662f\u600e\u6837\u6839\u636eURI\u7684\u5206\u6bb5\u53c2\u6570\u627e\u5230\u5bf9\u5e94\u7684\u7c7b\u548c\u65b9\u6cd5\uff1f 2.\u81ea\u5b9a\u4e49\u8def\u7531\u662f\u600e\u6837\u5b9e\u73b0\u7684\uff1f \u5e26\u7740\u4ee5\u4e0a\u51e0\u4e2a\u95ee\u9898\u6211\u4eec\u5f00\u59cb\u9605\u8bfbRouter\u7684\u6e90\u7801\uff1a<\/p>\n<h3>1.\u6784\u9020\u51fd\u6570<\/h3>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Class constructor\n     *\n     * Runs the route mapping function.\n     *\n     * @param   array   $routing\n     * @return  void\n     *\/\n    public function __construct($routing = NULL)\n    {\n        $this-&gt;config =&amp; load_class('Config', 'core');\n        $this-&gt;uri =&amp; load_class('URI', 'core');\n\n        $this-&gt;enable_query_strings = ( ! is_cli() &amp;&amp; $this-&gt;config-&gt;item('enable_query_strings') === TRUE);\n\n        \/\/ If a directory override is configured, it has to be set before any dynamic routing logic\n        is_array($routing) &amp;&amp; isset($routing['directory']) &amp;&amp; $this-&gt;set_directory($routing['directory']);\n        $this-&gt;_set_routing();\n\n        \/\/ Set any routing overrides that may exist in the main index file\n        if (is_array($routing))\n        {\n            empty($routing['controller']) OR $this-&gt;set_class($routing['controller']);\n            empty($routing['function'])   OR $this-&gt;set_method($routing['function']);\n        }\n\n        log_message('info', 'Router Class Initialized');\n    }\n<\/code><\/pre>\n<p>_set_routing()\u51fd\u6570\u624d\u662f\u771f\u6b63\u5b9e\u73b0\u8def\u7531\u7684\u5730\u65b9\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u6211\u4eec\u5177\u4f53\u770b\u770b\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<h3>2._set_routing()<\/h3>\n<p>_set_routing()\u8fd9\u4e2a\u51fd\u6570\u662f\u4e3b\u8981\u5b9e\u73b0\u8def\u7531\u7684\u5730\u65b9\uff0c\u5b83\u53ef\u4ee5\u6839\u636euri\u8bf7\u6c42\u786e\u5b9a\u8def\u7531\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u5b9e\u73b0\u5728\u8def\u7531\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u7684\u8def\u7531\u65b9\u5f0f \u8fd9\u4e2a\u51fd\u6570\u7684\u4ee3\u7801\u53ef\u4ee5\u5206\u4e3a\u4e09\u90e8\u5206\u6765\u9605\u8bfb\uff1a 1.\u52a0\u8f7droutes\u914d\u7f6e\u6587\u4ef6 2.query_string\u8def\u7531\u65b9\u5f0f 3.\u975equery_string\u8def\u7531\u65b9\u5f0f<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Set route mapping\n     *\n     * Determines what should be served based on the URI request,\n     * as well as any \"routes\" that have been set in the routing config file.\n     *\n     * @return  void\n     *\/\n    protected function _set_routing()\n    {\n        \/\/ Load the routes.php file. It would be great if we could\n        \/\/ skip this for enable_query_strings = TRUE, but then\n        \/\/ default_controller would be empty ...\n        if (file_exists(APPPATH.'config\/routes.php'))\n        {\n            include(APPPATH.'config\/routes.php');\n        }\n\n        if (file_exists(APPPATH.'config\/'.ENVIRONMENT.'\/routes.php'))\n        {\n            include(APPPATH.'config\/'.ENVIRONMENT.'\/routes.php');\n        }\n\n        \/\/ Validate &amp; get reserved routes\n        if (isset($route) &amp;&amp; is_array($route))\n        {\n            isset($route['default_controller']) &amp;&amp; $this-&gt;default_controller = $route['default_controller'];\n            isset($route['translate_uri_dashes']) &amp;&amp; $this-&gt;translate_uri_dashes = $route['translate_uri_dashes'];\n            unset($route['default_controller'], $route['translate_uri_dashes']);\n            $this-&gt;routes = $route;\n        }\n\n        \/\/ Are query strings enabled in the config file? Normally CI doesn't utilize query strings\n        \/\/ since URI segments are more search-engine friendly, but they can optionally be used.\n        \/\/ If this feature is enabled, we will gather the directory\/class\/method a little differently\n        if ($this-&gt;enable_query_strings)\n        {\n            \/\/ If the directory is set at this time, it means an override exists, so skip the checks\n            if ( ! isset($this-&gt;directory))\n            {\n                $_d = $this-&gt;config-&gt;item('directory_trigger');\n                $_d = isset($_GET[$_d]) ? trim($_GET[$_d], \" \\t\\n\\r\\0\\x0B\/\") : '';\n\n                if ($_d !== '')\n                {\n                    $this-&gt;uri-&gt;filter_uri($_d);\n                    $this-&gt;set_directory($_d);\n                }\n            }\n\n            $_c = trim($this-&gt;config-&gt;item('controller_trigger'));\n            if ( ! empty($_GET[$_c]))\n            {\n                $this-&gt;uri-&gt;filter_uri($_GET[$_c]);\n                $this-&gt;set_class($_GET[$_c]);\n\n                $_f = trim($this-&gt;config-&gt;item('function_trigger'));\n                if ( ! empty($_GET[$_f]))\n                {\n                    $this-&gt;uri-&gt;filter_uri($_GET[$_f]);\n                    $this-&gt;set_method($_GET[$_f]);\n                }\n\n                $this-&gt;uri-&gt;rsegments = array(\n                    1 =&gt; $this-&gt;class,\n                    2 =&gt; $this-&gt;method\n                );\n            }\n            else\n            {\n                $this-&gt;_set_default_controller();\n            }\n\n            \/\/ Routing rules don't apply to query strings and we don't need to detect\n            \/\/ directories, so we're done here\n            return;\n        }\n\n        \/\/ Is there anything to parse?\n        if ($this-&gt;uri-&gt;uri_string !== '')\n        {\n            $this-&gt;_parse_routes();\n        }\n        else\n        {\n            $this-&gt;_set_default_controller();\n        }\n    }\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u901a\u8fc7query_string\u53c2\u6570\u8def\u7531\u7684\u5b9e\u73b0\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a 1.\u53d6\u51faconfig\u914d\u7f6e\u4e2d\u7684directory_trigger\uff0ccontroller_trigger\uff0cfunction_trigger\u4e09\u4e2a\u53c2\u6570 2.\u53d6\u51faquery_string\u4e2d\u53c2\u6570\u540d\u4e3a\u4ee5\u4e0a\u4e09\u4e2a\u914d\u7f6e\u9879\u53c2\u6570\u7684\u503c 3.\u5c06\u5bf9\u5e94\u7684query_string\u53c2\u6570\u5b58\u50a8\u5230\u5bf9\u5e94\u7684\u53d8\u91cf\u4e2d 4.\u5c06\u8def\u7531\u53c2\u6570\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8d4b\u503c\u7ed9uri->rsegments\u53d8\u91cf\u4e2d\uff0c\u8def\u7531\u5b8c\u6210<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u770b\u4e00\u4e0b_parse_toutes()\u51fd\u6570\uff0c\u770b\u5176\u4ed6\u60c5\u51b5\u662f\u5982\u4f55\u5b9e\u73b0\u8def\u7531\u7684<\/p>\n<h3>3._parse_routes()<\/h3>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5b9e\u73b0\u7684\u8def\u7531\u65b9\u5f0f\u5c31\u662f\u6211\u4eec\u5e73\u5e38\u5e38\u89c1\u7684ci\u8def\u7531\u5730\u5740\uff0c\u4f8b\u5982\uff1ahttp:\/\/dump.com\/test\/t_fun\uff0c\u4f1a\u8def\u7531\u5230test\u63a7\u5236\u5668\u7684t_fun\u65b9\u6cd5\u3002 \u5148\u770b\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Parse Routes\n     *\n     * Matches any routes that may exist in the config\/routes.php file\n     * against the URI to determine if the class\/method need to be remapped.\n     *\n     * @return  void\n     *\/\n    protected function _parse_routes()\n    {\n        \/\/ Turn the segment array into a URI string\n        $uri = implode('\/', $this-&gt;uri-&gt;segments);\n\n        \/\/ Get HTTP verb\n        $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';\n\n        \/\/ Loop through the route array looking for wildcards\n        foreach ($this-&gt;routes as $key =&gt; $val)\n        {\n            \/\/ Check if route format is using HTTP verbs\n            if (is_array($val))\n            {\n                $val = array_change_key_case($val, CASE_LOWER);\n                if (isset($val[$http_verb]))\n                {\n                    $val = $val[$http_verb];\n                }\n                else\n                {\n                    continue;\n                }\n            }\n\n            \/\/ Convert wildcards to RegEx\n            $key = str_replace(array(':any', ':num'), array('[^\/]+', '[0-9]+'), $key);\n\n            \/\/ Does the RegEx match?\n            if (preg_match('#^'.$key.'$#', $uri, $matches))\n            {\n                \/\/ Are we using callbacks to process back-references?\n                if ( ! is_string($val) &amp;&amp; is_callable($val))\n                {\n                    \/\/ Remove the original string from the matches array.\n                    array_shift($matches);\n\n                    \/\/ Execute the callback using the values in matches as its parameters.\n                    $val = call_user_func_array($val, $matches);\n                }\n                \/\/ Are we using the default routing method for back-references?\n                elseif (strpos($val, '$') !== FALSE &amp;&amp; strpos($key, '(') !== FALSE)\n                {\n                    $val = preg_replace('#^'.$key.'$#', $val, $uri);\n                }\n\n                $this-&gt;_set_request(explode('\/', $val));\n                return;\n            }\n        }\n\n        \/\/ If we got this far it means we didn't encounter a\n        \/\/ matching route so we'll set the site default route\n        $this-&gt;_set_request(array_values($this-&gt;uri-&gt;segments));\n    }\n<\/code><\/pre>\n<p>\u5230\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u7b2c\u4e8c\u4e2a\u95ee\u9898\u4e86\uff0cRouter\u7528routes\u914d\u7f6e\u7684key\u5339\u914duri\uff0c\u7528key\u5bf9\u5e94\u7684\u503c\u6307\u5b9a\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8c03\u7528\u51fd\u6570\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528\u6b63\u5219\u66ff\u6362\uff0c\u4ee5\u6b64\u5b9e\u73b0\u4e86\u81ea\u5b9a\u4e49\u8def\u7531\u683c\u5f0f\u3002 \u81ea\u5b9a\u4e49\u8def\u7531\u683c\u5f0f\u6700\u7ec8\u8fd8\u662f\u8981\u88ab\u8f6c\u6362\u6210\u548cURI\u4e2d\u5206\u6bb5\u53c2\u6570\u4e00\u6837\u7684\u683c\u5f0f\uff0c\u518d\u7531set_request\u51fd\u6570\u5904\u7406 \u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0bset_request\u51fd\u6570\u7684\u6e90\u7801<\/p>\n<h3>4._set_request<\/h3>\n<pre><code class=\"language-php line-numbers\">\/**\n     * Set request route\n     *\n     * Takes an array of URI segments as input and sets the class\/method\n     * to be called.\n     *\n     * @used-by CI_Router::_parse_routes()\n     * @param   array   $segments   URI segments\n     * @return  void\n     *\/\n    protected function _set_request($segments = array())\n    {\n        $segments = $this-&gt;_validate_request($segments);\n        \/\/ If we don't have any segments left - try the default controller;\n        \/\/ WARNING: Directories get shifted out of the segments array!\n        if (empty($segments))\n        {\n            $this-&gt;_set_default_controller();\n            return;\n        }\n\n        if ($this-&gt;translate_uri_dashes === TRUE)\n        {\n            $segments[0] = str_replace('-', '_', $segments[0]);\n            if (isset($segments[1]))\n            {\n                $segments[1] = str_replace('-', '_', $segments[1]);\n            }\n        }\n\n        $this-&gt;set_class($segments[0]);\n        if (isset($segments[1]))\n        {\n            $this-&gt;set_method($segments[1]);\n        }\n        else\n        {\n            $segments[1] = 'index';\n        }\n\n        array_unshift($segments, NULL);\n        unset($segments[0]);\n        $this-&gt;uri-&gt;rsegments = $segments;\n    }\n<\/code><\/pre>\n<p>\u5230\u8fd9\u91cc\u6574\u4e2a\u8def\u7531\u90fd\u5b9e\u73b0\u4e86\uff0csegments\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u63a7\u5236\u5668\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u65b9\u6cd5 \u8fd9\u91cc\u6709\u51e0\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1a rsegments\u7684\u6570\u7ec4\u4e0b\u6807\u4e5f\u662f\u4ece1\u5f00\u59cb\u7684\uff0c\u548cURI\u7684segments\u4e00\u6837\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CI\u6846\u67b6\u80fd\u6839\u636eURL\u5730\u5740\u8def\u7531\u5230\u6b63\u786e\u7684\u63a7\u5236\u5668\u548c\u65b9\u6cd5\uff0c\u4e3b\u8981\u662f\u9760URI\u548cRouter\u8fd9\u4e24\u4e2a\u7ec4\u4ef6\uff0cURI\u7ec4\u4ef6\u5bf9URL\u5730 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[405],"class_list":["post-985","post","type-post","status-publish","format-standard","hentry","category-15","tag-php"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=985"}],"version-history":[{"count":1,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions"}],"predecessor-version":[{"id":986,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/985\/revisions\/986"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}