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