{"id":866,"date":"2014-10-11T11:30:04","date_gmt":"2014-10-11T03:30:04","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=866"},"modified":"2014-10-14T11:21:36","modified_gmt":"2014-10-14T03:21:36","slug":"%e5%9f%b7%e8%a1%8c%e5%b7%a5%e4%bd%9c%ef%bc%9aroutingroute-php","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=866","title":{"rendered":"\u57f7\u884c\u5de5\u4f5c\uff1aRouting\/Route.php"},"content":{"rendered":"<p>\u8981\u8b80\u61c2\u6b64\u7bc7source code\uff0c\u9700\u8981\u5148\u77ad\u89e3Laravel\u63d0\u4f9b\u4e86\u54ea\u4e9b\u8c50\u5bcc\u7684routing\u65b9\u6cd5\uff1a<br \/>\n<a href=\"http:\/\/laravel.com\/docs\/4.2\/routing\" title=\"Routing\">Routing<\/a><br \/>\n\u4e00\u500broute\u7684\u4e3b\u8981\u5143\u7d20\u6709\u4e09\uff1a<br \/>\nmethod\uff1a\u9019\u500broute\u8655\u7406\u54ea\u4e9bHTTP Verb\uff1f(\u8b6c\u5982GET, POST, PUT &#8230; etc\uff0c\u53ef\u8907\u9078)<br \/>\nuri\uff1a\u9019\u500broute\u8655\u7406\u54ea\u7a2eURI\u7684pattern\uff1f(\u8ca0\u8cac\u7684\u7db2\u5740\u9577\u600e\u6a23)<br \/>\naction\uff1a\u9019\u500broute\u6703\u505a\u4ec0\u9ebc\u52d5\u4f5c<\/p>\n<pre>\r\n<?php namespace Illuminate\\Routing;\r\n\r\nuse Illuminate\\Http\\Request;\r\nuse Illuminate\\Routing\\Matching\\UriValidator;\r\nuse Illuminate\\Routing\\Matching\\HostValidator;\r\nuse Illuminate\\Routing\\Matching\\MethodValidator;\r\nuse Illuminate\\Routing\\Matching\\SchemeValidator;\r\nuse Symfony\\Component\\Routing\\Route as SymfonyRoute;\r\n\r\nclass Route {\r\n\r\n\t\/**\r\n\t * The URI pattern the route responds to.\r\n\t *\r\n\t * @var string\r\n\t *\/\r\n\tprotected $uri;\r\n\r\n\t\/**\r\n\t * The HTTP methods the route responds to.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $methods;\r\n\r\n\t\/**\r\n\t * The route action array.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $action;\r\n<\/pre>\n<p>$uri, $methods, $action\u4fbf\u70ba\u524d\u6587\u63d0\u5230\u7684\u4e09\u5143\u7d20\u3002<br \/>\n\u5176\u4e2d$action\u9663\u5217\u7d00\u9304\u4e00\u7cfb\u5217\u57f7\u884c\u6b64\u6b21route\u6703\u505a\u7684\u52d5\u4f5c\u3002<br \/>\n\u5305\u62ec\uff1aprefix, before, uses, after, domain, as, controller<br \/>\n\u5206\u5225\u7684\u610f\u7fa9\u70ba\uff1a<br \/>\nprefix\uff1a\u7528\u4ee5\u5be6\u4f5cRoute Prefixing\u529f\u80fd\uff0c\u53c3\u8003\uff1a<a href=\"http:\/\/stackoverflow.com\/questions\/14463344\/admin-routes-or-prefix-routes-in-laravel-4\" title=\"Admin Routes (or Prefix Routes) in Laravel 4\">Admin Routes (or Prefix Routes) in Laravel 4<\/a><br \/>\nbefore\uff1a\u57f7\u884c\u4e3b\u8981\u52d5\u4f5c\u524d\u5148\u57f7\u884c\u7684filter\u3002<br \/>\nuses\uff1a\u6b64route\u6703\u505a\u7684\u4e3b\u8981\u52d5\u4f5c<br \/>\nas\uff1a\u5e6b\u9019\u500broute\u547d\u540d\u3002\u4e4b\u5f8c\u7528URL::route\u7522\u751f\u7db2\u5740\u6bd4\u8f03\u65b9\u4fbf\u3002<br \/>\nafter\uff1a\u57f7\u884c\u4e3b\u8981\u52d5\u4f5c\u5f8c\u6703\u57f7\u884c\u7684filter\u3002<br \/>\ndomain\uff1a\u9019\u500broute\u8655\u7406\u54ea\u7a2edomain(\u5b50\u7db2\u57df)\u3002<br \/>\n\uff1f\uff1f\uff1f<br \/>\n<red><br \/>\nLaravel\u5f88\u591a\u7d30\u7bc0\u90fd\u62c6\u5206\u7684\u5f88\u6e05\u695a\u3001\u62c6\u51fa\u4e00\u5927\u5806\u7368\u7acb\u985e\u5225\uff0c\u9019\u500b\u5730\u65b9\u600e\u9ebc\u90a3\u9ebc\u6df7\u4e82\u3001\u5168\u90e8\u585e\u5728\u4e00\u500b\u9663\u5217\uff1f<br \/>\nbefore\u3001uses\u3001after\u662f\u4e09\u500b\u57f7\u884c\u52d5\u4f5c\uff0c\u653e\u4e00\u8d77\u4e5f\u5c31\u7b97\u4e86\uff0cprefix\u3001domain\u3001as \u3001controller\u4e5f\u653e\u5728\u9019\u662f\uff1f\u9019\u5e7e\u500b\u653e\u5728'action'\u88cf\u9762\u5408\u7406\u55ce\uff1f\u7368\u7acb\u70ba\u6210\u54e1\u8b8a\u6578\u6bd4\u8f03\u5408\u7406\u5427\u3002<br \/>\n<\/red><\/p>\n<pre>\r\n\t\/**\r\n\t * The default values for the route.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $defaults = array();\r\n\r\n\t\/**\r\n\t * The regular expression requirements.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $wheres = array();\r\n\r\n\t\/**\r\n\t * The array of matched parameters.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $parameters;\r\n\r\n\t\/**\r\n\t * The parameter names for the route.\r\n\t *\r\n\t * @var array|null\r\n\t *\/\r\n\tprotected $parameterNames;\r\n\r\n\t\/**\r\n\t * The compiled version of the route.\r\n\t *\r\n\t * @var \\Symfony\\Component\\Routing\\CompiledRoute\r\n\t *\/\r\n\tprotected $compiled;\r\n\r\n\t\/**\r\n\t * The validators used by the routes.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected static $validators;\r\n\r\n\t\/**\r\n\t * Create a new Route instance.\r\n\t *\r\n\t * @param  array   $methods\r\n\t * @param  string  $uri\r\n\t * @param  \\Closure|array  $action\r\n\t * @return void\r\n\t *\/\r\n\tpublic function __construct($methods, $uri, $action)\r\n\t{\r\n\t\t$this->uri = $uri;\r\n\t\t$this->methods = (array) $methods;\r\n\t\t$this->action = $this->parseAction($action);\r\n\r\n\t\tif (in_array('GET', $this->methods) && ! in_array('HEAD', $this->methods))\r\n\t\t{\r\n\t\t\t$this->methods[] = 'HEAD';\r\n\t\t}\r\n\r\n\t\tif (isset($this->action['prefix']))\r\n\t\t{\r\n\t\t\t$this->prefix($this->action['prefix']);\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>\u5efa\u69cb\u5f0f\u6703\u5c07\u4e09\u5143\u7d20\u7d66\u7d00\u9304\u8d77\u4f86\u3002<br \/>\n\u5efa\u69cb\u5f0f\u53ef\u4ee5\u63a5\u53d7$action\u70baclosure\u6216\u9663\u5217\uff0c\u82e5\u662fclosure\u7684\u8a71\uff0c\u6703\u5728parseAction\u5167\u88ab\u6574\u7406\u6210\u9663\u5217\u3002<br \/>\n\u5982\u679c\u6709\u8a2d\u5b9a\u8981\u8655\u7406GET\uff0c\u537b\u6c92\u6709\u8a2d\u5b9a\u8981\u8655\u7406HEAD\u7684\u8a71...\u90a3\u7576\u7136\u662f\u641e\u932f\u4e86\uff0c\u56e0\u70baHEAD\u662fGET\u7684\u5144\u5f1f\u3001\u5e6b\u5fd9\u52a0\u4e0a\u53bb\u5427\u3002\u53c3\u898b\uff1a<a href=\"https:\/\/ochronus.com\/http-head-request-good-uses\/\" title=\"What Is a HTTP HEAD Request Good for? Some Uses\">What Is a HTTP HEAD Request Good for? Some Uses<\/a><br \/>\n\u6700\u5f8c\u6aa2\u67e5\u662f\u5426\u6709\u8a2d\u5b9aprefix\uff0c\u6709\u7684\u8a71\u5c31\u7528prefix\u51fd\u5f0f\u53bb\u4fee\u6539$uri\u6210\u54e1\u8b8a\u6578\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Run the route action and return the response.\r\n\t *\r\n\t * @return mixed\r\n\t *\/\r\n\tpublic function run()\r\n\t{\r\n\t\t$parameters = array_filter($this->parameters(), function($p) { return isset($p); });\r\n\r\n\t\treturn call_user_func_array($this->action['uses'], $parameters);\r\n\t}\r\n<\/pre>\n<p>\u5c07\u8a2d\u5b9a\u597d\u7684\u53c3\u6578\u53d6\u51fa\uff0c\u4e1f\u7d66$action\u9663\u5217\u4e2d\u7d22\u5f15\u70ba'uses'\u7684\u52d5\u4f5c\u53bb\u57f7\u884c\u4e3b\u8981\u52d5\u4f5c\u3002<\/p>\n<pre>\r\n\r\n\t\/**\r\n\t * Determine if the route matches given request.\r\n\t *\r\n\t * @param  \\Illuminate\\Http\\Request  $request\r\n\t * @param  bool  $includingMethod\r\n\t * @return bool\r\n\t *\/\r\n\tpublic function matches(Request $request, $includingMethod = true)\r\n\t{\r\n\t\t$this->compileRoute();\r\n\r\n\t\tforeach ($this->getValidators() as $validator)\r\n\t\t{\r\n\t\t\tif ( ! $includingMethod && $validator instanceof MethodValidator) continue;\r\n\r\n\t\t\tif ( ! $validator->matches($this, $request)) return false;\r\n\t\t}\r\n\r\n\t\treturn true;\r\n\t}\r\n\r\n\t\/**\r\n\t * Compile the route into a Symfony CompiledRoute instance.\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tprotected function compileRoute()\r\n\t{\r\n\t\t$optionals = $this->extractOptionalParameters();\r\n\r\n\t\t$uri = preg_replace('\/\\{(\\w+?)\\?\\}\/', '{$1}', $this->uri);\r\n\r\n\t\t$this->compiled = with(\r\n\r\n\t\t\tnew SymfonyRoute($uri, $optionals, $this->wheres, array(), $this->domain() ?: '')\r\n\r\n\t\t)->compile();\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the optional parameters for the route.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tprotected function extractOptionalParameters()\r\n\t{\r\n\t\tpreg_match_all('\/\\{(\\w+?)\\?\\}\/', $this->uri, $matches);\r\n\r\n\t\treturn isset($matches[1]) ? array_fill_keys($matches[1], null) : [];\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the \"before\" filters for the route.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function beforeFilters()\r\n\t{\r\n\t\tif ( ! isset($this->action['before'])) return array();\r\n\r\n\t\treturn $this->parseFilters($this->action['before']);\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the \"after\" filters for the route.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function afterFilters()\r\n\t{\r\n\t\tif ( ! isset($this->action['after'])) return array();\r\n\r\n\t\treturn $this->parseFilters($this->action['after']);\r\n\t}\r\n\r\n\t\/**\r\n\t * Parse the given filter string.\r\n\t *\r\n\t * @param  string  $filters\r\n\t * @return array\r\n\t *\/\r\n\tpublic static function parseFilters($filters)\r\n\t{\r\n\t\treturn array_build(static::explodeFilters($filters), function($key, $value)\r\n\t\t{\r\n\t\t\treturn Route::parseFilter($value);\r\n\t\t});\r\n\t}\r\n\r\n\t\/**\r\n\t * Turn the filters into an array if they aren't already.\r\n\t *\r\n\t * @param  array|string  $filters\r\n\t * @return array\r\n\t *\/\r\n\tprotected static function explodeFilters($filters)\r\n\t{\r\n\t\tif (is_array($filters)) return static::explodeArrayFilters($filters);\r\n\r\n\t\treturn array_map('trim', explode('|', $filters));\r\n\t}\r\n\r\n\t\/**\r\n\t * Flatten out an array of filter declarations.\r\n\t *\r\n\t * @param  array  $filters\r\n\t * @return array\r\n\t *\/\r\n\tprotected static function explodeArrayFilters(array $filters)\r\n\t{\r\n\t\t$results = array();\r\n\r\n\t\tforeach ($filters as $filter)\r\n\t\t{\r\n\t\t\t$results = array_merge($results, array_map('trim', explode('|', $filter)));\r\n\t\t}\r\n\r\n\t\treturn $results;\r\n\t}\r\n\r\n\t\/**\r\n\t * Parse the given filter into name and parameters.\r\n\t *\r\n\t * @param  string  $filter\r\n\t * @return array\r\n\t *\/\r\n\tpublic static function parseFilter($filter)\r\n\t{\r\n\t\tif ( ! str_contains($filter, ':')) return array($filter, array());\r\n\r\n\t\treturn static::parseParameterFilter($filter);\r\n\t}\r\n\r\n\t\/**\r\n\t * Parse a filter with parameters.\r\n\t *\r\n\t * @param  string  $filter\r\n\t * @return array\r\n\t *\/\r\n\tprotected static function parseParameterFilter($filter)\r\n\t{\r\n\t\tlist($name, $parameters) = explode(':', $filter, 2);\r\n\r\n\t\treturn array($name, explode(',', $parameters));\r\n\t}\r\n\r\n\t\/**\r\n\t * Get a given parameter from the route.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @param  mixed   $default\r\n\t * @return string\r\n\t *\/\r\n\tpublic function getParameter($name, $default = null)\r\n\t{\r\n\t\treturn $this->parameter($name, $default);\r\n\t}\r\n\r\n\t\/**\r\n\t * Get a given parameter from the route.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @param  mixed   $default\r\n\t * @return string\r\n\t *\/\r\n\tpublic function parameter($name, $default = null)\r\n\t{\r\n\t\treturn array_get($this->parameters(), $name, $default);\r\n\t}\r\n\r\n\t\/**\r\n\t * Set a parameter to the given value.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @param  mixed   $value\r\n\t * @return void\r\n\t *\/\r\n\tpublic function setParameter($name, $value)\r\n\t{\r\n\t\t$this->parameters();\r\n\r\n\t\t$this->parameters[$name] = $value;\r\n\t}\r\n\r\n\t\/**\r\n\t * Unset a parameter on the route if it is set.\r\n\t *\r\n\t * @param  string  $name\r\n\t * @return void\r\n\t *\/\r\n\tpublic function forgetParameter($name)\r\n\t{\r\n\t\t$this->parameters();\r\n\r\n\t\tunset($this->parameters[$name]);\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the key \/ value list of parameters for the route.\r\n\t *\r\n\t * @return array\r\n\t *\r\n\t * @throws \\LogicException\r\n\t *\/\r\n\tpublic function parameters()\r\n\t{\r\n\t\tif (isset($this->parameters))\r\n\t\t{\r\n\t\t\treturn array_map(function($value)\r\n\t\t\t{\r\n\t\t\t\treturn is_string($value) ? rawurldecode($value) : $value;\r\n\r\n\t\t\t}, $this->parameters);\r\n\t\t}\r\n\r\n\t\tthrow new \\LogicException(\"Route is not bound.\");\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the key \/ value list of parameters without null values.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function parametersWithoutNulls()\r\n\t{\r\n\t\treturn array_filter($this->parameters(), function($p) { return ! is_null($p); });\r\n\t}\r\n\r\n\t\/**\r\n\t * Get all of the parameter names for the route.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function parameterNames()\r\n\t{\r\n\t\tif (isset($this->parameterNames)) return $this->parameterNames;\r\n\r\n\t\treturn $this->parameterNames = $this->compileParameterNames();\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the parameter names for the route.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tprotected function compileParameterNames()\r\n\t{\r\n\t\tpreg_match_all('\/\\{(.*?)\\}\/', $this->domain().$this->uri, $matches);\r\n\r\n\t\treturn array_map(function($m) { return trim($m, '?'); }, $matches[1]);\r\n\t}\r\n\r\n\t\/**\r\n\t * Bind the route to a given request for execution.\r\n\t *\r\n\t * @param  \\Illuminate\\Http\\Request  $request\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function bind(Request $request)\r\n\t{\r\n\t\t$this->compileRoute();\r\n\r\n\t\t$this->bindParameters($request);\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Extract the parameter list from the request.\r\n\t *\r\n\t * @param  \\Illuminate\\Http\\Request  $request\r\n\t * @return array\r\n\t *\/\r\n\tpublic function bindParameters(Request $request)\r\n\t{\r\n\t\t\/\/ If the route has a regular expression for the host part of the URI, we will\r\n\t\t\/\/ compile that and get the parameter matches for this domain. We will then\r\n\t\t\/\/ merge them into this parameters array so that this array is completed.\r\n\t\t$params = $this->matchToKeys(\r\n\r\n\t\t\tarray_slice($this->bindPathParameters($request), 1)\r\n\r\n\t\t);\r\n\r\n\t\t\/\/ If the route has a regular expression for the host part of the URI, we will\r\n\t\t\/\/ compile that and get the parameter matches for this domain. We will then\r\n\t\t\/\/ merge them into this parameters array so that this array is completed.\r\n\t\tif ( ! is_null($this->compiled->getHostRegex()))\r\n\t\t{\r\n\t\t\t$params = $this->bindHostParameters(\r\n\t\t\t\t$request, $params\r\n\t\t\t);\r\n\t\t}\r\n\r\n\t\treturn $this->parameters = $this->replaceDefaults($params);\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the parameter matches for the path portion of the URI.\r\n\t *\r\n\t * @param  \\Illuminate\\Http\\Request  $request\r\n\t * @return array\r\n\t *\/\r\n\tprotected function bindPathParameters(Request $request)\r\n\t{\r\n\t\tpreg_match($this->compiled->getRegex(), '\/'.$request->decodedPath(), $matches);\r\n\r\n\t\treturn $matches;\r\n\t}\r\n\r\n\t\/**\r\n\t * Extract the parameter list from the host part of the request.\r\n\t *\r\n\t * @param  \\Illuminate\\Http\\Request  $request\r\n\t * @param  array  $parameters\r\n\t * @return array\r\n\t *\/\r\n\tprotected function bindHostParameters(Request $request, $parameters)\r\n\t{\r\n\t\tpreg_match($this->compiled->getHostRegex(), $request->getHost(), $matches);\r\n\r\n\t\treturn array_merge($this->matchToKeys(array_slice($matches, 1)), $parameters);\r\n\t}\r\n\r\n\t\/**\r\n\t * Combine a set of parameter matches with the route's keys.\r\n\t *\r\n\t * @param  array  $matches\r\n\t * @return array\r\n\t *\/\r\n\tprotected function matchToKeys(array $matches)\r\n\t{\r\n\t\tif (count($this->parameterNames()) == 0) return array();\r\n\r\n\t\t$parameters = array_intersect_key($matches, array_flip($this->parameterNames()));\r\n\r\n\t\treturn array_filter($parameters, function($value)\r\n\t\t{\r\n\t\t\treturn is_string($value) && strlen($value) > 0;\r\n\t\t});\r\n\t}\r\n\r\n\t\/**\r\n\t * Replace null parameters with their defaults.\r\n\t *\r\n\t * @param  array  $parameters\r\n\t * @return array\r\n\t *\/\r\n\tprotected function replaceDefaults(array $parameters)\r\n\t{\r\n\t\tforeach ($parameters as $key => &$value)\r\n\t\t{\r\n\t\t\t$value = isset($value) ? $value : array_get($this->defaults, $key);\r\n\t\t}\r\n\r\n\t\treturn $parameters;\r\n\t}\r\n\r\n\t\/**\r\n\t * Parse the route action into a standard array.\r\n\t *\r\n\t * @param  callable|array  $action\r\n\t * @return array\r\n\t *\/\r\n\tprotected function parseAction($action)\r\n\t{\r\n\t\t\/\/ If the action is already a Closure instance, we will just set that instance\r\n\t\t\/\/ as the \"uses\" property, because there is nothing else we need to do when\r\n\t\t\/\/ it is available. Otherwise we will need to find it in the action list.\r\n\t\tif (is_callable($action))\r\n\t\t{\r\n\t\t\treturn array('uses' => $action);\r\n\t\t}\r\n\r\n\t\t\/\/ If no \"uses\" property has been set, we will dig through the array to find a\r\n\t\t\/\/ Closure instance within this list. We will set the first Closure we come\r\n\t\t\/\/ across into the \"uses\" property that will get fired off by this route.\r\n\t\telseif ( ! isset($action['uses']))\r\n\t\t{\r\n\t\t\t$action['uses'] = $this->findClosure($action);\r\n\t\t}\r\n\r\n\t\treturn $action;\r\n\t}\r\n\r\n\t\/**\r\n\t * Find the Closure in an action array.\r\n\t *\r\n\t * @param  array  $action\r\n\t * @return \\Closure\r\n\t *\/\r\n\tprotected function findClosure(array $action)\r\n\t{\r\n\t\treturn array_first($action, function($key, $value)\r\n\t\t{\r\n\t\t\treturn is_callable($value);\r\n\t\t});\r\n\t}\r\n<\/pre>\n<p>\u5728\u9663\u5217\u4e2d\u627e\u5230\u7b2c\u4e00\u500b\u53ef\u4ee5\u547c\u53eb\u7684\u5143\u7d20\u3002<br \/>\narray_first\u662fLaravel\u5b9a\u7fa9\u7684\u8f14\u52a9\u51fd\u6578\uff0c\u7528\u4f86\u627e\u5230\u7b2c\u4e00\u500b\u901a\u904e\u6e2c\u8a66\u7684\u5143\u7d20\uff1a<br \/>\nThe array_first method returns the first element of an array passing a given truth test.<br \/>\n\u7528\u5167\u5efa\u51fd\u6578is_callable\u6e2c\u8a66\u80fd\u5426\u547c\u53eb\u3002<\/p>\n<pre>\r\n\r\n\t\/**\r\n\t * Get the route validators for the instance.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic static function getValidators()\r\n\t{\r\n\t\tif (isset(static::$validators)) return static::$validators;\r\n\r\n\t\t\/\/ To match the route, we will use a chain of responsibility pattern with the\r\n\t\t\/\/ validator implementations. We will spin through each one making sure it\r\n\t\t\/\/ passes and then we will know if the route as a whole matches request.\r\n\t\treturn static::$validators = array(\r\n\t\t\tnew MethodValidator, new SchemeValidator,\r\n\t\t\tnew HostValidator, new UriValidator,\r\n\t\t);\r\n\t}\r\n\r\n\t\/**\r\n\t * Add before filters to the route.\r\n\t *\r\n\t * @param  string  $filters\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function before($filters)\r\n\t{\r\n\t\treturn $this->addFilters('before', $filters);\r\n\t}\r\n\r\n\t\/**\r\n\t * Add after filters to the route.\r\n\t *\r\n\t * @param  string  $filters\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function after($filters)\r\n\t{\r\n\t\treturn $this->addFilters('after', $filters);\r\n\t}\r\n\r\n\t\/**\r\n\t * Add the given filters to the route by type.\r\n\t *\r\n\t * @param  string  $type\r\n\t * @param  string  $filters\r\n\t * @return $this\r\n\t *\/\r\n\tprotected function addFilters($type, $filters)\r\n\t{\r\n\t\tif (isset($this->action[$type]))\r\n\t\t{\r\n\t\t\t$this->action[$type] .= '|'.$filters;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t$this->action[$type] = $filters;\r\n\t\t}\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set a default value for the route.\r\n\t *\r\n\t * @param  string  $key\r\n\t * @param  mixed  $value\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function defaults($key, $value)\r\n\t{\r\n\t\t$this->defaults[$key] = $value;\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set a regular expression requirement on the route.\r\n\t *\r\n\t * @param  array|string  $name\r\n\t * @param  string  $expression\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function where($name, $expression = null)\r\n\t{\r\n\t\tforeach ($this->parseWhere($name, $expression) as $name => $expression)\r\n\t\t{\r\n\t\t\t$this->wheres[$name] = $expression;\r\n\t\t}\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Parse arguments to the where method into an array.\r\n\t *\r\n\t * @param  array|string  $name\r\n\t * @param  string  $expression\r\n\t * @return array\r\n\t *\/\r\n\tprotected function parseWhere($name, $expression)\r\n\t{\r\n\t\treturn is_array($name) ? $name : array($name => $expression);\r\n\t}\r\n\r\n\t\/**\r\n\t * Set a list of regular expression requirements on the route.\r\n\t *\r\n\t * @param  array  $wheres\r\n\t * @return $this\r\n\t *\/\r\n\tprotected function whereArray(array $wheres)\r\n\t{\r\n\t\tforeach ($wheres as $name => $expression)\r\n\t\t{\r\n\t\t\t$this->where($name, $expression);\r\n\t\t}\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Add a prefix to the route URI.\r\n\t *\r\n\t * @param  string  $prefix\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function prefix($prefix)\r\n\t{\r\n\t\t$this->uri = trim($prefix, '\/').'\/'.trim($this->uri, '\/');\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the URI associated with the route.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tpublic function getPath()\r\n\t{\r\n\t\treturn $this->uri();\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the URI associated with the route.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tpublic function uri()\r\n\t{\r\n\t\treturn $this->uri;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the HTTP verbs the route responds to.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function getMethods()\r\n\t{\r\n\t\treturn $this->methods();\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the HTTP verbs the route responds to.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function methods()\r\n\t{\r\n\t\treturn $this->methods;\r\n\t}\r\n\r\n\t\/**\r\n\t * Determine if the route only responds to HTTP requests.\r\n\t *\r\n\t * @return bool\r\n\t *\/\r\n\tpublic function httpOnly()\r\n\t{\r\n\t\treturn in_array('http', $this->action, true);\r\n\t}\r\n\r\n\t\/**\r\n\t * Determine if the route only responds to HTTPS requests.\r\n\t *\r\n\t * @return bool\r\n\t *\/\r\n\tpublic function httpsOnly()\r\n\t{\r\n\t\treturn $this->secure();\r\n\t}\r\n\r\n\t\/**\r\n\t * Determine if the route only responds to HTTPS requests.\r\n\t *\r\n\t * @return bool\r\n\t *\/\r\n\tpublic function secure()\r\n\t{\r\n\t\treturn in_array('https', $this->action, true);\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the domain defined for the route.\r\n\t *\r\n\t * @return string|null\r\n\t *\/\r\n\tpublic function domain()\r\n\t{\r\n\t\treturn isset($this->action['domain']) ? $this->action['domain'] : null;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the URI that the route responds to.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tpublic function getUri()\r\n\t{\r\n\t\treturn $this->uri;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set the URI that the route responds to.\r\n\t *\r\n\t * @param  string  $uri\r\n\t * @return \\Illuminate\\Routing\\Route\r\n\t *\/\r\n\tpublic function setUri($uri)\r\n\t{\r\n\t\t$this->uri = $uri;\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the prefix of the route instance.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tpublic function getPrefix()\r\n\t{\r\n\t\treturn isset($this->action['prefix']) ? $this->action['prefix'] : null;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the name of the route instance.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tpublic function getName()\r\n\t{\r\n\t\treturn isset($this->action['as']) ? $this->action['as'] : null;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the action name for the route.\r\n\t *\r\n\t * @return string\r\n\t *\/\r\n\tpublic function getActionName()\r\n\t{\r\n\t\treturn isset($this->action['controller']) ? $this->action['controller'] : 'Closure';\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the action array for the route.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function getAction()\r\n\t{\r\n\t\treturn $this->action;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set the action array for the route.\r\n\t *\r\n\t * @param  array  $action\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function setAction(array $action)\r\n\t{\r\n\t\t$this->action = $action;\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the compiled version of the route.\r\n\t *\r\n\t * @return \\Symfony\\Component\\Routing\\CompiledRoute\r\n\t *\/\r\n\tpublic function getCompiled()\r\n\t{\r\n\t\treturn $this->compiled;\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u8b80\u61c2\u6b64\u7bc7source code\uff0c\u9700\u8981\u5148\u77ad\u89e3Laravel\u63d0\u4f9b\u4e86\u54ea\u4e9b\u8c50\u5bcc\u7684routing\u65b9\u6cd5\uff1a Routing &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=866\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">\u57f7\u884c\u5de5\u4f5c\uff1aRouting\/Route.php<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_mi_skip_tracking":false},"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/866"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=866"}],"version-history":[{"count":7,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/866\/revisions"}],"predecessor-version":[{"id":906,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/866\/revisions\/906"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}