tags:

views:

33

answers:

4

I have a website where people can place bids on products. So the first thing I have done is created a normal HTML-PHP version of the website which works great.

I have created some basic PHP functions that look at the url -> route you to the controller -> perform the action -> take the POST object etc.

Now I have implemented an AJAX version for this action but I want to know if I am doing the correct thing.

I made a new folder called ajax, and there I made a new controller for the ajax request. Now when somebody clicks on 'bid' then the request is hijacked by Javascript and sent to the AJAX controller. Now in the AJAX controller I strip down the link and then load the controller and model, and the work is done by the class.

Now the response the class has to send back is different for the AJAX version and for the HTML version. Now in the code of the class I have something like this

if(ajax version){
    give ajax response
}else{
    give html response
}

Now there are more things in the classes that are different.

Now my question is is it ok to use the same classes and config files for the ajax and non ajax function, or do they have to be seperate.

Or is there a more elegant manner of tackling this problem, maybe using interfaces.

A: 

An easy way to do this is set a GET or POST parameter in the request when using AJAX, such as ajax=1 then check for this in the PHP:

if (isset($_GET['ajax'])){
    give ajax response
} else {
    give html response
}
Delan Azabani
A: 

AJAX requests have a header HTTP_X_REQUESTED_WITH with a value XMLHttpRequest

For example:

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
  // Ajax
} else {
  // Normal request
}
Harmen
No, they don't. Some JS frameworks add that header when using XMLHttpRequest, but it isn't intrinsic to Ajax or the XMLHttpRequest object. (And you can do Ajax without XHR (JSON-P and dynamic script elements for example)).
David Dorward
@David Dorward: You're right there, but it works for frameworks like jQuery and you can always set this header yourself if the framework doesn't set it.
Harmen
A: 

As a rule of thumb, it is usually better to keep the same Model and Controller and, based on data in the request (such as a value in the query string, or a custom header such as X-Requested-By), switch Views (probably between an HTML Template View and a JSON View).

David Dorward
A: 

If you follow the MVC pattern correctly, which it looks like from your description you are, then you should have a different action per request.

The flow should be

  • a different URL and action to receive the AJAX request
  • perform the business logic in your model
  • the action should then format the response in the style required (AJAX)

By having a switch (the if statement) in a shared controller, you run the risk of adding logic into the controller, which reduces the re-usability of your code.

So, put simply. Have a different PHP page for the different actions.

Codemwnci