I
I
Ivan Artamonov2017-11-17 00:22:30
API
Ivan Artamonov, 2017-11-17 00:22:30

Should Laravel map model namespaces to route namespaces when developing APIs?

When developing an application, I put models in App/Models. In a large project, there are usually several dozen models. To make it more convenient to work with them, and also so that models with common class names do not overlap, I decompose the models into namespaces.
For example: I have articles App/Models/Articles/Articleand products App/Models/Catalogue/Good. Both articles and products can be categorized into many-to-many categories: App/Models/Articles/Categoryand App/Models/Catalogue/Category. While I'm working with classes: models, controllers, view composers, etc. - everything is fine, I like it. Related models are grouped, models with common class names do not interfere with each other.
But as soon as it comes to routing, I start having problems with naming. Now I started to master the development of Web-API and the routes are obtained like/api/v1/articles/categories/1/articles, because I'm trying to reflect the namespaces of resource models in the route. I do not like the fact that there are two articles in the path, and the route also /api/v1/articlesgives 404, although when taken out of context it hints at a collection of articles.
Structurally, my routing and models are connected by namespace: it seems to me that this is logical and transparent for the API developer, which is good. If I want to bring the route with articles in the category to the form /api/v1/articles-categories/1/articles, then I’ll get rid of the confusion in the routes, but then it’s logical to name the models as App/Models/ArticleCategorywell, but then I will lose the benefits of the namespace, since related models will be ungrouped.
Or is it not worth bothering with these correspondences and still keeping the models in convenient namespaces, and not reflecting the model namespaces in the routes for the API in any way? How do you deal with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YaRobot, 2017-11-17
@iv4art

I don't know to be honest.
But I don't think it's worth linking.
And why do you have categories in the address for the material?
You're making an API. Start up in the filter or heading.
Let 's say GET /api/v1/articles?fq['category']=1(well, or in the title)
We get all the articles from the category. If without a filter, then we get all the articles in general.
You can of course rework the structure.
Routes do as convenient, and models and others like them, it is logical to arrange.
I don't care what route we have in the project. If I need to change something, I already know where to look.
For example, I need to change the article model (with translation to Lara).
Domain/Model/Article/ArticleModel.php(this is if you do the domain, well, or leave the App )
UPD:
It is customary for us to give a name at the end of each file, what it does.
App/Model/Article/ArticleCategoryModel.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question