Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kohana - where do you put AJAX scripts?

I am using Kohana but this question applies to Rails, CI, or any other MVC web development framework. Where is the best place to stick one's server side AJAX scripts?

I was planning on creating an Ajax_Controller and using a method/action per individual script.

For example, a login form on the home page index.php/home would send an XMLHttpRequest to index.php/ajax/login, and the edit profile form index.php/profile/edit would send an XMLHttpRequest to index.php/ajax/editprofile. What's the best practice?

like image 693
GeekJock Avatar asked Apr 05 '09 14:04

GeekJock


8 Answers

I tend to put my ajax actions in the same controller as the non-ajax actions for any given model.

When I can, I try to use the same actions and only change the output type. Most tasks should have a non-ajax version anyway, so this tends to work quite well. Very handy for reducing logic duplication.

like image 68
Tom Wright Avatar answered Oct 02 '22 23:10

Tom Wright


AJAX crosses all of the MVC boundaries. That is, it doesn't go into just one of model, view or controller.

  • Your AJAX scripts will be calling scripts on your site - so this would involve a section of your controller layer which you've created for the purpose.
  • That controller in turn would access the database using the interface provided by your model layer, just as a non-AJAX request would.
  • The data for the response back to the client may be packaged as JSON or XML or something. Technically this is the task of your view layer, though if your application's definition of a view layer is nothing more than "an HTML templating system" rather than "processing and formatting anything that gets sent back to the client whether it's HTML or something else like XML" then your XML or JSON generation may need to go into a new little section of its own.

As for sending the scripts (Javascript files) themselves, this is probably going to be handled directly by the web server rather than from within your MVC framework.

like image 29
thomasrutter Avatar answered Oct 01 '22 23:10

thomasrutter


Do you make different controllers for GET and POST requests? I don't. In my opinion, JS requests shouldn't be dealt with differently either.

I personally see JS requests just like GET, POST or any other type of request. So if I have user-related JS-based actions, I simply create them in the user controller.

like image 27
Mario Avatar answered Oct 01 '22 23:10

Mario


If you mean the AJAX (Javascript) scripts themselves, these should go into your public/js folder. However, if you mean the actions invoked by these AJAX requests, they should be treated as any other actions of the respective controllers. To be completely RESTful, you should be using a different format (json, xml, etc.) as return values for those actions.

like image 43
Pras Avatar answered Oct 02 '22 23:10

Pras


I am a noob, but based on my understanding, to achieve ajax with php mvc... thinking steps might be:

  • change the definition/function of the existing php view layer from 'HTML template' into 'results formatting (XML,JSON etc..' -> results from relevant module, which then called by controller to output into AJAX object, then it means you need to write view layers into each particular class with formatting methods
  • PHP module layer stays same
  • build a Ajax router class with JS which stay the same structure which you route in your PHP
  • build a ajax results handler class with JS to handle the results got back from PHP controllers (XML JSON etc..), then from here do whatever user interactions you want, this will be called by above Ajax router class

So,

ajax router (send XMLhttprequest)
-> PHP controllers  C
-> PHP module -> PHP view results M
-> PHP controllers output results V
-> ajax results handle (into page)
like image 37
timm Avatar answered Oct 01 '22 23:10

timm


I don't use Kohana but what I do in my framework is that AJAX scripts are controllers. I try to treat them as standalone controllers but in the end they are just controllers.

like image 43
Ólafur Waage Avatar answered Oct 03 '22 23:10

Ólafur Waage


Using a separate controller is a good idea. I either organize my controllers by function and then actions by return type.

Additionally, when I'm using Pylons I can decorate an action with @jsonify and that will automatically take care of converting python objects to JSON. Very handy.

like image 24
dave paola Avatar answered Oct 01 '22 23:10

dave paola


I like to keep all my ajax requests in one controller, typically dispatching their requests through a shared model (that the non ajax controller also uses)

The main difference being the view that results via the ajax controller (html fragments, json data, etc) or the non-ajax controller (full pages)

like image 39
Darryl E. Clarke Avatar answered Oct 01 '22 23:10

Darryl E. Clarke