I have an application that uses JSON to comunicate the front end with the service.

After a few tests with yii i have an idea, but i don't know if it's the best to offer JSON by the MVC pattern.

  1. Extend CController to a CJsonController and it catch all the action return the JSON Objet to serialize

  2. Override CJsonController::run method to this:

	public function run($actionID)


               echo json_encode(parent::run($actionID));


  1. return the object to serialize in the action method.

Another way is to make a widget to transform a parameter to JSON. It will be a little more extensible than this way and the controller remains as the base class.

I don't know which one use or if you know some other way.

What do you think?



Have you considered defining a filter?

No. Could you give an example?

This is about creating a new filter: http://www.yiiframew…n.create#filter

Once a filter is created, you can use it to apply to all actions in a controller or just a few of them. This is very flexible.

Thank qiang for your answer!!

I understand the filter theory, but how can i modify the data?? I must create a filter for each JSON RESPONSE??

How will the filter modify the data must show ??? Maybe an Interface in the Controller or something?

I can't see the best implementation of this with filter.

Sorry, I misunderstood what you wanted to do.

Why don't you just call json_encode() in your actions to get the needed JSON responses? Creating a JsonController as you described is fine, but it seems to me complicate things unnecessarily.

Ok!  Thank you!!

i thought that, but i also thought that it must be in the view and not in the controller.

But i think is fastest if it is in the view.

Maybe my view is the json_encode function. Am i correct?

Thanks!! Yii is a really powerfull and awesome framework!!

Good job!

Because you are returning some structured data rather than text strings, it is inappropriate to use views here.

Another way to solve your problem (if you still want to avoid calling json_encode explicitly in each action) is that you declare a base action class. In its run() method you do JSON encoding.

class JsonAction extends CAction


   public function run()


        echo json_encode($this->runJson());



class Action1 extends JsonAction


    protected function runJson()


        // generate data 

        // return data



Hey, that's cool!!

Thank you!

It's excellent this solution because i can change my actions dynamically changing the actions() function.

Have to bring back this rather old topic: Is there an easy way to create a JSON object from a AR query? This doesn't do, what i thought:



// both give [{"isNewRecord":false},{"isNewRecord":false}]

echo json_encode($projects);

echo CJSON::encode($projects);

So ActiveRecords must be JSON encoded manually?

Something like this:



mikl has an array of AR to encode, I think that's the problem.

I think we can implement array access interface for model so that it can be properly encoded. Could you please create a ticket for this? Thanks.

Done. :)

So fast…

I also wondered, what the CJSON::nameValue() method is for and how it's used. It would be nice, if you could specify the AR attributes that i need in my JSON response somehow (i don't want all of them). Is that what nameValue() is for?

I could use CDbCriteria's select property, to only fetch the columns i want in my response. But what about custom attributes again?


class Customer extends CActiveRecord {

    public function getFullName()


        return $this->firstname.' '.$this->lastname;


That's a good question. I can't think of a good solution.

Perhaps it should use safeAttributes()?  So you could define a new scenario to choose which attributes are safe?

Not sure using safeAttributes() is good because it is often needed to get attributes including those that are not safe (such as PK).