@@ -282,14 +282,14 @@ This would render **plugins/Users/templates/UserDetails/custom_file.php**
282282Content Type Negotiation
283283========================
284284
285- .. php :method :: viewClasses ()
285+ .. php :method :: addViewClasses ()
286286
287287 Controllers can define a list of view classes they support. After the
288288controller's action is complete CakePHP will use the view list to perform
289- content-type negotiation with either :ref: `file-extensions ` or ``Content-Type ``
289+ content-type negotiation with either :ref: `file-extensions ` or ``Accept ``
290290headers. This enables your application to re-use the same controller action to
291291render an HTML view or render a JSON or XML response. To define the list of
292- supported view classes for a controller is done with the ``viewClasses () ``
292+ supported view classes for a controller is done with the ``addViewClasses () ``
293293method::
294294
295295 namespace App\Controller;
@@ -299,25 +299,25 @@ method::
299299
300300 class PostsController extends AppController
301301 {
302- public function viewClasses (): array
302+ public function initialize (): void
303303 {
304- return [JsonView::class, XmlView::class];
304+ parent::initialize();
305+
306+ $this->addViewClasses([JsonView::class, XmlView::class]);
305307 }
306308 }
307309
308310The application's ``View `` class is automatically used as a fallback when no
309311other view can be selected based on the request's ``Accept `` header or routing
310312extension. If your application only supports content types for a specific
311- actions, you can define that logic within `` viewClasses () ``::
313+ actions, you can call `` addClasses () `` within your action too ::
312314
313- public function viewClasses (): array
315+ public function export (): void
314316 {
315- if ($this->request->getParam('action') === 'export') {
316- // Use a custom CSV view for data exports.
317- return [CsvView::class];
318- }
317+ // Use a custom CSV view for data exports.
318+ $this->addViewClasses([CsvView::class]);
319319
320- return [JsonView::class];
320+ // Rest of the action code
321321 }
322322
323323If within your controller actions you need to process request or load data
@@ -331,9 +331,9 @@ differently based on the controller action you can use
331331 $query->contain('Authors');
332332 }
333333
334- You can also set your controllers' supported view classes
335- using the ``addViewClasses () `` method which will merge the provided views with
336- those held in the `` viewClasses `` property .
334+ In case your app need more complex logic to decide which view classes to use
335+ then you can override the ``Controller::viewClasses () `` method and return
336+ an array of view classes as required .
337337
338338.. note ::
339339 View classes must implement the static ``contentType() `` hook method to
@@ -344,13 +344,15 @@ Content Type Negotiation Fallbacks
344344
345345If no View can be matched with the request's content type preferences, CakePHP
346346will use the base ``View `` class. If you want to require content-type
347- negotiation, you can use the ``NegotiationRequiredView `` which sets a 406 status
347+ negotiation, you can use the ``NegotiationRequiredView `` which sets a `` 406 `` status
348348code::
349349
350- public function viewClasses (): array
350+ public function initialize (): void
351351 {
352+ parent::initialize();
353+
352354 // Require Accept header negotiation or return a 406 response.
353- return [JsonView::class, NegotiationRequiredView::class];
355+ $this->addViewClasses( [JsonView::class, NegotiationRequiredView::class]) ;
354356 }
355357
356358You can use the ``TYPE_MATCH_ALL `` content type value to build your own fallback
@@ -372,6 +374,22 @@ view logic::
372374It is important to remember that match-all views are applied only *after *
373375content-type negotiation is attempted.
374376
377+ Using AjaxView
378+ ==============
379+
380+ In applications that use hypermedia or AJAX clients, you often need to render
381+ view contents without the wrapping layout. You can use the ``AjaxView `` that
382+ is bundled with the application skeleton::
383+
384+ // In a controller action, or in beforeRender.
385+ if ($this->request->is('ajax')) {
386+ $this->viewBuilder()->setClassName('Ajax');
387+ }
388+
389+ ``AjaxView `` will respond as ``text/html `` and use the ``ajax `` layout.
390+ Generally this layout is minimal or contains client specific markup. This
391+ replaces usage of ``RequestHandlerComponent `` automatically using the
392+ ``AjaxView `` in 4.x.
375393
376394Redirecting to Other Pages
377395==========================
@@ -476,8 +494,8 @@ behaves::
476494 {
477495 protected array $paginate = [
478496 'Articles' => [
479- 'conditions' => ['published' => 1]
480- ]
497+ 'conditions' => ['published' => 1],
498+ ],
481499 ];
482500 }
483501
0 commit comments