O'Reilly logo

Yii Project Blueprints by Charles R. Portwood II

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

Extending Yii to return data

There are two approaches to having Yii render the JSON or XML data. The first and the easiest approach is to create a JSON or XML view file and, from every action, call $this->render('json'). While this is simple, it forces us to store a lot of information and explicitly call the render() method in each action. If we're extending a class that modifies the render() method, this can be extremely problematic if we want to make changes later. Another issue with this approach is that it treats errors as separate response types. When throwing an error with this approach, Yii will want to render the error as HTML rather than JSON. Depending upon our logging and debug level, this can cause our API to return the wrong data ...

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