Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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;
}

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.

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

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.

How about every ajax call has an id (which you should document in some way if you have a lot of them) and all the handlers are in one file with a huge ass switch case loop.Or if it's gonna be a big file maybe organise them in a few files - id 3001, 3002, 3003 form submits go to formHandler.php id 4001 400n go to layout system and so on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top