4

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

4

1 回答 1

2

一般来说

  1. 您应该使用 htaccess+mod_rewite 将所有请求重定向到一个文件。
  2. 然后你应该用 PHP 解析 URI。
  3. 一个控制器类,用于根据解析的参数决定执行什么操作。

例如:

www.domain.com/api/book/1234

您将其解析为:

action: book
id:     1234

因此,您showBook()使用参数1234-运行操作showBook(1234),这将完成所有其余工作。

但...

我建议为 REST 应用程序使用一些简单的框架。例如Slim 框架

于 2013-09-16T13:00:51.480 回答