S
S
Stanislav Pochepko2015-11-18 12:54:14
RESTful API
Stanislav Pochepko, 2015-11-18 12:54:14

Should REST routes be built dynamically?

I understand the semantics of building routes in REST. But I do not understand how to organize them correctly. (I use laravel)
For example, I have a user model that has some methods (posts, comments, role)
And accordingly routes for them

Route::get('users', function(){/** --- */});
Route::get('users/{user}/posts', function(){/** --- */});
Route::get('users/{user}/comments', function(){/** --- */});

The question is this. Should these routes be built automatically? In the likeness of one route and depending on the name of the method, then give it away. If so, what to do with a request with a large number of segments? Or is it not correct? /users/1/posts/5/comments/34/likes
Route::get('users/{user}/{method}');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Flaker, 2015-11-18
@DJZT

You have 3 entities: User, Post, Comment.
REST implies that each of them can be accessed separately from the others.
That is, you will have routes like:
/posts
/posts/{id}
/users
...
If you are making a Single Page Application, then it would be logical to load each of them as needed (Yes, in several separate requests)
But in order not to make 3 requests, you can make friends all of them together, specifying what to make with the help of get parameters. For example:
You can create your own API and REST is not a panacea. Definitely worth knowing his ideas, but blindly following them is not worth it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question