Welcome Guest, Not a member yet? Register   Sign In
AJAX with Application Folder Above the Webroot
#4

[eluser]boltsabre[/eluser]
I've only just started to ajax my first CI application, not sure how others handle it, or if there is a better way, but this is how I'm doing it.

- I already have a custom built 404 helper function.
- In my controllers folder I have a folder called "ajax", and in there I have each "ajax" request stored in it's own file.
- Sorry, I don't have this code on hand, you'll have to find it yourself, as I'm at work, but there is some code that determines if it is a ajax request. I've put that into my "config/constants" file, which returns "IS_AJAX" when it's an ajax request (just google "codeigniter, how to tell if it is an ajax request", you'll find something that works, I think there may even be a built in function in the latest CI release?)

- And in my "controller/ajax/some_ajax_controller" controller I just check on the first line of code if it is a legitimate ajax request, and if not show them a 404 page! (with 404 http headers obviously! All handled direct in my 404 helper)
Code:
class Some_ajax_controller extends MY_Controller(){
   //constructor goes here, if you want you can put the below 404 code,
  // which would make sense if you have more than one function!
  
   function some_ajax_function(){
      if(!IS_AJAX){
         page_not_found() //which is my custom 404 helper, which is already autoloaded
      }
   }

I can make ajax calls to this file, it'll work fine, just echo back whatever you need to, else if google bot, or a user tries to navigate to the file via the browser it'll throw my custom 404 page, essentially locking out all unauthorised access to the file.

I like this system because I can group "related" ajax functionality into a class and call it by function names, or if it's an isolated ajax call it just get's its own controller and I call the function "index" like normal.

I'm sure there are better ways of handling this, but it's working fine for me. And of course if you have a HUGE amount of ajax and you find that having one "ajax" folder is restrictive (ie, application/controllers/ajax/), you can scale it up and create ajax1, ajax2, ajax3, etc, or even group them into CRUD:

- application/controllers/ajax_create/
- application/controllers/ajax_read/
- application/controllers/ajax_update/
- application/controllers/ajax_delete/

It's all pretty flexible on how you want to set your folder/controller/ structure, whatever is going to work best for your situation!

If anyone more experienced has any input into this methodology I'd be grateful for your input, I've only just started my ajax-ifcation process, so it be very easy for me to update (now, not in 3 months time) to a better methodology.

Hope it helps somewhat Marcello


Messages In This Thread
AJAX with Application Folder Above the Webroot - by El Forum - 01-23-2013, 04:04 PM
AJAX with Application Folder Above the Webroot - by El Forum - 01-23-2013, 07:06 PM
AJAX with Application Folder Above the Webroot - by El Forum - 01-23-2013, 09:07 PM
AJAX with Application Folder Above the Webroot - by El Forum - 01-24-2013, 03:24 AM
AJAX with Application Folder Above the Webroot - by El Forum - 01-24-2013, 12:08 PM
AJAX with Application Folder Above the Webroot - by El Forum - 01-25-2013, 01:06 AM
AJAX with Application Folder Above the Webroot - by El Forum - 01-25-2013, 01:19 AM



Theme © iAndrew 2016 - Forum software by © MyBB