PHP REST API Routing

I studied the API and developed the REST API for the project we are working on.

The API only accepts connections from a single source in JSON format, I understand that this is a bit.

If you understand most of the above, however, I don’t understand the third code example and where routing information will be displayed.

An example they provided:

$data = RestUtils::processRequest(); switch($data->getMethod) { case 'get': // retrieve a list of users break; case 'post': $user = new User(); $user->setFirstName($data->getData()->first_name); // just for example, this should be done cleaner // and so on... $user->save(); break; // etc, etc, etc... } 

The part I'm not sure about is how to accept the original ie / get / user / 1 request - how you redirect this to the correct part of the script.

If there was another SO question (I searched quite a bit of time) or any additional educational examples, please point me in the right direction.

Update

I found several PHP classes for routing, but nothing that just does a little and does what it says in tins, everything seems to do routing + 2000 other things on top.

Now I have all the classes that I need for this project, since I want to access them from the ie URI:

/ data / users / data / users / 1 / hash / users / hash / users / 1 / put / users / 1? JSON = {data}

Thus, they should all use the users class, then one of the data, hash or put methods then passes something extra after that to the method as arguments.

If anyone could just explain how this bit works, that would be a huge help!

Thanks:)

+4
source share
2 answers

From the very beginning, it seems that the website you pointed to does not have a router or dispatcher enabled. There are many PHP5 frameworks around which there is a route and / or sending or some kind of description. ( http://en.wikipedia.org/wiki/Comparison_of_Web_application_frameworks#PHP )

A router will be a class that has a list of predefined routes that can be really basic or quite complex, it all depends on what you want to do. A good IMO REST router would look something like this:

 :module/:controller/:params 

Then the router will route the correct action based on the HTTP request (GET, POST, PUT, DELETE, OPTIONS)

 public function getAction($id) { // Load item $id } 
0
source

In your case, will you need a redirection rule that will send a request for something like index.php? user = identifier. You can then process the receipt request.

The best solution I found for php REST architecture (including routing) is:

http://peej.github.com/tonic/

0
source

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


All Articles