3
* Flight: An extensible micro-framework.
5
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
6
* @license MIT, http://flightphp.com/license
12
* The Router class is responsible for routing an HTTP request to
13
* an assigned callback function. The Router tries to match the
14
* requested URL against a series of URL patterns.
22
protected $routes = array();
25
* Pointer to current route
34
* @return array Array of routes
36
public function getRoutes() {
41
* Clears all routes in the router.
43
public function clear() {
44
$this->routes = array();
48
* Maps a URL pattern to a callback function.
50
* @param string $pattern URL pattern to match
51
* @param callback $callback Callback function
52
* @param boolean $pass_route Pass the matching route object to the callback
54
public function map($pattern, $callback, $pass_route = false) {
56
$methods = array('*');
58
if (strpos($pattern, ' ') !== false) {
59
list($method, $url) = explode(' ', trim($pattern), 2);
61
$methods = explode('|', $method);
64
$this->routes[] = new Route($url, $callback, $methods, $pass_route);
68
* Routes the current request.
70
* @param Request $request Request object
71
* @return Route Matching route
73
public function route(Request $request) {
74
while ($route = $this->current()) {
75
if ($route !== false && $route->matchMethod($request->method) && $route->matchUrl($request->url)) {
85
* Gets the current route.
89
public function current() {
90
return isset($this->routes[$this->index]) ? $this->routes[$this->index] : false;
94
* Gets the next route.
98
public function next() {
103
* Reset to the first route.
105
public function reset() {