Exemplo n.º 1
0
 /**
  * Attempt to match a url array.  If the url matches the route parameters + settings, then
  * return a generated string url.  If the url doesn't match the route parameters false will be returned.
  * This method handles the reverse routing or conversion of url arrays into string urls.
  *
  * @param array $url An array of parameters to check matching with.
  * @return mixed Either a string url for the parameters if they match or false.
  */
 public function match($url)
 {
     if (!isset($this->options['models'])) {
         return false;
     }
     $index = 0;
     foreach ($this->options['models'] as $checkNamed => $slugField) {
         if (is_numeric($checkNamed)) {
             $checkNamed = $slugField;
             $slugField = null;
         }
         if (!empty($url[$index])) {
             $url[$checkNamed] = $url[$index];
             unset($url[$index]);
         }
         if (isset($url[$checkNamed])) {
             $slugSet = $this->_Sluggable->getSlugs($checkNamed, $slugField);
             if ($slugSet === false) {
                 continue;
             }
             if (isset($slugSet[$url[$checkNamed]])) {
                 $url[$index] = $slugSet[$url[$checkNamed]];
                 unset($url[$checkNamed]);
             }
         }
         $index++;
     }
     return parent::match($url);
 }
Exemplo n.º 2
0
/**
 * test that routes match their pattern.
 *
 * @return void
 */
	function testMatchBasic() {
		$route = new I18nRoute('/:controller/:action/:id', array('plugin' => null));
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
		$this->assertFalse($result);

		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 0));
		$this->assertFalse($result);

		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1));
		$this->assertEqual($result, '/spa/posts/view/1');
		
		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1, 'lang' => 'fre'));
		$this->assertEqual($result, '/fre/posts/view/1');


		$route = new I18nRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
		$this->assertEqual($result, '/spa');

		$route = new I18nRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
		$this->assertEqual($result, '/spa/pages/home');

		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
		$this->assertEqual($result, '/spa/pages/about');


		$route = new I18nRoute('/blog/:action', array('controller' => 'posts'));
		$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
		$this->assertEqual($result, '/spa/blog/view');

		$result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
		$this->assertFalse($result);


		$route = new I18nRoute('/foo/:controller/:action', array('action' => 'index'));
		$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
		$this->assertEqual($result, '/spa/foo/posts/view');


		$route = new I18nRoute('/admin/subscriptions/:action/*', array(
			'controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'
		));

		$url = array('controller' => 'subscribe', 'admin' => true, 'action' => 'edit', 1);
		$result = $route->match($url);
		$expected = '/spa/admin/subscriptions/edit/1';
		$this->assertEqual($result, $expected);
	}
Exemplo n.º 3
0
 public function testMatch_ShouldNotRemoveDefaultLangWhenUsedInRouteContent()
 {
     $routeStartsChunk = '/' . $this->__defaultLang . '/pages';
     $route = new I18nRoute($routeStartsChunk . '/*', array('controller' => 'pages', 'action' => 'display', 'lang' => $this->__defaultLang), array('disableAutoNamedLang' => true));
     $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home', 'lang' => $this->__defaultLang));
     $this->assertEquals($routeStartsChunk . '/home', $result);
 }
Exemplo n.º 4
0
 /**
  * Reverse route plugin shortcut urls.  If the plugin and controller
  * are not the same the match is an auto fail.
  *
  * @param array $url Array of parameters to convert to a string.
  * @return mixed either false or a string url.
  */
 public function match($url)
 {
     if (isset($url['controller']) && isset($url['plugin']) && $url['plugin'] != $url['controller']) {
         return false;
     }
     $this->defaults['controller'] = $url['controller'];
     $result = parent::match($url);
     unset($this->defaults['controller']);
     return $result;
 }