Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
is_ajax() or extension()
  • I've written a controller and the index action serves page with a datatable when loaded in browser and returns the datatable elements JSON when reached with ajax request.

    My question is: which above mentioned function is better to use?

    http://bin.fuelphp.com/~ut

    The first one's advantage: the JSON itself can only be reached through AJAX request (or with ajax headers sent).

    The second one's advantage: can be debuged in browser.

    I am also curious about the performance hit of the two functions.
  • From a best practice point of view, your REST API should be separate from your interactive pages. So I would not build something like this.

    Also, you can only do this with Controller_Hybrid, and as of 1.7.1 is can handle this itself (although not with a datatable but with a JSON dump), but not when in production (it is considered a security risk if your API responses can be dumped into a browser).
  • What do you mean by "not with a datatable but with a JSON dump"?

    Datatable needs a "JSON dump"

    I checked Hybrid Controller and it uses is_ajax().
  • I mean HTML, text.

    I consider it bad practice if a REST API returns data on a browser request. I can understand it's useful for development purposes, so Controller_Rest (as of 1.7.1) will dump an array returned by an action method as a JSON "printout" if the requested format is not compatible (notably "html", the default of a browser request) when not running in a production environment, and will return an error (406 NOT ACCEPTABLE) when in production.

    If your application has a functional requirement to provide the same data both through an API and through a web page, best practices dictate they should be two different functions, in two different controllers.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion