Example #1
0
/**
 * Connects the default, built-in routes, including prefix and plugin routes with the i18n custom Route
 * Code mostly duplicated from Router::__connectDefaultRoutes
 *
 * @TODO Add Short route for plugins
 * @see Router::__connectDefaultRoutes
 * @param array $pluginExceptions Plugins ommited from the lang default routing
 * @return void
 */
	public static function connectDefaultRoutes($pluginExceptions = array()) {
		if (!self::$__defaultsMapped) {
			Router::defaults(false);
			$options = array('routeClass' => __CLASS__);
			$prefixes = Router::prefixes();
			
			if ($plugins = App::objects('plugin')) {
				foreach ($plugins as $key => $value) {
					$plugins[$key] = Inflector::underscore($value);
				}
				$plugins = array_diff($plugins, $pluginExceptions);

				$pluginPattern = implode('|', $plugins);
				$match = array('plugin' => $pluginPattern) + $options;
				
				foreach ($prefixes as $prefix) {
					$params = array('prefix' => $prefix, $prefix => true);
					$indexParams = $params + array('action' => 'index');
					Router::connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
					Router::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
				}
				Router::connect('/:plugin/:controller', array('action' => 'index'), $match);
				Router::connect('/:plugin/:controller/:action/*', array(), $match);
			}
	
			foreach ($prefixes as $prefix) {
				$params = array('prefix' => $prefix, $prefix => true);
				$indexParams = $params + array('action' => 'index');
				Router::connect("/{$prefix}/:controller/:action/*", $params, $options);
				Router::connect("/{$prefix}/:controller", $indexParams, $options);
			}
			Router::connect('/:controller', array('action' => 'index'), $options);
			Router::connect('/:controller/:action/*', array(), $options);

			$Router = Router::getInstance();
			if ($Router->named['rules'] === false) {
				$Router->connectNamed(true);
			}

			self::$__defaultsMapped = true;
		}
	}
Example #2
0
/**
 * test that created routes are parsed correctly.
 *
 * @return void
 * @access public
 */
	public function testParsing() {
		Configure::write('Routing.prefixes', array('admin'));
		I18nRoute::reload();
		Router::defaults(false);

		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'), array('routeClass' => 'I18nRoute'));
		Router::connect('/admin/:controller/:action/*', array('action' => 'index', 'admin' => true), array('routeClass' => 'I18nRoute'));
		Router::connect('/:controller/:action/*', array('action' => 'index'), array('routeClass' => 'I18nRoute'));
		// This call is needed to work since the "default language" route is created from the constructor
		I18nRoute::promoteLangRoutes();

		$result = Router::parse('/');
		$expected = array(
			'plugin' => null, 'controller' => 'pages', 'action' => 'display',
			'named' => array(), 'pass' => array('home'), 'lang' => $this->__defaultLang
		);
		$this->assertEqual($result, $expected);
		$this->assertEqual(Configure::read('Config.language'), $this->__defaultLang);
		
		$result = Router::parse('/posts/view/42');
		$expected = array(
			'plugin' => null, 'controller' => 'posts', 'action' => 'view',
			'named' => array(), 'pass' => array(42), 'lang' => $this->__defaultLang
		);
		$this->assertEqual($result, $expected);

		$result = Router::parse('/admin/posts/view/42');
		$expected = array(
			'plugin' => null, 'controller' => 'posts', 'action' => 'view',
			'named' => array(), 'pass' => array(42), 'lang' => $this->__defaultLang,
			'admin' => true, 'prefix' => 'admin'
		);
		$this->assertEqual($result, $expected);
		

		$result = Router::parse('/spa');
		$expected = array(
			'plugin' => null, 'controller' => 'pages', 'action' => 'display',
			'named' => array(), 'pass' => array('home'), 'lang' => 'spa'
		);
		$this->assertEqual($result, $expected);
		$this->assertEqual(Configure::read('Config.language'), 'spa');
		
		
		$result = Router::parse('/spa/posts/view/42');
		$expected = array(
			'plugin' => null, 'controller' => 'posts', 'action' => 'view',
			'named' => array(), 'pass' => array(42), 'lang' => 'spa'
		);
		$this->assertEqual($result, $expected);

		$result = Router::parse('/spa/admin/posts/view/42');
		$expected = array(
			'plugin' => null, 'controller' => 'posts', 'action' => 'view',
			'named' => array(), 'pass' => array(42), 'lang' => 'spa', 'admin' => true, 'prefix' => 'admin'
		);
		$this->assertEqual($result, $expected);
	}
Example #3
0
 /**
  * test that connectDefaults() can disable default route connection
  *
  * @return void
  */
 function testDefaultsMethod()
 {
     Router::defaults(false);
     Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2));
     $result = Router::parse('/posts/edit/5');
     $this->assertFalse(isset($result['controller']));
     $this->assertFalse(isset($result['action']));
 }
Example #4
0
 /**
  * test the parsing of routes.
  *
  * @return void
  */
 public function testParsing()
 {
     Router::defaults(false);
     Router::connect('/:plugin', array('action' => 'index'), array('routeClass' => 'PluginShortI18nRoute', 'plugin' => 'foo|bar'));
     // This call is needed to work since the "default language" route is created from the constructor
     PluginShortI18nRoute::promoteLangRoutes();
     $result = Router::parse('/foo');
     $this->assertEqual($result['plugin'], 'foo');
     $this->assertEqual($result['controller'], 'foo');
     $this->assertEqual($result['action'], 'index');
     $this->assertEqual($result['lang'], $this->__defaultLang);
     $result = Router::parse('/spa/foo');
     $this->assertEqual($result['plugin'], 'foo');
     $this->assertEqual($result['controller'], 'foo');
     $this->assertEqual($result['action'], 'index');
     $this->assertEqual($result['lang'], 'spa');
     $result = Router::parse('/wrong');
     $this->assertTrue(empty($result['plugin']), 'Wrong plugin name matched %s');
 }