/** * 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; }