Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion Neos.Flow/Classes/Mvc/ActionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@
* via `$this->response` {@see AbstractController::$response} and pass it along to places.
* But this behaviour is deprecated!
*
* Instead, you can directly return a PSR repose {@see \GuzzleHttp\Psr7\Response} from a controller:
* Instead of modifying the repose via $this->response like
*
* - $this->response->addHttpHeader
* - $this->response->setHttpHeader
* - $this->response->setContentType
* - $this->response->setStatusCode
*
* you can directly return a PSR repose {@see \GuzzleHttp\Psr7\Response} from a controller.
*
* *set status code and contents and additional header:*
*
* ```php
* public function myAction()
Expand All @@ -38,6 +47,28 @@
* }
* ```
*
* *modify a view response with additional header:*
*
* ```php
* public function myAction()
* {
* $response = $this->view->render();
* if (!$response instanceof Response) {
* $response = new Response(body: $response);
* }
* return $response->withAddedHeader('X-My-Header', 'foo');
* }
* ```
*
* *render json without using the legacy json view:*
*
* ```php
* public function myAction()
* {
* return new Response(body: json_encode($data, JSON_THROW_ON_ERROR), headers: ['Content-Type' => 'application/json']);
* }
* ```
*
* @deprecated with Flow 9
* @Flow\Proxy(false)
*/
Expand Down
12 changes: 12 additions & 0 deletions Neos.Flow/Classes/Mvc/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* source code.
*/

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Neos\Error\Messages as Error;
use Neos\Flow\Annotations as Flow;
Expand Down Expand Up @@ -63,6 +64,17 @@ abstract class AbstractController implements ControllerInterface

/**
* The legacy response which will is provide by this action controller
*
* Legacy ways to modify a repose:
*
* - $this->response->addHttpHeader
* - $this->response->setHttpHeader
* - $this->response->setContentType
* - $this->response->setStatusCode
*
* Please return a new {@see Response} instead from your controller action.
* Documentation to adjust your code is provided in {@see ActionResponse}.
*
* @var ActionResponse
* @deprecated with Flow 9 {@see ActionResponse}
*/
Expand Down
30 changes: 11 additions & 19 deletions Neos.Flow/Classes/Mvc/Controller/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
* source code.
*/

use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Neos\Error\Messages as Error;
use Neos\Error\Messages\Result;
use Neos\Flow\Annotations as Flow;
use Neos\Error\Messages as Error;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Mvc\ActionRequest;
Expand All @@ -36,7 +37,6 @@
use Neos\Flow\Reflection\ReflectionService;
use Neos\Utility\TypeHandling;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -563,7 +563,15 @@ protected function callActionMethod(ActionRequest $request, Arguments $arguments
}

if ($actionResult === null && $this->view instanceof ViewInterface) {
return $this->renderView($httpResponse);
$result = $this->view->render();

if ($result instanceof Response) {
// merging of the $httpResponse (previously $this->response) was previously done to a limited extend via the use of replaceHttpResponse.
// With Flow 9 the returned response will overrule any changes made to $this->response as there is no clear way to merge them.
return $result;
}

return $httpResponse->withBody($result);
}

return $httpResponse->withBody(Utils::streamFor($actionResult));
Expand Down Expand Up @@ -828,20 +836,4 @@ protected function getErrorFlashMessage()
{
return new Error\Error('An error occurred while trying to call %1$s->%2$s()', null, [get_class($this), $this->actionMethodName]);
}

/**
* Renders the view and returns the psr response.
*
* If a stream is returned it will be applied (to the most likely empty response) which was previously available as $this->response.
*/
protected function renderView(ResponseInterface $httpResponse): ResponseInterface
{
$result = $this->view->render();

if ($result instanceof StreamInterface) {
return $httpResponse->withBody($result);
}

return $result;
}
}
11 changes: 10 additions & 1 deletion Neos.Flow/Classes/Mvc/View/JsonView.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
/**
* A JSON view
*
* @deprecated please use json_encode instead
* please return a new response instead in your controller and set the Content-Type to application/json
*
* ```php
* public function myAction()
* {
* return new Response(body: json_encode($data, JSON_THROW_ON_ERROR), headers: ['Content-Type' => 'application/json']);
* }
* ```
*
* @deprecated with Flow 9.0 please use the native json_encode instead, without relying on the flow object conversion magic
*/
class JsonView extends AbstractView
{
Expand Down