O'Reilly logo

Zend Framework 2 Cookbook by Josephus Callaars

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using context switching for a different output

Not only do we want to be able to output the content through different view strategies, we sometimes also want to do this on demand, so that we can switch the output, for example, from HTML to JSON by simply changing the headers in our request.

Getting ready

In some cases (for example, in the REST servers) it is necessary to switch the response output of the content depending on what the user asks for. The user can add an Accept header to let the server know which output formats it accepts, for example application/json, and text/html.

What we are going to do is create a simple website that will output a text/html format on default (which is normal), but it will also output a JSON string whenever we have ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required