REST URL mapping and physical implementation in PHP

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

Example: Book resource:

1- GET www.domain.com/api/book/- receives all books (possible parameters in the body) 2- GET www.domain.com/api/book/1234 - receives details of the book instance with identifier = 1234 (without parameters in Tele) 3- GET GET www.domain.com/api/book/1234/author - gets the author of the book with ID = 1234

I'm curious about server-side implementation of these services. What PHP files will the corresponding code be stored in? Is there any kind of server configuration?

I assume that I will have a server folder structure like this: 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 is it somehow being processed by a script in api / book /?

Thanks and hi!

Where should I code the implementation

+4
source share
1 answer

Usually

  • You must redirect all your requests to a single file using htaccess + mod_rewite.
  • Then you should parse the URI with PHP.
  • A controller class to decide which action to perform based on the parameters analyzed.

For instance:

www.domain.com/api/book/1234 

You parse it:

 action: book id: 1234 

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

But...

I recommend using some simple framework for REST applications. For example, Slim Framework .

+2
source

Source: https://habr.com/ru/post/1502410/


All Articles