2

I am working on a website with PHP as backend. There are things from the front-end which I'd like to accomplish using AJAX calls.

Which of these methods should I use to design them and why?

  • Call to different PHP files for different uses enter image description here

  • Call to the same PHP file, with instructions on what to do enter image description here

  • A RESTful API

  • Any other better way?

8
  • In general, how much functionality do the different actions share?
    – Izkata
    Dec 1, 2013 at 18:41
  • @Izkata: Apart from the very common ones (Session Validation, Database Connection, etc.), let's say that the different actions do not share much in common. Dec 1, 2013 at 18:49
  • You could also have AJAX.php accept all the requests, and require('like.php') or comment.php to handle the specifics. Thus AJAX.php has one purpose: handle request, but you only need to handle requests in the one file.
    – zeel
    Dec 1, 2013 at 19:31
  • @zeel: that's the second architecture in my question, right? Dec 1, 2013 at 19:37
  • @Fa773NM0nK Oh I guess so, the illustration makes it look like AJAX.php dose all the work.
    – zeel
    Dec 1, 2013 at 21:11

1 Answer 1

2

Have a look at building a REST API. you can have clean urls that represent resources and these can be mapped to php servers in variouse different ways.

POST  /like
    { 
      userId: 'foo',
      itemId: 'xxxxxxxxxxxxx'
    }

POST  /comment
    { 
      commentText: 'bar',
      itemId: 'xxxxxxxxxxxxx'
    }

The Slim framework provides a quick and easy way to set up a REST api in php

2
  • Thanks a lot. I finally setup Slim on my system. (Had real trouble with apache's rewrite module and the .htaccess file). However, this solution is platform-configuration specific. If I want to move to a different web-server, I need to find a way to redirect all calls to index.php (all arguments intact). Is REST the only way to go? Dec 6, 2013 at 1:57
  • good job setting up slim, in php the .htaccess file is what you'll need to use. REST is language agnostic based on HTTP so moving to another backend tech stack should be doable. Dec 8, 2013 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.