Skip to content

Commit

Permalink
Pass the base and webroot properties into sub-requests.
Browse files Browse the repository at this point in the history
Clone the root request's base and webroot properties into sub-requests.
This is necessary to allow images URLs to be generated correctly in
views rendered inside a requestAction() call.

Refs #6598
  • Loading branch information
markstory committed May 22, 2015
1 parent 77017c6 commit 7204e18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Routing/RequestActionTrait.php
Expand Up @@ -127,6 +127,11 @@ public function requestAction($url, array $extra = [])
$params['params']['pass'] = [];
}
}
$current = Router::getRequest();
if ($current) {
$params['base'] = $current->base;
$params['webroot'] = $current->webroot;
}

$params['post'] = $params['query'] = [];
if (isset($extra['post'])) {
Expand Down
19 changes: 19 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\Network\Request;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\RequestActionTrait;
use Cake\Routing\Router;
Expand Down Expand Up @@ -222,6 +223,24 @@ public function testRequestActionParamParseAndPass()
$this->assertNull($result['params']['plugin']);
}

/**
* Test that requestAction() is populates the base and webroot properties properly
*
* @return void
*/
public function testRequestActionBaseAndWebroot()
{
$request = new Request([
'base' => '/subdir',
'webroot' => '/subdir/'
]);
Router::setRequestInfo($request);
$result = $this->object->requestAction('/request_action/params_pass');
$result = json_decode($result, true);
$this->assertEquals($request->base, $result['base']);
$this->assertEquals($request->webroot, $result['webroot']);
}

/**
* test that requestAction does not fish data out of the POST
* superglobal.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_app/TestApp/Controller/RequestActionController.php
Expand Up @@ -116,6 +116,8 @@ public function query_pass()
public function params_pass()
{
$this->response->body(json_encode([
'base' => $this->request->base,
'webroot' => $this->request->webroot,
'params' => $this->request->params,
'query' => $this->request->query,
'url' => $this->request->url,
Expand Down

0 comments on commit 7204e18

Please sign in to comment.