Hi, I'm totally new in using fuelphp, actually I'm a newbie at php programming, but i'm not new as being a programmer. Currently I need to create a web api for my mobile application, and I think fuelphp looks great for creating api, by utilizing its built in rest controller. Notes: I'm using fuelphp v1.5.3 on AMPPS on Mac.
I started by using database table example from fuel installation (fuel_intro.messages) and created one model and one controller as follows:
When you set a response, the Reponse class will have to convert your response to a string to be able to echo it to the browser.
The error message says you can't echo out a database response object. You can't echo any object, unless that object has __toString() implemented to convert the object to a string representation.
What I need is to get the query result as json or xml so that my mobile app could use it. I thought by echoing out the response to the browser, it will show that the response is correct. I read somewhere in the documentation that by supplying different file extension e.g: json, xml, csv, etc... fuelphp will give the appropriate format as the response (default to config setting).
Do you have any suggestion to make my previous code working? To make it able to send query result as json or xml to my mobile app? Thanks.
If you want to build an API, have your controller extend Controller_Rest (for API only) or Controller_Hybrid (for a mix of REST and HTML output).
It will take care of the output conversion for you (json, xml, etc). In this case the response need to be an array, so your DB query needs to return an array instead of an object (check the docs).