I’m a heavy Codeignitor user and currently I have to build a page with extensive AJAX bits and pieces. I have been using JQuery all along the website and it’s AJAX handling was perfect up until now. There is something that just doesn’t feel right when i use a MVC with JQuery.
For example:
in Jquery I setup the callback URL easily that point to the controller, the controler then calls a view page and the AJAX is displayed but this view page is very small PHP code.
So now I have about 40 ajax function to do in my page, would that mean I would have to make 40 views?? that doesn’t seem right to me.
Is there any better way to handle/manage the views created for the AJAX in MVC Frameworks
Thank you.
You could create one view, e.g. ajax.php and set the content accordingly in the methods of the controllers. All methods can use the same view.
The above is only true for similar views, but if the methods are similar as well, then you should think about refactoring your code to using uri parameters, see my answer to a question on how to get parameters from the url.
But all that depends on the nature of the controllers, their methods and the related views.
EDIT:
I do not advise you to use HTML in the controller, but lay out your controller like this:
Create views like success.php
And call it via
$.get('/index.php/my_controller/edit_via_ajax/name/John_Doe');in jquery to edit the name. In that way, lots of similar AJAX requests can share the same method and methods can share views.