views:

695

answers:

8

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?

+1  A: 

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.

Ólafur Waage
+1  A: 

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.

Mario
+4  A: 

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.

Tom Wright
@Mario seems to agree to. Does that count as consensus?
Tom Wright
indeed, don't change the controllers, the actions are the same. Just change your views (let AJAX views return JSON or something and "normal" views html)
Pim Jager
+1  A: 

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.

dave paola
+1  A: 

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.

Pras
+4  A: 

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.

thomasrutter
+1  A: 

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)

Darryl E. Clarke
A: 

You could wrap it up as a general REST-api, and use RESTful conventions and URIs. Example:

Instead of index.php/ajax/editprofile it could be a PUT request to index.php/api/profile/profilename.

finpingvin