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.
示例#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));
 }
示例#2
0
 public function match(array $options = array(), $context = null)
 {
     $locale = Environment::get('locale');
     return parent::match($options + compact('locale'), $context);
 }
示例#3
0
	/**
	 * Tests that routes with optional trailing elements have unnecessary slashes trimmed.
	 */
	public function testTrimmingEmptyPathElements() {
		$route = new Route(array(
			'template' => '/{:controller}/{:id:[0-9]+}',
			'params' => array('action' => 'index', 'id' => null)
		));

		$url = $route->match(array('controller' => 'posts', 'id' => '13'));
		$this->assertEqual("/posts/13", $url);

		$url = $route->match(array('controller' => 'posts'));
		$this->assertEqual("/posts", $url);
	}
示例#4
0
 /**
  * Tests that continuation routes don't append query strings.
  */
 public function testContinuationRouteWithQueryString()
 {
     $route = new Route(array('template' => '/admin/{:args}', 'continue' => true, 'params' => array('admin' => true)));
     $result = $route->match(array('Posts::index', 'admin' => true, '?' => array('page' => 2)));
     $this->assertEqual('/admin/{:args}', $result);
 }
示例#5
0
 /**
  * Tests that routes with Unicode characters are correctly parsed.
  */
 public function testUnicodeParameters()
 {
     $route = new Route(array('template' => '/{:slug:[\\pL\\pN\\-\\%]+}', 'params' => array('controller' => 'users', 'action' => 'view')));
     $unicode = 'clément';
     $slug = rawurlencode($unicode);
     $params = array('controller' => 'users', 'action' => 'view') + compact('slug');
     $result = $route->match($params);
     $this->assertEqual("/{$slug}", $result);
     $request = new Request(array('url' => "/{$slug}"));
     $result = $route->parse($request, array('url' => $request->url));
     $expected = array('controller' => 'users', 'action' => 'view') + compact('slug');
     $this->assertEqual($expected, $result->params);
     $request = new Request(array('url' => "/{$slug}"));
     $result = $route->parse($request, array('url' => $request->url));
     $expected = array('controller' => 'users', 'action' => 'view') + compact('slug');
     $this->assertEqual($expected, $result->params);
 }
 /**
  * Tests creating a route with a custom pattern that accepts URLs in two formats but only
  * generates them in one.
  *
  * @return void
  */
 public function testRoutingMultipleMatch()
 {
     $route = new Route(array('template' => '/users/{:user}', 'pattern' => '@^/u(?:sers)?(?:/(?P<user>[^\\/]+))$@', 'params' => array('controller' => 'users', 'action' => 'index'), 'match' => array('controller' => 'users', 'action' => 'index'), 'defaults' => array('controller' => 'users'), 'keys' => array('user' => 'user'), 'options' => array('compile' => false, 'wrap' => false)));
     $result = $route->match(array('controller' => 'users', 'user' => 'alke'));
     $expected = '/users/alke';
     $this->assertEqual($expected, $result);
     $request = new Request();
     $request->url = '/users/alke';
     $expected = array('controller' => 'users', 'action' => 'index', 'user' => 'alke');
     $result = $route->parse($request);
     $this->assertEqual($expected, $result);
     $request->url = '/u/alke';
     $result = $route->parse($request);
     $this->assertEqual($expected, $result);
 }