views:

117

answers:

5

Simple question...I am using an MVC structure to build my site (its fairly large, so it helps with maintenance. I am now starting to integrate a few fun AJAX functions to spruce things up for Javascript/AJAX friendly users.

Since I am so anal about organization, I wanna store my ajax PHP handlers in a way that is logical, secure, and (hopefully) separate.

How do you organize yours? Is there a best practice?

A: 

Mine are stored in a specified folder _ajaxHandlers, this folder contains files like updatePageList.php, deletePage.php, etc).

I'm not sure this is the best way to do it since I'm also relativly new to the ajax concept :) Any suggestions for better storing / handling files are also appriciated by me.

The underscore in the dirname is because I like alphabetic listed folders sorted by their purpose. In my structure underscored foldernames are considers systemdirs.

Ben Fransen
is this folder in a public folder? (im assuming it HAS to be, but i want to make sure)
johnnietheblack
Yes it is, at least for my System classes it is. By default directorylisting is disabled.
Ben Fransen
Does it matter if it's public or if the directory listing is disabled? Inspection of the JavaScript is at least as easy as trying to look at the server.
Nosredna
The only thing a person gains is a peek into your file structure. In my system all files need eachother and cannot even be parsed unless the FrontController passes the desired page to the View otherwise a user will only receive a bunch of fatal errors.
Ben Fransen
+1  A: 

For me the best solution is to have an index.php front controller which handles both page generation and ajax requests, and than have the same files that create pages do ajax things for you, even templating and module reloading.

That simplifies everything and you have an unified structure.

dfilkovi
A: 

I do the same as well, previously I used to store ajax handlers with ajax- prefix like

ajax-updatestatus.php
ajax-managemessages.php

but recently I moved out to using a new folder

atif089
A: 

if you use jquery or another framwork you can distinguish the requests natively with the $_SERVER['HTTP-X-REQUEST-BY'] header within your existent methods. This way you have ajax and normal request on the same place, so there is no structual change.

hubbl
A: 

There's no best practices, and it's one of the places in PHP projects where things quickly go off the rails with lots of single files outside teh controller structure. Check your framework docs to see if they offer any advice in this area, as its an increasingly common way of building applications.

The best way I've seen this handled in the wild is to treat an ajax request like any other request in MVC. Create a controller action for your request. Depending on the size of the project/personal preference, you can create separate logical controllers for ajax requests, or group your ajax actions with existing controllers, (optionally giving the action name an "ajax" prefix or suffix)

class IndexController extends BaseMvcController{
    public function indexAction(){}

    public function ajaxuserinfoAction(){}
}

//or

class AjaxController extends BaseMvcController{
    public function userinfoAction(){}
}

That still leaves how to handle the view portion of your request. I've become a big fan of creating stdClass objects and then using echo json_encode($object); with a header('Content-Type: application/json');.

If your MVC framework supports suppressing the layout of your site, you can build your response output in your views. If not, adding a simple helper function somewhere like this will work just as well

protected function outputJsonForAjax($object)
{
    header('Content-Type: application/json');
    echo json_encode($object); 
    exit;
}
Alan Storm