Skip to content

Commit

Permalink
Moving appending of prefix to action into the Router. This makes Rout…
Browse files Browse the repository at this point in the history
…er::parse() always return what you get in the controller.

Updating Dispatcher to not hold a reference to the current request, as its not used.
Updating private action detection so its simpler.
Fixes issues with prefix actions not getting the correct default view.  Fixes #1580
  • Loading branch information
markstory committed Mar 11, 2011
1 parent b292f4f commit 7f18f05
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
24 changes: 6 additions & 18 deletions cake/libs/dispatcher.php
Expand Up @@ -37,14 +37,6 @@
*/
class Dispatcher {

/**
* The request object
*
* @var CakeRequest
* @access public
*/
public $request = null;

/**
* Response object used for asset/cached responses.
*
Expand Down Expand Up @@ -87,18 +79,16 @@ public function dispatch(CakeRequest $request, $additionalParams = array()) {
return;
}

$this->request = $this->parseParams($request, $additionalParams);
$controller = $this->_getController($this->request);
$request = $this->parseParams($request, $additionalParams);
Router::setRequestInfo($request);
$controller = $this->_getController($request);

if (!is_object($controller)) {
Router::setRequestInfo($request);
if (!($controller instanceof Controller)) {
throw new MissingControllerException(array(
'controller' => Inflector::camelize($request->params['controller']) . 'Controller'
));
}

Router::setRequestInfo($request);

if ($this->_isPrivateAction($request)) {
throw new PrivateActionException(array(
'controller' => Inflector::camelize($request->params['controller']) . "Controller",
Expand All @@ -120,10 +110,8 @@ protected function _isPrivateAction($request) {
$privateAction = $request->params['action'][0] === '_';
$prefixes = Router::prefixes();

if (!empty($prefixes)) {
if (isset($request->params['prefix'])) {
$request->params['action'] = $request->params['prefix'] . '_' . $request->params['action'];
} elseif (strpos($request->params['action'], '_') > 0) {
if (!$privateAction && !empty($prefixes)) {
if (empty($request->params['prefix']) && strpos($request->params['action'], '_') > 0) {
list($prefix, $action) = explode('_', $request->params['action']);
$privateAction = in_array($prefix, $prefixes);
}
Expand Down
3 changes: 3 additions & 0 deletions cake/libs/router.php
Expand Up @@ -503,6 +503,9 @@ public static function parse($url) {
break;
}
}
if (isset($out['prefix'])) {
$out['action'] = $out['prefix'] . '_' . $out['action'];
}

if (!empty($ext) && !isset($out['url']['ext'])) {
$out['url']['ext'] = $ext;
Expand Down
8 changes: 6 additions & 2 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1877,7 +1877,7 @@ function testParsingWithPrefixes() {
);

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

$result = Router::parse('/admin/posts');
Expand Down Expand Up @@ -1911,7 +1911,7 @@ function testParsingWithPrefixes() {
);

$result = Router::parse('/members/posts/index');
$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'members' => true);
$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'members_index', 'members' => true);
$this->assertEqual($result, $expected);

$result = Router::url(array('members' => true, 'controller' => 'posts', 'action' =>'index', 'page' => 2));
Expand Down Expand Up @@ -1941,6 +1941,10 @@ function testUrlWritingWithPrefixes() {
$expected = '/company/users/login';
$this->assertEqual($result, $expected);

$result = Router::url(array('controller' => 'users', 'action' => 'company_login', 'company' => true));
$expected = '/company/users/login';
$this->assertEqual($result, $expected);

$request = new CakeRequest();
Router::setRequestInfo(
$request->addParams(array(
Expand Down

0 comments on commit 7f18f05

Please sign in to comment.