0

I have some classes and methods which are containing some php codes. Now I want to use those PHPcodes both for ajax and http requests. How?

Should I write all my PHP codes twice? onetime for ajax requests and one time for http request?

Here is my currect structure:

/********************************************** Search.php ****/

... // some html codes

<div id="content">

<?php

if(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && 
   strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
{ 
  $type = true; // true means ajax request
} else {
  $type = false; // false means http request
}

  $obj = new classname;
  $results = obj->func($type);
  echo $results;

?>

</div>

... // some html codes



/********************************************** Classname.php ****/

class classname {

  public function func($type){
    $arr = ('key1'=>'some', 'kay2'=>'data');

    if ($type){
      echo json_encode($arr);
    } else {
      return $arr;
    }

  }

}

Now I want to know, this is standard? Actually I want to use it also for a request which came from a mobile app (something like an API). I think the above code has two problem:

  1. there will be a lot of IF-statement just for detecting type of requests (in the every methods)
  2. there is just one file (class.php) for both ajax and http requests. And when I send a ajax request by for example mobile, it will process some codes which doesn't need to them at all

Well, is there any point that I need to know?

stack
  • 10,280
  • 19
  • 65
  • 117
  • What do you mean with: "Now I want to use those PHPcodes both for ajax and http requests"? – Bob van Luijt Dec 12 '15 at 22:27
  • @BobvanLuijt I want to know, should I write a php code twice for using it both for ajax and http requests? or can I write it (php code) once and use it for both requests? In other word, my approach *(which is mentioned in the question)* is a normal/standard way? – stack Dec 12 '15 at 22:28
  • if you want more info on my answer, please let me know. – Bob van Luijt Dec 12 '15 at 22:34

3 Answers3

1

This is more about the design of your software then simplifying it. Often you would have a structure with classes that you use and separate PHP files for types of responses.

For example:
You could have the documents: classes.php, index.php and ajax.php.

The classes would contain generic classes that can both be shown as JSON or as HTML.

PS:
Simplified could mean:

/********************************************** Search.php ****/

... // some html codes

<div id="content">

<?php

  $obj = new classname;
  $results = obj->func($_SERVER["HTTP_X_REQUESTED_WITH"]);
  echo $results;

?>

</div>

... // some html codes



/********************************************** Classname.php ****/

class classname {

  public function func($type){
    $arr = ('key1'=>'some', 'kay2'=>'data');

    if(!empty($type["HTTP_X_REQUESTED_WITH"]) && 
   strtolower($type["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest"){
      echo json_encode($arr);
    } else {
      return $arr;
    }

  }

}
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • Thanks ....! +1 However your approach is not good, because it needs to a lot of IF-statements *(for each methods)* to detect type of request. – stack Dec 12 '15 at 22:42
1

You can write a single code, and you should use only partials to load AJAX data. The main reason we are using AJAX is to minimize data transfer. You should not load a full page HTML in an AJAX call, although it is perfectly valid.

I would suggest from my experience is, have a single file:

<?php
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        /* special ajax here */
        die(json_encode($data));
    } else { ?>

Regular Flow of Content

<?php } ?>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    Sounds great ..., But actually your example isn't clear completely for me.., Can you please add some detail to your example? specially please declare two separated files. *(`search.php`, `classname.php`. which classname uses an autoloader for calling)*. Anyway please apply your approach exactly on my codes. tnx +1 – stack Dec 12 '15 at 22:42
  • 1
    @stack Thanks. Your code is too long. I just thought of giving my bit of information if I would do it this way. Lemme edit my post according to your code. – Praveen Kumar Purushothaman Dec 12 '15 at 22:43
  • @stack Yeah man... I am not good with OOPHP... It is not executing in my PC. `:(` – Praveen Kumar Purushothaman Dec 12 '15 at 23:02
  • 1
    @stack Just noticed. I am trying to do something like what [trincot](http://stackoverflow.com/users/5459839/trincot) has done. That's the same thing. – Praveen Kumar Purushothaman Dec 12 '15 at 23:03
1

I would first do all the business logic, calling on your classes, without any dependency on whether you are in an ajax call or not. When all is ready for output, make the switch:

Search.php:

<?php
    // first do your business logic without view dependency.
    $obj = new classname;
    $results = obj->func(); // no type is passed.

    if(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && 
       strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
    { 
        // ajax request
        echo json_encode($results);
        exit();
    } 
    // http request
?>
... // some html codes
<div id="content">
    <?=$results?>
</div>
... // some html codes

Classname.php:

<?php
class classname {
  public function func(){
    $arr = ('key1'=>'some', 'kay2'=>'data');
    return $arr;
  }
}
?>
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Just one thing, How can I use this as a API *(for for example a mobile-app)* – stack Dec 12 '15 at 23:01
  • That is a large subject. I would refer you to [this question](http://stackoverflow.com/questions/4973156/how-to-write-a-rest-api). – trincot Dec 12 '15 at 23:07