Book Image

Yii Project Blueprints

By : Charles R. Portwood ll
Book Image

Yii Project Blueprints

By: Charles R. Portwood ll

Overview of this book

Table of Contents (15 chapters)
Yii Project Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 to our client.

A more preferable approach is to simply return the data that we want to present to the client in each action and have our parent controller class handle the rendering and output. This approach makes it easier to identify what data is...