Hullo. I'm just beginning with FuelPHP and I like it. It feels really friendly coming from CodeIgniter, and I am starting out with a tutorial. It presents me with the following code: (I've converted it to shortened pseudo-code)
$messages = Model_Message::find('all');
foreach ($messages as $message) {
$query = DB::get_comment();
}
Should I use that in my actual production code, or is it there for the sake of simplicity? I feel like it would be better to do some sort of join, rather than a ton of queries. What should I do?
Thank you. That does look nicer to me. I didn't want to make a separate thread for another question I have.
I want to make a RESTful application where I can make 1 controller per resource, but also have different front ends like web, android, IOS, and maybe native apps for desktops.
I believe I would output JSON (I prefer this over XML) responses, but I am unsure of how to make that work with views (keeping views out of the controller)
Restful controllers should extend Controller_Rest. Ideally you don't mix them with frontend functionality (altough you could using Controller_Hybrid), also think about proper versioning of your API, and about consistency in your responses.
Controller_Rest uses automatic detection, it supports multiple response types, based on what the client requests.
Okay, I see that, but I'm not understanding how I would go about making it render a view if it comes from the web.
class Controller_Test extends Controller_Rest
{
public function get_index()
{
return $this->response(array(
'hello'=>'world'
));
}
}
localhost/test.json gives me a JSON output like I would want. I'm trying to make it so that if there is no extension, then it will send the data to a view and display that
Either you have an API, which returns a block of data in some form (json or other), or you have an interactive application that returns html to be rendered in a browser.
Ideally you should not mix the two, and you should absolutely not have the two functions under the same URI, that is going to lead to complex code and a maintenance nightmare.
If you insist you can use Controller_Hybrid, and do something like
"Either you have an API, which returns a block of data in some form"
Alright, alright. Does this mean my web app has to be ajax based? That doesn't sound entirely bad, but it seems like it would cause there to be more HTTP requests than necessary, i.e.
Visits test.php and fetches assets
Then an AJAX request is made to the controller to fetch the data?
that doesn't sound right, so maybe i'm crazy. Is there a guide anywhere for doing what you're explaining to me and I'm missing?
What I meant was that you should not design an application where "/controller/method/parameter" returns a data structure in one case, an html webpage in another. Keep your API and your web frontend separate.
I usually use the prefix "api", which will be a folder in the Controller directory. You could version that using further folders, like "v1", etc. You then get URI's like "/api/v1/users/get/10", which will then call Controller_Api_V1_Users.
It allows you to develop your api separate from your frontend application, and keep it simple to maintain and expand in the future.
If you setup your API properly, you can use HMVC requests you have your web frontend controller call your API controller to fetch the required data. You should return an array or object instead of json then, it saves you a bit of parsing.