[eluser]Randy Casburn[/eluser]
@CARP -- I use AJAX extensively with CI. As you suspect, the application flow is different than using PHP to build the view for you. Since I use JSON to provide the output of the CRUD functions, I don't use views at all when using AJAX. In fact, I use a completely different set of Models for my AJAX responses than I do for classic CI responses. I actually thread an encrypted key with my AJAX requests for security and validate that key in my AJAX models. If you don't have the key, you can't invoke my AJAX methods. Chances of getting the correct key are greatly diminished unless you requested the correct page, etc. etc. As you know, AJAX security is very important and that is what makes CI an ideal platform for AJAX!
Normal flow -> TEST if AJAX -> IF NOT AJAX NORMAL CI
Code:
[REQUEST] -> [Controller] \ [no]/[Model] -> [View] -> [Output] ->
\ /
? HttpRequest ?
\
[yes]\
[ajax/Model] -> [Output] ->
Normal flow -> TEST if AJAX -> IF AJAX Implement AJAX MODEL & echo json_encode()'d string directly to the browser.
That's it. This is efficient, and has worked flawlessly for me for a very long time.
My file structure is:
Code:
[root]
-assets
-- scripts (js goes here)
-- images
-- css
-system
-- application
---models
----ajax
crud.php (maybe your ajax class here)
The beauty of this is that you can still move the system directory outside the document root for security purposes (this is a very good practice). Your AJAX will still work because CI is managing every aspect of finding and executing the correct model for you. The only files that must be in the root are those in the assets folder.
I hope you find this helpful.
Randy