Browse Source

Stop inflecting prefix.

Inflecting prefix causes issues with reverse routing as prefixes are
always connected as snake_case versions. Inflecting on parse/match
causes match errors.

The ControllerFactory changes are required to make namespaces match
correctly.
mark_story 11 years ago
parent
commit
0dcfaff52e

+ 1 - 10
src/Controller/Controller.php

@@ -392,20 +392,11 @@ class Controller implements EventListener {
  * @return bool
  */
 	protected function _isPrivateAction(\ReflectionMethod $method, Request $request) {
-		$privateAction = (
+		return (
 			$method->name[0] === '_' ||
 			!$method->isPublic() ||
 			!in_array($method->name, $this->methods)
 		);
-		$prefixes = Router::prefixes();
-
-		if (!$privateAction && !empty($prefixes)) {
-			if (empty($request->params['prefix']) && strpos($request->params['action'], '_') > 0) {
-				list($prefix) = explode('_', $request->params['action']);
-				$privateAction = in_array($prefix, $prefixes);
-			}
-		}
-		return $privateAction;
 	}
 
 /**

+ 2 - 1
src/Routing/Filter/ControllerFactoryFilter.php

@@ -17,6 +17,7 @@ namespace Cake\Routing\Filter;
 use Cake\Core\App;
 use Cake\Event\Event;
 use Cake\Routing\DispatcherFilter;
+use Cake\Utility\Inflector;
 
 /**
  * A dispatcher filter that builds the controller to dispatch
@@ -64,7 +65,7 @@ class ControllerFactoryFilter extends DispatcherFilter {
 			$controller = $request->params['controller'];
 		}
 		if (!empty($request->params['prefix'])) {
-			$namespace .= '/' . $request->params['prefix'];
+			$namespace .= '/' . Inflector::camelize($request->params['prefix']);
 		}
 		$className = false;
 		if ($pluginPath . $controller) {

+ 0 - 6
src/Routing/Route/InflectedRoute.php

@@ -41,9 +41,6 @@ class InflectedRoute extends Route {
 		if (!empty($params['plugin'])) {
 			$params['plugin'] = Inflector::camelize($params['plugin']);
 		}
-		if (!empty($params['prefix'])) {
-			$params['prefix'] = Inflector::camelize($params['prefix']);
-		}
 		return $params;
 	}
 
@@ -64,9 +61,6 @@ class InflectedRoute extends Route {
 		if (!empty($url['plugin'])) {
 			$url['plugin'] = Inflector::underscore($url['plugin']);
 		}
-		if (!empty($url['prefix'])) {
-			$url['prefix'] = Inflector::underscore($url['prefix']);
-		}
 		return parent::match($url, $context);
 	}
 

+ 0 - 20
src/Routing/Router.php

@@ -53,14 +53,6 @@ class Router {
 	protected static $_fullBaseUrl;
 
 /**
- * List of action prefixes used in connected routes.
- * Includes admin prefix
- *
- * @var array
- */
-	protected static $_prefixes = [];
-
-/**
  * List of valid extensions to parse from a URL. If null, any extension is allowed.
  *
  * @var array
@@ -418,18 +410,6 @@ class Router {
 	}
 
 /**
- * Returns the list of prefixes used in connected routes
- *
- * @return array A list of prefixes used in connected routes
- */
-	public static function prefixes() {
-		if (empty(static::$_prefixes)) {
-			return (array)Configure::read('Routing.prefixes');
-		}
-		return static::$_prefixes;
-	}
-
-/**
  * Parses given URL string. Returns 'routing' parameters for that URL.
  *
  * @param string $url URL to be parsed

+ 13 - 16
src/Template/Error/missing_route.ctp

@@ -27,25 +27,22 @@ use Cake\Utility\Debugger;
 Add a matching route to <?= APP_DIR . DS . 'Config' . DS . 'routes.php' ?></p>
 
 <h3>Connected Routes</h3>
+<table cellspacing="0" cellpadding="0">
+<tr><th>Template</th><th>Defaults</th><th>Options</th></tr>
 <?php
-foreach (Router::routes() as $scope):
-	printf('<h4>Scope: %s</h4>', $scope->path());
-	echo '<table cellspacing="0" cellpadding="0">';
-	echo '<tr><th>Template</th><th>Defaults</th><th>Options</th></tr>';
-
-	foreach ($scope->routes() as $route):
-		echo '<tr>';
-		printf(
-			'<th width="25%%">%s</th><th>%s</th><th width="20%%">%s</th>',
-			$route->template,
-			Debugger::exportVar($route->defaults),
-			Debugger::exportVar($route->options)
-		);
-		echo '</tr>';
-	endforeach;
-	echo '</table>';
+foreach (Router::routes() as $route):
+	echo '<tr>';
+	printf(
+		'<th width="25%%">%s</th><th>%s</th><th width="20%%">%s</th>',
+		$route->template,
+		Debugger::exportVar($route->defaults),
+		Debugger::exportVar($route->options)
+	);
+	echo '</tr>';
 endforeach;
 ?>
+</table>
+
 <p class="notice">
 	<strong>Notice: </strong>
 	<?= sprintf('If you want to customize this error message, create %s', APP_DIR . DS . 'Template' . DS . 'Error' . DS . 'missing_route.ctp'); ?>

+ 8 - 0
tests/TestCase/Controller/Component/AuthComponentTest.php

@@ -858,9 +858,13 @@ class AuthComponentTest extends TestCase {
  */
 	public function testAdminRoute() {
 		$event = new Event('Controller.startup', $this->Controller);
+		Router::reload();
 		Router::prefix('admin', function($routes) {
 			$routes->fallbacks();
 		});
+		Router::scope('/', function($routes) {
+			$routes->fallbacks();
+		});
 
 		$url = '/admin/auth_test/add';
 		$this->Auth->request->addParams(Router::parse($url));
@@ -912,9 +916,13 @@ class AuthComponentTest extends TestCase {
  */
 	public function testLoginActionRedirect() {
 		$event = new Event('Controller.startup', $this->Controller);
+		Router::reload();
 		Router::prefix('admin', function($routes) {
 			$routes->fallbacks();
 		});
+		Router::scope('/', function($routes) {
+			$routes->fallbacks();
+		});
 
 		$url = '/admin/auth_test/login';
 		$request = $this->Auth->request;

+ 0 - 31
tests/TestCase/Routing/RouterTest.php

@@ -1492,30 +1492,6 @@ class RouterTest extends TestCase {
 	}
 
 /**
- * Test that Routing.prefixes are used when a Router instance is created
- * or reset
- *
- * @return void
- */
-	public function testRoutingPrefixesSetting() {
-		$restore = Configure::read('Routing');
-
-		Configure::write('Routing.prefixes', array('admin', 'member', 'super_user'));
-		Router::reload();
-		$result = Router::prefixes();
-		$expected = array('admin', 'member', 'super_user');
-		$this->assertEquals($expected, $result);
-
-		Configure::write('Routing.prefixes', array('admin', 'member'));
-		Router::reload();
-		$result = Router::prefixes();
-		$expected = array('admin', 'member');
-		$this->assertEquals($expected, $result);
-
-		Configure::write('Routing', $restore);
-	}
-
-/**
  * testParseExtensions method
  *
  * @return void
@@ -2011,7 +1987,6 @@ class RouterTest extends TestCase {
  * @return void
  */
 	public function testParsingWithLiteralPrefixes() {
-		Configure::write('Routing.prefixes', []);
 		Router::reload();
 		$adminParams = array('prefix' => 'admin');
 		Router::connect('/admin/:controller', $adminParams);
@@ -2040,10 +2015,6 @@ class RouterTest extends TestCase {
 		$expected = '/base/admin/posts';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::prefixes();
-		$expected = [];
-		$this->assertEquals($expected, $result);
-
 		Router::reload();
 
 		$prefixParams = array('prefix' => 'members');
@@ -2080,7 +2051,6 @@ class RouterTest extends TestCase {
 		Router::connect('/company/:controller/:action/*', array('prefix' => 'company'));
 		Router::connect('/:action', array('controller' => 'users'));
 
-		/*
 		$result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => 'company'));
 		$expected = '/company/users/login';
 		$this->assertEquals($expected, $result);
@@ -2088,7 +2058,6 @@ class RouterTest extends TestCase {
 		$result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => 'company'));
 		$expected = '/company/users/login';
 		$this->assertEquals($expected, $result);
-		 */
 
 		$request = new Request();
 		Router::setRequestInfo(