From 8c0aad9439f6fbf63ab0dd94767de7fa439cbb5d Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 11 Oct 2023 13:21:21 +0530 Subject: [PATCH 1/2] Promote use of addViewClasses() instead of overriding viewClasses() --- en/controllers.rst | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/en/controllers.rst b/en/controllers.rst index e23efdff7d..88f6c47aa6 100644 --- a/en/controllers.rst +++ b/en/controllers.rst @@ -282,14 +282,14 @@ This would render **plugins/Users/templates/UserDetails/custom_file.php** Content Type Negotiation ======================== -.. php:method:: viewClasses() +.. php:method:: addViewClasses() Controllers can define a list of view classes they support. After the controller's action is complete CakePHP will use the view list to perform content-type negotiation with either :ref:`file-extensions` or ``Content-Type`` headers. This enables your application to re-use the same controller action to render an HTML view or render a JSON or XML response. To define the list of -supported view classes for a controller is done with the ``viewClasses()`` +supported view classes for a controller is done with the ``addViewClasses()`` method:: namespace App\Controller; @@ -299,25 +299,23 @@ method:: class PostsController extends AppController { - public function viewClasses(): array + public function initialize(): void { - return [JsonView::class, XmlView::class]; + $this->addViewClasses([JsonView::class, XmlView::class]); } } The application's ``View`` class is automatically used as a fallback when no other view can be selected based on the request's ``Accept`` header or routing extension. If your application only supports content types for a specific -actions, you can define that logic within ``viewClasses()``:: +actions, you can call ``addClasses()`` within your action too:: - public function viewClasses(): array + public function export(): void { - if ($this->request->getParam('action') === 'export') { - // Use a custom CSV view for data exports. - return [CsvView::class]; - } + // Use a custom CSV view for data exports. + $this->addViewClasses([CsvView::class]); - return [JsonView::class]; + // Rest of the action code } If within your controller actions you need to process request or load data @@ -331,9 +329,9 @@ differently based on the controller action you can use $query->contain('Authors'); } -You can also set your controllers' supported view classes -using the ``addViewClasses()`` method which will merge the provided views with -those held in the ``viewClasses`` property. +In case your app need more complex logic to decide which view classes to use +then you can override the ``Controller::viewClasses()`` method and return +an array of view classes as required. .. note:: View classes must implement the static ``contentType()`` hook method to @@ -344,13 +342,13 @@ Content Type Negotiation Fallbacks If no View can be matched with the request's content type preferences, CakePHP will use the base ``View`` class. If you want to require content-type -negotiation, you can use the ``NegotiationRequiredView`` which sets a 406 status +negotiation, you can use the ``NegotiationRequiredView`` which sets a ``406`` status code:: - public function viewClasses(): array + public function initialize(): void { // Require Accept header negotiation or return a 406 response. - return [JsonView::class, NegotiationRequiredView::class]; + $this->addViewClasses([JsonView::class, NegotiationRequiredView::class]); } You can use the ``TYPE_MATCH_ALL`` content type value to build your own fallback From 62d646fd5bca92f02a09347560436209e0ed5323 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 12 Oct 2023 18:34:45 +0530 Subject: [PATCH 2/2] Make code examples newbie friendly --- en/controllers.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/en/controllers.rst b/en/controllers.rst index 88f6c47aa6..6f6e2810fa 100644 --- a/en/controllers.rst +++ b/en/controllers.rst @@ -301,6 +301,8 @@ method:: { public function initialize(): void { + parent::initialize(); + $this->addViewClasses([JsonView::class, XmlView::class]); } } @@ -347,6 +349,8 @@ code:: public function initialize(): void { + parent::initialize(); + // Require Accept header negotiation or return a 406 response. $this->addViewClasses([JsonView::class, NegotiationRequiredView::class]); }