ajax requests through a single point of entry
Im building a little mvc style framework, with a single point of entry through the index in the public folder. I am now passing all my ajax requests through this, and directing them to the appropriate controllers in the private application directory.
What are the pros and cons of doing it like this?
开发者_JS百科should I do be doing it like this? or another way?
Initially probably not many cons. As Pickle pointed out you have everything in one area so easy to find.
I would say that functionality should stay in the controller where it belongs. Just because the input and output are AJAX shouldn't matter. In fact, you could have a method in a controller like a show user method that could either output an HTML page OR AJAX data. No reason to put that stuff in a separate AJAX controller when it belongs in the User controller.
Honestly this is all design decisions. It's just my preference to keep things where they belong logically and approach AJAX as an input and output problem and build your methods to handle that in/out.
We rolled our own framework and one thing we build was a router that handles incoming traffic. And ajax calls have a .json at the end. This is then available to the controllers and if a controller supports ajax requests it will output valid JSON data instead of passing the data off to the view and then displaying HTML.
Pros: You don't have to search through a bunch of code to find out where your AJAX request is being handled.
Cons: None I can think of.
The only potential downside would be any "expensive" bootstrapping going on that the AJAX doesn't need which may slow down response times. APC may help or negate this, though, depending on what you are doing, and you could probably tailor the bootstrap process to handle lightweight requests.
FWIW, the normal Drupal method for AJAX funnels everything through the same place as normal pages. This is also done to help provide a fallback when the user doesn't have JS enabled.
精彩评论