Answer the question
In order to leave comments, you need to log in
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(){/** --- */});
Route::get('users/{user}/{method}');
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question