lithium\net\http\Route::match PHP Method

match() public method

Matches a set of parameters against the route, and returns a URL string if the route matches the parameters.
public match ( array $options = [] ) : string | boolean
$options array An array of parameters.
return string | boolean URL string on success, else `false` if the route didn't match.
    public function match(array $options = array())
    {
        $defaults = array('action' => 'index', 'http:method' => 'GET');
        $query = null;
        if (!$this->_config['continue']) {
            $options += $defaults;
            if (isset($options['?'])) {
                $query = $options['?'];
                $query = '?' . (is_array($query) ? http_build_query($query) : $query);
                unset($options['?']);
            }
        }
        if (!($options = $this->_matchMethod($options))) {
            return false;
        }
        if (!($options = $this->_matchKeys($options))) {
            return false;
        }
        foreach ($options as $key => $value) {
            if (isset($this->_config['formatters'][$key])) {
                $options[$key] = $this->_config['formatters'][$key]($value);
            }
        }
        foreach ($this->_subPatterns as $key => $pattern) {
            if (isset($options[$key]) && !preg_match("/^{$pattern}\$/", $options[$key])) {
                return false;
            }
        }
        $defaults = $this->_defaults + $defaults;
        if ($this->_config['continue']) {
            return $this->_write(array('args' => '{:args}') + $options, $this->_defaults);
        }
        return $this->_write($options, $defaults + array('args' => '')) . $query;
    }

Usage Example

Example #1
0
 /**
  * Tests that a single route with default values matches its default parameters, as well as
  * non-default parameters.
  */
 public function testSingleRouteWithDefaultValues()
 {
     $defaults = array('controller' => 'Admin', 'action' => 'index');
     $route = new Route(compact('defaults') + array('template' => '/{:controller}/{:action}', 'pattern' => '@^(?:/(?P[^\\/]+)?)?(?:/(?P[^\\/]+)?)?$@u', 'params' => array('controller' => 'Admin', 'action' => 'index'), 'keys' => array('controller' => 'controller', 'action' => 'action'), 'match' => array()));
     $this->assertIdentical('/', $route->match($defaults));
     $nonDefault = array('controller' => 'Admin', 'action' => 'view');
     $this->assertIdentical('/Admin/view', $route->match($nonDefault));
 }
All Usage Examples Of lithium\net\http\Route::match