/** * Allow for static access to HTTP Methods. * * @param string $method The method name * @param array $args The function arguments * * @return mixed The route is compiled */ public static function __callStatic($method, $args) { $method = strtolower($method); list($route, $closure) = $args; if (!in_array($method, static::$allowed)) { throw new InvalidMethodException($method . ' is not a valid method name.'); } if (!static::$router) { static::$router = new static(); } return static::$router->compile($method, $route, $closure); }
/** * Redirect to a route. * * @param Route|string $route The route object or route name * @param array $vars An array of variables to pass to the route */ public static function route($route, array $vars = []) { $app = Application::instance(); if (!$route instanceof Route) { $route = Router::route($route); } if ($route !== null) { $app->response->redirect($route->uri($vars)); } }
/** * Create a new request for the application. */ public function __construct() { $this->router = new Router(); $this->method = strtoupper($this->router->requestMethod()); // Store the raw input data $input = array_merge($_GET, $_POST); $this->rawInput = new Input($input); // Escape the input data, and store it again $input = $this->escapeInput($input); $this->input = new Input($input); // Store the current URI if (isset($_SERVER['REQUEST_URI'])) { $this->uri = $_SERVER['REQUEST_URI']; } // Retrieve the previous URI from the session, and store it // against the request object if (($previous = Session::get('previous_uri')) !== null) { $this->previousUri = $previous; } // Update the previous URI session key now that we have retrieved // it's value Session::set('previous_uri', $this->uri); }
public function test() { list($uri, $verb) = array_pad($this->args, 2, null); if (!$uri) { Prompt::outputend(ANSI::fg('You must specify a URI to test.' . "\n", ANSI::RED)); return Help::execute(Application::instance()); } if (!$verb) { $verb = 'GET'; } $this->loadRoutes(strtoupper($verb)); $_SERVER['REQUEST_URI'] = $uri; foreach (Router::routes() as $route) { if ($route->match()) { Prompt::tableHeader(['Verb' => 10, 'Route' => 35, 'Name' => 20]); return Prompt::tableRow([strtoupper($route->verb), $route->route, $route->name !== $route->route ? $route->name : ' ']); } } Prompt::outputend(ANSI::fg('Route for URI ' . $uri . ' could not be found.', ANSI::RED)); exit; }
/** * Check that a valid route has been found. */ protected function checkRoute() { if (Router::current() === null) { return $this->error(404); } }
/** * Check if the route matches the current request on ANY method, * and if so execute its callback. * * @return mixed The route callback is invoked */ private function executeAny() { if (in_array(Router::requestMethod(), Router::allowed()) && is_array($vars = $this->match())) { $ref = new \ReflectionFunction($this->callback); return $ref->invokeArgs($vars); } return; }