public static function boot(Route $route) { if (!empty($route->middleware())) { foreach ($route->middleware() as $middleware) { self::$name = $middleware; $namespace = File::find($middleware, 'Middleware'); $class = new $namespace(); $call = $class->run(self::$request, self::$response); if (is_subclass_of($call, Middleware::class) !== TRUE) { throw new MiddlewareException("':middleware' does not return instance of Stativo\\App\\Middleware", [':middleware' => $middleware]); } } } return TRUE; }
protected function execute(InputInterface $input, OutputInterface $output) { $routes = array(); foreach (\Stativo\Core\Route::all() as $name => $route) { $routes[] = [$name, $route->_uri, is_array($route->_method) ? implode('|', $route->_method) : $route->_method, implode('@', array_map(function ($word) { return ucfirst($word); }, $route->_defaults)), is_array($route->_middleware) ? implode('|', $route->_middleware) : $route->_middleware]; } $table = new Table($output); $table->setHeaders(array('Name', 'Uri', 'Method', 'Defaults', 'Middleware'))->setRows($routes); $table->render(); }
/** * Process URI * @param string $uri uri from url * @param array $routes avalible routes * @return array params and route */ public static function process_uri($uri, $routes = null) { // Load routes $routes = empty($routes) ? Route::all() : $routes; $params = null; foreach ($routes as $name => $route) { // We found something suitable if ($params = $route->matches($uri, $name)) { return array('params' => $params, 'route' => $route); } } return null; }
<?php use Stativo\Core\Route; Route::set('stativo.guide.media', Route::GET, 'guide-media/<file>', array('file' => '.+'))->defaults(array('controller' => 'guide', 'action' => 'get_media')); Route::set('stativo.guide.site', Route::GET, 'guide(/<class>)')->defaults(array('controller' => 'guide', 'action' => 'index'));
/** * Tests if the route matches a given Request. A successful match will return * all of the routed parameters as an array. A failed match will return * boolean FALSE. * * // Params: controller = users, action = edit, id = 10 * $params = $route->matches(Request::factory('users/edit/10')); * * This method should almost always be used within an if/else block: * * if ($params = $route->matches($request)) * { * // Parse the parameters * } * * @param Request $request Request object to match * @return array on success * @return FALSE on failure */ public function matches(Request $request, $name) { // Get the URI from the Request $_request = Request::instance(); $uri = $_request->uri(); if (!preg_match($this->_route_regex, $uri, $matches)) { return FALSE; } $params = array(); foreach ($matches as $key => $value) { if (is_int($key)) { // Skip all unnamed keys continue; } // Set the value for all matched keys $params[$key] = $value; } foreach ($this->_defaults as $key => $value) { if (!isset($params[$key]) or $params[$key] === '') { // Set default values for any key that was not matched $params[$key] = $value; } } if (!empty($params['controller'])) { // PSR-0: Replace underscores with spaces, run ucwords, then replace underscore $params['controller'] = str_replace(' ', '\\', ucwords(str_replace('\\', ' ', $params['controller']))); } if (!empty($params['directory'])) { // PSR-0: Replace underscores with spaces, run ucwords, then replace underscore $params['directory'] = str_replace(' ', '\\', ucwords(str_replace('\\', ' ', $params['directory']))); } if ($this->_filters) { foreach ($this->_filters as $callback) { // Execute the filter giving it the route, params, and request $return = call_user_func($callback, $this, $params, $request); if ($return === FALSE) { // Filter has aborted the match return FALSE; } elseif (is_array($return)) { // Filter has modified the parameters $params = $return; } } } self::$_current_route = Route::get($name); return $params; }
/** * Create URL from named route * @param string $route route name * @param array $params parameters for the url * @return void */ function route($route, $params = array()) { return Route::url($route, $params); }