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?

  • In general, how much functionality do the different actions share? – Izkata Dec 1 '13 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. – Fa773N M0nK Dec 1 '13 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 '13 at 19:31
  • @zeel: that's the second architecture in my question, right? – Fa773N M0nK Dec 1 '13 at 19:37
  • @Fa773NM0nK Oh I guess so, the illustration makes it look like AJAX.php dose all the work. – zeel Dec 1 '13 at 21:11

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

  • 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? – Fa773N M0nK Dec 6 '13 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. – roo2 Dec 8 '13 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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