Normally, you would not mix REST and non-REST functions in the same controller, but you would have specific controllers for your REST API, which extend Controller_Rest instead of Controller.
Output handing of standard controllers isn't designed to return raw json.
And you can also use the format class to convert to json, no need to do that manually.
Thanks for your answer Harro, I do that because in this way I can reuse most of the code already written in the controllers, otherwise I must duplicate my controllers in REST controllers that would do the same things...It's right?
I'm not stopping you, I'm only highlighting best practice. Fuel doesn't care. The only thing you have to look out for is that it outputs exactly what you think it does.
As to why you don't get your json returned, I don't know. You'll have to debug.
Ok I get it! thanks...I tried to print my output before I send it:
This is the result of the first json_encode($a),when the model return one row: (It works)
{"id":"1","name":"Jhon","surname":"Larry","age":"25"} This is the result of the second json_encode($a),when the model return more than one row: (doesn't works) [{"id":"1","name":"Jhon","surname":"Larry","age":"25"},{"id":"4","name":"Matt","surname":"Damon","age":"38"}] I don't know why I can't receive the second one in my view...I'm going to go mad!
This is all very weird, since a { } means it's an object, not an array. It translates to:
array (size=2) 0 => object(stdClass)[29] public 'id' => string '1' (length=1) public 'name' => string 'Jhon' (length=4) public 'surname' => string 'Larry' (length=5) public 'age' => string '25' (length=2) 1 => object(stdClass)[30] public 'id' => string '4' (length=1) public 'name' => string 'Matt' (length=4) public 'surname' => string 'Damon' (length=5) public 'age' => string '38' (length=2)
So what works is an object, what doesn't work is an array of objects. Which does not match your code example, somewhere something else happens, to_array() doesn't return objects...
I'm sorry, I haven't explained well...the result that I introduced to you, is referred to the json_encode($a)
so is correct that the result is an object (specifically a JSON)...and is exactly what you said (what works is a json (object), what doesn't work is a json with more than one element)