System\Router::match PHP Method

match() public method

Try and match the request method and uri with defined routes
public match ( ) : object
return object Return a instance of a Route
    public function match()
    {
        $routes = $this->routes();
        // try a simple match
        if (array_key_exists($this->uri, $routes)) {
            return new Route($routes[$this->uri]);
        }
        // search for patterns
        $searches = array_keys(static::$patterns);
        $replaces = array_values(static::$patterns);
        foreach ($routes as $pattern => $action) {
            // replace wildcards
            if (strpos($pattern, ':') !== false) {
                $pattern = str_replace($searches, $replaces, $pattern);
            }
            // slice array of matches. $matches[0] will contain the text that
            // matched the full pattern, $matches[1] will have the text that
            // matched the first captured parenthesized subpattern, and so on.
            if (preg_match('#^' . $pattern . '$#', $this->uri, $matched)) {
                return new Route($action, array_slice($matched, 1));
            }
        }
        // call 404 handler
        if (is_array(static::$not_found)) {
            return new Route(static::$not_found);
        }
        throw new Router\Exception('No routes matched');
    }

Usage Example

Example #1
0
 public function test_matching()
 {
     $this->assertFalse(\System\Router::match('/{res_src:varchar:yes:static,media}/{res_type:varchar}/{res_path:any}', '/api/schema/public'));
 }