Skip to content

Commit

Permalink
Remove additionalParams and update RequestActionTrait tests.
Browse files Browse the repository at this point in the history
The additionalParams parameter isn't very useful as it simply gets
merged into the request later on. We can easily do that earlier in the
process now.

Fix incorrectlty dispatched array based requestAction() calls. They were
going back through Router::parse() when they did not need to.
  • Loading branch information
markstory committed May 15, 2014
1 parent 25c5cc3 commit d367c8a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Routing/Dispatcher.php
Expand Up @@ -94,8 +94,8 @@ public function getEventManager() {
* @return string|void if `$request['return']` is set then it returns response body, null otherwise
* @throws \Cake\Controller\Error\MissingControllerException When the controller is missing.
*/
public function dispatch(Request $request, Response $response, array $additionalParams = array()) {
$beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
public function dispatch(Request $request, Response $response) {
$beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('request', 'response'));
$this->getEventManager()->dispatch($beforeEvent);

$request = $beforeEvent->data['request'];
Expand Down
21 changes: 13 additions & 8 deletions src/Routing/RequestActionTrait.php
Expand Up @@ -16,7 +16,7 @@
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Routing\Dispatcher;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;

/**
Expand Down Expand Up @@ -95,19 +95,23 @@ public function requestAction($url, array $extra = array()) {
}
unset($extra['post'], $extra['query']);

if (is_string($url) && strpos($url, Configure::read('App.fullBaseUrl')) === 0) {
$url = Router::normalize(str_replace(Configure::read('App.fullBaseUrl'), '', $url));
$baseUrl = Configure::read('App.fullBaseUrl');
if (is_string($url) && strpos($url, $baseUrl) === 0) {
$url = Router::normalize(str_replace($baseUrl, '', $url));
}
if (is_string($url)) {
$params = [
'url' => $url
];
} elseif (is_array($url)) {
$params = array_merge($url, [
'pass' => [],
$params = [
'params' => $url,
'base' => false,
'url' => Router::reverse($url)
]);
];
if (empty($params['params']['pass'])) {
$params['params']['pass'] = [];
}
}
if (!empty($post)) {
$params['post'] = $post;
Expand All @@ -116,8 +120,9 @@ public function requestAction($url, array $extra = array()) {
$params['query'] = $query;
}
$request = new Request($params);
$dispatcher = new Dispatcher();
$result = $dispatcher->dispatch($request, new Response(), $extra);
$request->addParams($extra);
$dispatcher = DispatcherFactory::create();
$result = $dispatcher->dispatch($request, new Response());
Router::popRequest();
return $result;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Routing/RequestActionTraitTest.php
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\RequestActionTrait;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
Expand All @@ -40,9 +41,20 @@ public function setUp() {
parent::setUp();
Configure::write('App.namespace', 'TestApp');
Configure::write('Security.salt', 'not-the-default');
DispatcherFactory::add('RoutingFilter');
$this->object = $this->getObjectForTrait('Cake\Routing\RequestActionTrait');
}

/**
* teardown
*
* @return void
*/
public function tearDown() {
parent::tearDown();
DispatcherFactory::clear();
}

/**
* testRequestAction method
*
Expand Down

0 comments on commit d367c8a

Please sign in to comment.