CakePHP 4 - JSON and XML views

Поделиться
HTML-код
  • Опубликовано: 8 фев 2025
  • Covers
    How to get clients to request XML and JSON via extension (.xml/.json) or Accept headers (application/json, text/xml).
    How to do auto-magic XML and JSON views using the serialize option key.
    Serving JSON and XML views use view templates.
    4:48 Bypass Authentication to an Action to when using CakeDC/Users plugin to allow unauthenticated requests

Комментарии • 2

  • @KevinPfeifer-zr4cq
    @KevinPfeifer-zr4cq 2 года назад +1

    The reason why CakePHP uses XML as "default" when specifying the viewClasses method + the "serialize" option is because of what the browser sends the server as a HTTP "Accept" header. If you look at that value in the browsers network tab you will see the value: text/html,application/xhtml+xml .........etc
    And that matches what is specified in the "contentType()" of the XmlView inside Cake.
    This behavior however would not happen if you remove the "serialize" call at the end of your controllers action.

    • @toggenation
      @toggenation  2 года назад +1

      Thanks Kevin that makes perfect sense as to why XmlView is displayed by default when specifying both JsonView and XmlView.
      Removing serialize and specifying JsonView only in the viewClasses method and making the request with a browser returns the default AppView (HTML). It will only return JSON when an Accept application/json header is set. I was expecting (incorrectly) to only receive views I had specified in viewClasses. I live and learn.