router.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php namespace Laravel\Routing; use Laravel\Request;
  2. class Delegate {
  3. /**
  4. * The destination of the route delegate.
  5. *
  6. * @var string
  7. */
  8. public $destination;
  9. /**
  10. * Create a new route delegate instance.
  11. *
  12. * @param string $destination
  13. * @return void
  14. */
  15. public function __construct($destination)
  16. {
  17. $this->destination = $destination;
  18. }
  19. }
  20. class Router {
  21. /**
  22. * The route loader instance.
  23. *
  24. * @var Loader
  25. */
  26. public $loader;
  27. /**
  28. * The named routes that have been found so far.
  29. *
  30. * @var array
  31. */
  32. protected $names = array();
  33. /**
  34. * The path the application controllers.
  35. *
  36. * @var string
  37. */
  38. protected $controllers;
  39. /**
  40. * The wildcard patterns supported by the router.
  41. *
  42. * @var array
  43. */
  44. protected $patterns = array(
  45. '(:num)' => '([0-9]+)',
  46. '(:any)' => '([a-zA-Z0-9\.\-_]+)',
  47. );
  48. /**
  49. * The optional wildcard patterns supported by the router.
  50. *
  51. * @var array
  52. */
  53. protected $optional = array(
  54. '/(:num?)' => '(?:/([0-9]+)',
  55. '/(:any?)' => '(?:/([a-zA-Z0-9\.\-_]+)',
  56. );
  57. /**
  58. * Create a new router for a request method and URI.
  59. *
  60. * @param Loader $loader
  61. * @param string $controllers
  62. * @return void
  63. */
  64. public function __construct(Loader $loader, $controllers)
  65. {
  66. $this->loader = $loader;
  67. $this->controllers = $controllers;
  68. }
  69. /**
  70. * Find a route by name.
  71. *
  72. * The returned array will be identical the array defined in the routes.php file.
  73. *
  74. * @param string $name
  75. * @return array
  76. */
  77. public function find($name)
  78. {
  79. if (array_key_exists($name, $this->names)) return $this->names[$name];
  80. // To find a named route, we need to iterate through every route defined
  81. // for the application. We will cache the routes by name so we can load
  82. // them very quickly if we need to find them a second time.
  83. foreach ($this->loader->everything() as $key => $value)
  84. {
  85. if (is_array($value) and isset($value['name']) and $value['name'] === $name)
  86. {
  87. return $this->names[$name] = array($key => $value);
  88. }
  89. }
  90. }
  91. /**
  92. * Search the routes for the route matching a request method and URI.
  93. *
  94. * @param string $method
  95. * @param string $uri
  96. * @return Route
  97. */
  98. public function route($method, $uri)
  99. {
  100. $routes = $this->loader->load($uri);
  101. // All route URIs begin with the request method and have a leading
  102. // slash before the URI. We'll put the request method and URI into
  103. // that format so we can easily check for literal matches.
  104. $destination = $method.' /'.trim($uri, '/');
  105. if (isset($routes[$destination]))
  106. {
  107. return new Route($destination, $routes[$destination], array());
  108. }
  109. // If no literal route match was found, we will iterate through all
  110. // of the routes and check each of them one at a time, translating
  111. // any wildcards in the route into actual regular expressions.
  112. foreach ($routes as $keys => $callback)
  113. {
  114. // Only check the routes that couldn't be matched literally...
  115. if (strpos($keys, '(') !== false or strpos($keys, ',') !== false)
  116. {
  117. if ( ! is_null($route = $this->match($destination, $keys, $callback)))
  118. {
  119. return $route;
  120. }
  121. }
  122. }
  123. return $this->controller($method, $uri, $destination);
  124. }
  125. /**
  126. * Attempt to match a given route destination to a given route.
  127. *
  128. * The destination's methods and URIs will be compared against the route's.
  129. * If there is a match, the Route instance will be returned, otherwise null
  130. * will be returned by the method.
  131. *
  132. * @param string $destination
  133. * @param array $keys
  134. * @param mixed $callback
  135. * @return mixed
  136. */
  137. protected function match($destination, $keys, $callback)
  138. {
  139. foreach (explode(', ', $keys) as $key)
  140. {
  141. if (preg_match('#^'.$this->wildcards($key).'$#', $destination))
  142. {
  143. return new Route($keys, $callback, $this->parameters($destination, $key));
  144. }
  145. }
  146. }
  147. /**
  148. * Attempt to find a controller for the incoming request.
  149. *
  150. * @param string $method
  151. * @param string $uri
  152. * @param string $destination
  153. * @return Route
  154. */
  155. protected function controller($method, $uri, $destination)
  156. {
  157. // If the request is to the root of the application, an ad-hoc route
  158. // will be generated to the home controller's "index" method, making
  159. // it the default controller method.
  160. if ($uri === '/') return new Route($method.' /', 'home@index');
  161. $segments = explode('/', trim($uri, '/'));
  162. if ( ! is_null($key = $this->controller_key($segments)))
  163. {
  164. // Extract the various parts of the controller call from the URI.
  165. // First, we'll extract the controller name, then, since we need
  166. // to extract the method and parameters, we will remove the name
  167. // of the controller from the URI. Then we can shift the method
  168. // off of the array of segments. Any remaining segments are the
  169. // parameters that should be passed to the controller method.
  170. $controller = implode('.', array_slice($segments, 0, $key));
  171. $segments = array_slice($segments, $key);
  172. $method = (count($segments) > 0) ? array_shift($segments) : 'index';
  173. return new Route($destination, $controller.'@'.$method, $segments);
  174. }
  175. }
  176. /**
  177. * Search for a controller that can handle the current request.
  178. *
  179. * If a controller is found, the array key for the controller name in the URI
  180. * segments will be returned by the method, otherwise NULL will be returned.
  181. * The deepest possible controller will be considered the controller that
  182. * should handle the request.
  183. *
  184. * @param array $segments
  185. * @return int
  186. */
  187. protected function controller_key($segments)
  188. {
  189. // To find the proper controller, we need to iterate backwards through
  190. // the URI segments and take the first file that matches. That file
  191. // should be the deepest controller matched by the URI.
  192. foreach (array_reverse($segments, true) as $key => $value)
  193. {
  194. $controller = implode('/', array_slice($segments, 0, $key + 1)).EXT;
  195. if (file_exists($path = $this->controllers.$controller))
  196. {
  197. return $key + 1;
  198. }
  199. }
  200. }
  201. /**
  202. * Translate route URI wildcards into actual regular expressions.
  203. *
  204. * @param string $key
  205. * @return string
  206. */
  207. protected function wildcards($key)
  208. {
  209. $count = 0;
  210. // For optional parameters, first translate the wildcards to their
  211. // regex equivalent, sans the ")?" ending. We will add the endings
  212. // back on after we know how many replacements we made.
  213. $key = str_replace(array_keys($this->optional), array_values($this->optional), $key, $count);
  214. $key .= ($count > 0) ? str_repeat(')?', $count) : '';
  215. return str_replace(array_keys($this->patterns), array_values($this->patterns), $key);
  216. }
  217. /**
  218. * Extract the parameters from a URI based on a route URI.
  219. *
  220. * Any route segment wrapped in parentheses is considered a parameter.
  221. *
  222. * @param string $uri
  223. * @param string $route
  224. * @return array
  225. */
  226. protected function parameters($uri, $route)
  227. {
  228. list($uri, $route) = array(explode('/', $uri), explode('/', $route));
  229. $count = count($route);
  230. $parameters = array();
  231. // To find the parameters that should be passed to the route, we will
  232. // iterate through the route segments, and if the segment is enclosed
  233. // in parentheses, we will take the matching segment from the request
  234. // URI and add it to the array of parameters.
  235. for ($i = 0; $i < $count; $i++)
  236. {
  237. if (preg_match('/\(.+\)/', $route[$i]) and isset($uri[$i]))
  238. {
  239. $parameters[] = $uri[$i];
  240. }
  241. }
  242. return $parameters;
  243. }
  244. }