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

match() public static method

Router::connect('/login', array('controller' => 'users', 'action' => 'login')); This will match: $url = Router::match(array('controller' => 'users', 'action' => 'login')); returns /login For URLs templates with no insert parameters (i.e. elements like {:id} that are replaced with a value), all parameters must match exactly as they appear in the route parameters. Alternatively to using a full array, you can specify routes using a more compact syntax. The above example can be written as: $url = Router::match('Users::login'); // still returns /login You can combine this with more complicated routes; for example: Router::connect('/posts/{:id:\d+}', array('controller' => 'posts', 'action' => 'view')); This will match: $url = Router::match(array('controller' => 'posts', 'action' => 'view', 'id' => '1138')); returns /posts/1138 Again, you can specify the same URL with a more compact syntax, as in the following: $url = Router::match(array('Posts::view', 'id' => '1138')); again, returns /posts/1138 You can use either syntax anywhere an URL is accepted, i.e. when redirecting or creating links using the Html helper.
See also: lithium\action\Controller::redirect()
See also: lithium\template\helper\Html::link()
public static match ( array | string $url = [], Request $context = null, array $options = [] ) : string
$url array | string An array of parameters to match, or paremeters in their shorthand form (i.e. `'Posts::view'`). Also takes non-routed, manually generated URL strings.
$context lithium\action\Request This supplies the context for any persistent parameters, as well as the base URL for the application.
$options array Options for the generation of the matched URL. Currently accepted values are: - `'absolute'` _boolean_: Indicates whether or not the returned URL should be an absolute path (i.e. including scheme and host name). - `'host'` _string_: If `'absolute'` is `true`, sets the host name to be used, or overrides the one provided in `$context`. - `'scheme'` _string_: If `'absolute'` is `true`, sets the URL scheme to be used, or overrides the one provided in `$context`. - `'scope'` _string_: Optionnal scope name.
return string Returns a generated URL, based on the URL template of the matched route, and prefixed with the base URL of the application.
    public static function match($url = array(), $context = null, array $options = array())
    {
        $options = static::_matchOptions($url, $context, $options);
        if (is_string($url = static::_prepareParams($url, $context, $options))) {
            return $url;
        }
        $base = $options['base'];
        $url += array('action' => 'index');
        $stack = array();
        $suffix = isset($url['#']) ? "#{$url['#']}" : null;
        unset($url['#']);
        $scope = $options['scope'];
        if (isset(static::$_configurations[$scope])) {
            foreach (static::$_configurations[$scope] as $route) {
                if (!($match = $route->match($url + array('scope' => static::attached($scope)), $context))) {
                    continue;
                }
                if ($route->canContinue()) {
                    $stack[] = $match;
                    $export = $route->export();
                    $keys = $export['match'] + $export['keys'] + $export['defaults'];
                    unset($keys['args']);
                    $url = array_diff_key($url, $keys);
                    continue;
                }
                if ($stack) {
                    $stack[] = $match;
                    $match = static::_compileStack($stack);
                }
                $path = rtrim("{$base}{$match}{$suffix}", '/') ?: '/';
                $path = $options ? static::_prefix($path, $context, $options) : $path;
                return $path ?: '/';
            }
        }
        $url = static::_formatError($url);
        $message = "No parameter match found for URL `{$url}`";
        $message .= $scope ? " in `{$scope}` scope." : '.';
        throw new RoutingException($message);
    }

Usage Example

Example #1
0
 /**
  * Create navigation link compatible with `Twitter Bootstrap` markup.
  * Instead of plain `<a/>` output this method wrap anchor in `<li/>`. If current url is url of
  * wrapped `<a/>` add `active` class to `<li/>` wrapper.
  * For example:
  * {{{
  *   $this->backend->nav('Test', '/');
  *   // outputs:
  *   // <li><a href="/">Test</a></li>
  *   // if current url is url of anchor:
  *   // <li class="active"><a href="/">Test</a></li>
  * }}}
  *
  * @param $title
  * @param mixed $url
  * @param array $options Add following options to link:
  *     - `'wrapper-options'` _array_: Options that will be passed to `'nav-link'`
  *     - `'return'` _string_: Define would you like `'html'` output or `'array'` that contains two
  *     keys `'active'` _boolean_ and `'html'` used by `dropdown` method for example to know when
  *     to add `'active'` class to parent.
  *
  * @return array|string
  *
  * @see lithium\template\helper\Html::link()
  */
 public function nav($title, $url = null, array $options = array())
 {
     $defaults = array('wrapper-options' => array(), 'return' => 'html');
     list($scope, $options) = $this->_options($defaults, $options);
     $request = $this->_context->request();
     $currentUrl = $request->env('base') . $request->url;
     $matchedUrl = Router::match($url, $request);
     $active = false;
     if ($currentUrl === $matchedUrl || $currentUrl === $matchedUrl . '/index') {
         $active = true;
         if (isset($scope['wrapper-options']['class'])) {
             $scope['wrapper-options']['class'] .= ' active';
         } else {
             $scope['wrapper-options']['class'] = 'active';
         }
     }
     $link = $this->link($title, $url, $options);
     $html = $this->_render(__METHOD__, 'nav-link', array('options' => $scope['wrapper-options'], 'link' => $link));
     if ($scope['return'] === 'html') {
         return $html;
     }
     if ($scope['return'] === 'array') {
         return compact('active', 'html');
     }
 }
All Usage Examples Of lithium\net\http\Router::match