Answer the question
In order to leave comments, you need to log in
Is it possible to use one method for 2 routes?
In general, I used this technique in order not to create another method for categories and copy-paste the code there with minimal additions. But is it right?
Route::get('/shop', 'Front\[email protected]');
Route::get('/cat/{slug}', 'Front\[email protected]');
Answer the question
In order to leave comments, you need to log in
From the point of view of the code, you have two routes, the request is processed in one controller and in one method, of course it will work.
But from the point of view of the code, this is certainly not perfect. Here, as in the classics, "Everything is mixed up in the Oblonskys' house."
- The controller must be different, and at the moment, not because it just has to be, but simply the name of the route and the controller cause dissonance in the first place.
- Secondly, there is such a term as REST API, and in the spirit of Laravel, the show method is more suitable, documentation
. I also don’t like your namespace Front, and there is a feeling that it is no longer needed than it is needed.
And probably category is always better than cat or c.
You can also read about resources
And most importantly, about shortening the code:
- "To not create another method" (anywhere)
- To not create another class
- To not create another table in the database
- To not create another ...
More likely to lead to bad code and confusion. Little code does not equal good code. In commercial development, there is often a very large subject area, you need to work with it, maintain it, and the like - this generates more code, because:
- It is better (sometimes) to create another method
- It is better (sometimes) to put it in a separate class
- It is better (sometimes) to normalize and put into a separate
Advice table - Read the Laravel documentation✌️
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question