Aleks
Aleks

Reputation: 5854

REST URLs mapping and physical implementation in PHP

I am starting my first REST-based application and have a probably trivial question.

Example: a resource "Book":

1- GET www.domain.com/api/book/ - gets all the Books (possible parameters in body) 2- GET www.domain.com/api/book/1234 - gets the detailes of the Book instance with the ID=1234 (no params in body) 3- GET GET www.domain.com/api/book/1234/author - gets the Author of the book with the ID=1234

I am wondering about the physical server side implementation of these services. In which PHP files will the corresponding code be stored? Is there some server configuration to be done?

I guess I will have a server folder structure similar to this one: api/book/ api/author/

...and some php files inside: api/book/file.php api/author/file.php

Should I also have a physical folder api/book/1234 or it is somehow asumed to be handled by the script in api/book/?

Thankys and regards!

Where should I code the implementation of the

Upvotes: 4

Views: 2462

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

In general

  1. You should redirect all of your requests to one file with htaccess+mod_rewite.
  2. Then you should parse URI with PHP.
  3. A Controller class to decide what action to execute basing on parsed parameters.

For example:

www.domain.com/api/book/1234

You parse it to:

action: book
id:     1234

So you run showBook() action with parameter 1234 - showBook(1234), which will do all the rest of work.

But...

I recommend to use some simple framework for REST applications. For example Slim Framework.

Upvotes: 2

Related Questions