Skip to content

Commit

Permalink
Merge pull request #3407 from mhsdesign/bugfix/mvc-dispatcher-refacto…
Browse files Browse the repository at this point in the history
…ring-followup

BUGFIX: Fix use of `$this->response->set...` in controllers
  • Loading branch information
kitsunet authored Oct 28, 2024
2 parents c02e067 + 96958c2 commit f1d1bb9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
16 changes: 1 addition & 15 deletions Neos.Flow/Classes/Mvc/ActionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ class ActionRequest
*/
protected $format = '';

/**
* If this request has been changed and needs to be dispatched again
* @var boolean
*/
protected $dispatched = false;

/**
* The parent request – either another sub ActionRequest a main ActionRequest or null
* @var ?ActionRequest
Expand Down Expand Up @@ -656,14 +650,6 @@ public function getFormat(): string
return $this->format;
}

/**
* Resets the dispatched status to false
*/
public function __clone()
{
$this->dispatched = false;
}

/**
* We provide our own __sleep method, where we serialize all properties *except* the parentRequest if it is
* a HTTP request -- as this one contains $_SERVER etc.
Expand All @@ -672,7 +658,7 @@ public function __clone()
*/
public function __sleep()
{
$properties = ['controllerPackageKey', 'controllerSubpackageKey', 'controllerName', 'controllerActionName', 'arguments', 'internalArguments', 'pluginArguments', 'argumentNamespace', 'format', 'dispatched'];
$properties = ['controllerPackageKey', 'controllerSubpackageKey', 'controllerName', 'controllerActionName', 'arguments', 'internalArguments', 'pluginArguments', 'argumentNamespace', 'format'];
if ($this->parentRequest instanceof ActionRequest) {
$properties[] = 'parentRequest';
}
Expand Down
9 changes: 6 additions & 3 deletions Neos.Flow/Classes/Mvc/Controller/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function processRequest(ActionRequest $request): ResponseInterface
$this->initializeView($this->view);
}

$httpResponse = $this->callActionMethod($request, $this->arguments, $response->buildHttpResponse());
$httpResponse = $this->callActionMethod($request, $this->arguments, $response);

if (!$httpResponse->hasHeader('Content-Type')) {
$httpResponse = $httpResponse->withHeader('Content-Type', $this->negotiatedMediaType);
Expand Down Expand Up @@ -512,9 +512,9 @@ protected function initializeAction()
*
* @param ActionRequest $request
* @param Arguments $arguments
* @param ResponseInterface $httpResponse The most likely empty response, previously available as $this->response
* @param ActionResponse $response The most likely empty response, previously available as $this->response
*/
protected function callActionMethod(ActionRequest $request, Arguments $arguments, ResponseInterface $httpResponse): ResponseInterface
protected function callActionMethod(ActionRequest $request, Arguments $arguments, ActionResponse $response): ResponseInterface
{
$preparedArguments = [];
foreach ($arguments as $argument) {
Expand Down Expand Up @@ -555,6 +555,9 @@ protected function callActionMethod(ActionRequest $request, Arguments $arguments
}
}

// freeze $response previously available as $this->response
$httpResponse = $response->buildHttpResponse();

if ($actionResult instanceof ResponseInterface) {
return $actionResult;
}
Expand Down
10 changes: 10 additions & 0 deletions Neos.Flow/Tests/Functional/Mvc/ActionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public function defaultTemplateIsResolvedAndUsedAccordingToConventions()
self::assertEquals('Fourth action <b>[email protected]</b>', $response->getBody()->getContents());
}

/**
* @test
*/
public function requestAndResponseAreAvailableInTheAction()
{
$response = $this->browser->request('http://localhost/test/mvc/actioncontrollertesta/fifth?argument=the-value');
self::assertEquals('Fifth action (fifth) with: "the-value"', $response->getBody()->getContents());
self::assertEquals('Hello World', $response->getHeaderLine('X-Foo'));
}

/**
* Bug #36913
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public function fourthAction($emailAddress)
$this->view->assign('emailAddress', $emailAddress);
}

/**
* Tests response and request
*/
public function fifthAction()
{
$this->response->setHttpHeader('X-Foo', 'Hello World');
return sprintf('Fifth action (%s) with: "%s"', $this->request->getControllerActionName(), $this->request->getArgument('argument'));
}

/**
* @param string $putArgument
* @param string $getArgument
Expand Down

0 comments on commit f1d1bb9

Please sign in to comment.