Inject services, unclutter your controllers Since we just tackled everything route related, the next logical step is to get into Controllers. There are those that would have argued that we should have covered migrations, Models and then Controllers/Routes/Views, and there’s no disagreeing with that statement. Maybe I should write two Laravel article series concurrently. In the routing concept, without getting too far into the weeds, the Controller comes next. https://www.dinocajic.com/laravel-p55-soft-deleted-models-in-routes/ It’s not that we haven’t covered controllers yet, we just haven’t dug into some of the more infrequently/complicated functionality yet. Most of the concepts that we have already covered in

Route to trashed records with confidence Running through a tutorial series like this isn’t as straightforward as it seems. You want to cover topics in a specific order and yet each time you get to a new topic, you realize that you need to cover something else in order to do it. For example, after going through some of the easier topics in Laravel, I figured we could tackle some of the topics on their own, like Routes, Models, Controllers, etc. I’m in somewhat of dilemma right now. The last topic of the route series that I wanted to cover

Organize routes, secure them, and keep controllers lean The final topic that I wanted to touch upon in the web.php file are routing groups. We’re not fully finished talking about routes considering that we really haven’t even touched on the api.php file, which is used frequently as well. https://www.dinocajic.com/laravel-p53-rate-limiting/ We looked at route groups in an earlier article. If you need a refresher, you may check it out here. https://medium.com/geekculture/laravel-p23-route-groups-8d4ce3e8e083 What I really wanted to focus on here is adding middleware to a group and grouping a specific controller. Applying Middleware to a Group To apply middleware to a specific group, you simply use

Throttle traffic, keep your API safe For the sharp-eyed readers, you might have noticed a method called configureRateLimiting in the RouteServiceProvider in our previous article. protected function configureRateLimiting() { RateLimiter::for(‘api’, function (Request $request ) { return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); }); } What this does is limits the user’s requests per minute. The default is set to 60 requests per minute. If you needed to modify the number of requests, this is where you would typically do it (i.e. change it from 60 to 100). https://www.dinocajic.com/laravel-p52-explicit-route-model-binding/ As you can see in the code, the rate limiter’s are defined in the RateLimiter facade’s for method. It accepts a rate limiter and a closure that returns

Take full control of model resolution in your routes When would you ever need to explicitly bind a model vs utilize Laravel’s implicit binding? A frequently asked question that I frequently find doesn’t have an appropriate, or simple enough, response. Because of that, I don’t see it used as frequently as it should be. https://www.dinocajic.com/laravel-p51-route-model-implicit-binding/ In the previous example, we used the following implicit route-model binding technique. use App\Models\User; Route::get(‘/implicit-binding/{user}’, function (User $user) { return $user; }); What if we wanted to only return the user that has been verified. What I normally see is the following code: use App\Models\User;

Let Laravel fetch your models automatically Now that we’re familiar with dependency injection, what else can we do with the concept? Can we “inject” our models, like the User model, into our route and then utilize model properties without instantiating the User model? How would we even do that? Don’t we need to first “find” the specific user before we can use the properties associated with that user? Believe it or not, Laravel has a way to get a user’s details without explicitly doing something like: User::find(24);. https://www.dinocajic.com/laravel-p50-routing-and-dependency-injection/ Implicit Binding We should understand that we do need to provide our model with an id at the very

Combine smart routing with seamless DI We’ve gone through a few dependency injection topics in the last couple of articles, but now we want to start getting more specific. We have finally entered a more advanced portion of Laravel and will start focusing on specific topics within specific areas of Laravel. The next few articles will deal with routing. In this article, we’ll focus on dependency injections in routes, which is really straightforward. https://www.dinocajic.com/laravel-p49-dependency-injection/ In your route you may type-hint any dependency into your callback signature. If you’re using controllers, the type hint will occur at the method level. use

Implement effective DI patterns for cleaner, testable Laravel code I hope that the last article made sense. Dependency Injection really is a simple concept. I feel like you should understand it in its entirety before you state that you know “Dependency Injection in Laravel.” Before we get into Dependency Injection again, you should understand Laravel’s service container. What is a service container in Laravel? It’s simply a tool that manages dependencies and it is this tool that actually performs the dependency injection. https://medium.com/geekculture/laravel-p48-dependency-injection-concept-high-level-php-concept-482e9f13d97b In the previous article, we looked at an extremely simple dependency injection example. <?php class Driver { private Car

Elevate your Laravel code with cleaner, modular dependencies You’re going to hear the term “dependency injection” many times throughout your Laravel career, so you might as well get acquainted with it. Before we get into Laravel, let’s look at Dependency Injection as a concept in PHP (or really any programming language). https://medium.com/geekculture/laravel-p47-final-touchups-cmp-5b2764c43565 We should know that we can pass objects as arguments to methods. We can also type hint with the inherited class, which is an overview of polymorphism in PHP. We can pass the object as an argument to the constructor. The constructor will then set the property inside

Give your Laravel project the perfect finishing touches It’s time to wrap this project up and move on to more advanced functionality in Laravel. Before we do, we need to add a couple of additional features to our Car Management Project, like deleting photos. This was a fun little project and in my opinion a little better than a traditional to-do list. https://medium.com/geekculture/laravel-p46-controller-destroy-cmp-6dab2b3da6aa Displaying All Images on the Show Page There is an issue with our show page. I’m sure you’ve caught that already. When we were just adding URL’s to other sites, it was easy to display the image.