diff --git a/src/View/AjaxView.php b/src/View/AjaxView.php index 94b08a0e10aa..f0fa4ae491e3 100644 --- a/src/View/AjaxView.php +++ b/src/View/AjaxView.php @@ -16,10 +16,6 @@ */ namespace Cake\View; -use Cake\Event\EventManager; -use Cake\Http\Response; -use Cake\Http\ServerRequest; - /** * A view class that is used for AJAX responses. * Currently only switches the default layout and sets the response type - which just maps to @@ -33,23 +29,11 @@ class AjaxView extends View protected $layout = 'ajax'; /** - * Constructor - * - * @param \Cake\Http\ServerRequest|null $request The request object. - * @param \Cake\Http\Response|null $response The response object. - * @param \Cake\Event\EventManager|null $eventManager Event manager object. - * @param array $viewOptions View options. + * @inheritDoc */ - public function __construct( - ?ServerRequest $request = null, - ?Response $response = null, - ?EventManager $eventManager = null, - array $viewOptions = [] - ) { - if ($response) { - $response = $response->withType('ajax'); - } - - parent::__construct($request, $response, $eventManager, $viewOptions); + public function initialize(): void + { + parent::initialize(); + $this->setResponse($this->getResponse()->withType('ajax')); } } diff --git a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php index 0d673a834744..e26fa9105b8a 100644 --- a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php +++ b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php @@ -391,6 +391,7 @@ public function testAutoAjaxLayout(): void $view = $this->Controller->createView(); $this->assertInstanceOf(AjaxView::class, $view); $this->assertSame('ajax', $view->getLayout()); + $this->assertSame((new Response(['type' => 'ajax']))->getType(), $view->getResponse()->getType()); $this->_init(); $this->Controller->setRequest($this->Controller->getRequest()->withParam('_ext', 'js'));