RESTful PHP Framework that Support Resources, Methods, and Formats

It seems that there are many PHP frameworks that require RESTful design patterns.

I am looking for a framework that does a very good job, providing a reliable solution to these three elements (or their equivalence).

1. Resource Requests

The first thing to do is to process the URL to resolve the resources.

/path/to/resource = controller.action() 

2. Request Methods

Secondly, you need to handle various types of query methods.

 GET /path/to/resource = controller.get() POST /path/to/resource = controller.post() 

Perhaps with a rollback from the universal handler if the request method does not exist.

 GET /path/to/resource = controller.action() PUT /path/to/resource = controller.action() POST /path/to/resource = controller.action() 

3. Answer formats

Finally, I saw people attaching formats to the end of a URL to help the framework know what type of response is expected.

 /path/to/resource.html /path/to/resource.json 

Other ways to pass the response file in the header or as a URI parameter ( ?format=json ).

These are the three elements that need to be covered. However, they do not need to be processed in the same way as I just showed - these are just examples.

+4
source share
2 answers

I studied a similar structure, but in the PHP world, it seems that not much is happening. Here 's a related question about PHP REST frameworks.

Recess looks interesting, and I found the new ZEST Framework REST controllers and routers very useful. I also started to take a simpler approach on top of the Zend components. Basically, you register a bunch of rendering views (HTML, JSON and a simple XML form are supported out of the box, by parsing the acceptance header or rewriting it using the format =? Parameter), and a parser tag (Web Foms and JSON are included) and based on this interface :

 interface Feathry_Rest_Resource { public function index($params = null); public function get($id, $params = null); public function post($data, $params = null); public function put($data, $id = null, $params = null); public function delete($id, $params = null); } 

If each method returns a simple array or object (using the toArray method), you can create a RESTful resource. The advantage is that your resources are completely separate from any view. They do not even need to know that they are used over HTTP if they follow an interface. This is still very alpha, and there wasn’t much interest in it, but it works, so maybe you want to try it.

+1
source

Recess has an interesting approach, using annotations to determine routing.

If you are using PHP 5.3, then Tonic is a very lightweight structure with an emphasis on REST. It also uses a similar style for Recess, using docblocks for routing.

Personally, I am using the Zend Framework with a custom version of Zend_Rest_Route .

Each of them also has some way to provide some processing of response formats, allowing you to coordinate content based not only on the method you describe, but also on the Accept header field to determine in which formats the client will understand. My personal approach in ZF was to work with a data structure (mainly ArrayObject ) with custom serializers for JSON, XML, YAML, etc. And have a controller to determine the best output format.

0
source

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


All Articles