Here I wanted to tell you something that I consider very important, which is the part of modularizing both your routes and your controllers, meaning the controller functions or methods, as you want to call them, that is, the one that processes the request itself. So for that I want to show you my previous version of free development since it is a comparison that I have here and look at the lot of get type routes that I have here, perfect, they may have many things to consult since it is for the API but notice that if I grab here:
Route::get('{tutorial:url_clean}', [\App\Http\Controllers\Api\TutorialController::class, 'show']);
Route::get('/coupon/check/{coupon}', [\App\Http\Controllers\Api\CouponController::class, 'active']);
Route::get('update-progress-by-user', [App\Http\Controllers\Api\TutorialController::class, 'update_progress_by_user']);
Route::get('/get-detail/{tutorial:url_clean}', [App\Http\Controllers\Api\TutorialController::class, 'getAllDetail']);
Route::get('/by-slug/{tutorial:url_clean}', [App\Http\Controllers\Api\TutorialController::class, 'by_slug']);
Route::get('/class/free/by-slug/{tutorial:url_clean}', [App\Http\Controllers\Api\TutorialController::class, 'class_free_by_slug']);
// detalle de las clases sin ID vimeo
Route::get('{id}/class/resume', [App\Http\Controllers\Api\TutorialController::class, 'get_class']);
Route::group(['middleware' => 'auth:sanctum'], function () {
Route::post('inscribe/{tutorial:url_clean}', [App\Http\Controllers\Api\TutorialController::class, 'inscribe']);
Route::get('user/my-courses', [App\Http\Controllers\Api\TutorialController::class, 'my_courses']);
});
The tutorial, notice that they are all practically of the get type, all of these that we have here and over here there is another and over here there is another. So what is the reason here? I know that this is a little abstract but try to understand me a little bit about the tutorial entity to manage the parts of the courses. In the courses I have sections and also the classes like in Udemy as it is we have the main course, the Arabic course, from there we have sections, there we have the relationship and then from there we have what the classes are, therefore it is a nesting of that type, I also have with the posts and others but that is not so much a problem, so in the end what I am doing here is obtaining the details of a tutorial, as simple as that, but it depends a little on the context since if the user bought the course then either the tutorial will have more information apart from the fact that it is different, the tutorial is being consulted from what would be the detail, that is to say, as if it were a publication, or it is already consulting everything that is the tutorial itself, that is to say, it has to load the sections and classes, therefore on the one hand I am going to want that return me what the tutorial itself is and on the other hand return me all the alignment that I was telling you about and that is why I was creating this bunch of routes apart from that I also have an interesting detail before that is that it can be consulted in various ways similar to what happens with the post that we can do it through the ID or to do it like this be a friend through the slug or URL clean which is the one I called here the most called URL clean then here you can see a bit of all the dizziness with this what is the problem with this apart from that all these routes are quite ugly and quite incomprehensible since I start to place names quite strange names and with this, the matter becomes much more complicated and with this we lose the modularization that was what I told you at the beginning So here Notice how they simplify they are basically inclusive I could have simplified it into just two routes but here is the main thing:
Route::group(['prefix' => 'tutorial'], function () {
Route::get('', [App\Http\Controllers\Api\TutorialController::class, 'getAll']); //get-detail
Route::get('simple/get-by-slug/{tutorial:url_clean}', [\App\Http\Controllers\Api\TutorialController::class, 'getSimple']);
Route::get('simple/get-by-id/{tutorial}', [\App\Http\Controllers\Api\TutorialController::class, 'getSimple']);
Route::get('full/get-by-slug/{tutorial:url_clean}', [\App\Http\Controllers\Api\TutorialController::class, 'getFull']);
Route::get('full/get-by-id/{tutorial}', [\App\Http\Controllers\Api\TutorialController::class, 'getFull']);
});
A very important thing, which is the first tip I want to give you, is that if you want to bring it by the ID and the slug is basically the same controller, the only thing you have to do is define an additional route just like you are seeing here:
Route::get('full/get-by-slug/{tutorial:url_clean}',
Route::get('full/get-by-id/{tutorial}',
And so here I put it Remember that this is grouped over here here I put tutorial it would be simple tutorial because it is the version as who says without the classes and sections and here get by loot for the loot and get by ID remember that you can always use these wildcards I think they are called to indicate and if you don't put anything here it would be by default for the apk which will be the ID but it is not necessary to put it it would be the normal operation therefore here you have the first tip which is that you learn or remember that we can use these types of parameters to indicate that it filters by a particular field which in this case is the slot and with this we can use exactly the same controller then with that I simplified half of the routes and stopped and said that we could merge this because in the end it was one more parameter which gives it to me full, that is to say with all the sections and classes I could have perfectly passed an additional parameter here but I wanted to handle it that way because for me it was more comfortable this way; As I told you there, simplify such ugly routes with these horrible names that I had here from title here from the root and everything that you can see here in this scheme that is much better. What is the other tip that you take advantage of the parameters instead of defining routes of this type, you can take advantage of reading either something about the authentication if the user is authenticated then you take advantage and show more details and for example the user bought the course then it is simply a conditional or simply a parameter via request:
public function getFull(Tutorial $tutorial) //show
{
$user = auth()->user() ?? auth('sanctum')->user();
if ($user) {
// do somethig
}else{
// do somethig more
}
}
In case it is like this something not protected for example Here I am taking advantage of this does not convince me much but it is the way that I have been able to advance this I also use it for when the user is going to buy the course that is when I use only on the information page of the tutorial this you can see on the Academy website I go to the list I select a course and you will see that I do not load what are the classes and sections directly I load information of the tutorial then I use this resource while when the user has already bought it he will see the cold detail he simply bought it and would use the full one that returns all the information of the tutorial So here instead of creating an additional resource so that look calculate the price and so on I simply pass it through here if here I receive a parameter called request that tells me Look give me the extra price here I simply calculate it and return it to him as well as the exclusive price for classes and it is basically what you can take advantage of and a bit the same here here I get the authenticated user since this resource I also left it as optional, that is to say the authentication is optional since it I use it for both the free view of the detail and for the payment view and this does not require passing a parameter through request nor should you for god's sake because if not it would be something very easy to hack if I don't do it through santum or the session or whatever I'm using here then if the user is authenticated I do a process and if not I do another process of course the controllers are a little more complex that you can also modularize to your liking for example here I create some help methods I think I have it here for what is the request sometimes I do it for example this but that depends a little on you or directly use Facade which I don't recommend much or directly from the model you implement some method or something like that but what I wanted to get to and this with this I already conclude the importance of modularizing your application since on the one hand I don't even know what the hell I went back to this different from this because you get lost apart from the fact that it is much more difficult to follow and above all to maintain not only for what I just told you but when you want to add a new parameter you are going to have to go testing each one of them and modifying each one of the controllers that we have here defined in this mess that we have here instead of having something as clean as what is shown here. So that is the message that I wanted to give you and without further ado, I will see you in another video.
- Andrés Cruz
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter
I agree to receive announcements of interest about this Blog.
!Courses from!
10$
On Udemy
There are 3d 06:56!
!Courses from!
4$
In Academy
View courses!Books from!
1$
See the books