How is it more rational to process ajax requests for php, if we consider that there are so many different requests for every taste? For each request, have a php file handler on the server? Or in requests to transfer the type of request and then in one bold php-dispatcher to smoke this type? How it's done? Surely many have solved this issue. Surely there are "best practices" and / or design patterns. Comrades php-shniki, share your experience. PHP-framework not to offer. I want to pick it up with pens.

thank

  • I, for example, use one PHP File request handler, and pass all AJAX requests to him. It also has a lot of different code, which is divided into switch-> case for POST and GET requests. - AseN

2 answers 2

In my opinion there are not many options here. Well, create a class that will handle requests. One way or another, but you take a parameter (GET toli, POST toli) and, depending on the key, perform one or another action. In addition, it is not always necessary to create the same type of answers on request.

    You have to have a controller that prepares the data, and then the corresponding view is simply superimposed depending on the format in which the data is needed.

    Pseudo code example:

    $data = <получаем данные для шаблона> if (isAjax){ $response = getAjax($data) }else { $response =getHtml($data); } echo $response;//отдаем браузеру или как оно у вас организовано