DEV Community

codeToBug
codeToBug

Posted on • Originally published at codetobug.dev

Injecting Dependency Like You've Never Seen Before: Unprecedented Laravel Missteps

Ah, the majestic art of Dependency Injection in Laravel – when executed flawlessly, it's like watching a ballet dancer pirouette gracefully. However, when it goes wrong, it's a hip-hop dancer in a ballet class, utterly hilarious.

Isn't it simply the most elegant thing in the world, that thing we call Dependency Injection? When you inject the class into your Laravel controller, it's supposed to glide smoothly, like a well-oiled machine. Alas! Some of us seem to be making steam engines instead.

Picture this: You're in the code wilderness, on a quest to instantiate that tricky Repository class. You've heard tales of Laravel's magical service container, its mystical powers of automatic resolution. But no, you say, "I’m not a magician! I am but a simple artisan."

public function __construct() 
{
    $this->repository = new Repository();
}
Enter fullscreen mode Exit fullscreen mode

Oh, a standing ovation for your courage, good sir/madam! Who needs Laravel's automatic resolution when you can manually do it yourself? It's like driving a Tesla but insisting on turning the ignition key that doesn't exist.

Now let's up the ante. Ever stumbled upon the folks who like to treat Laravel like it's a one-man show, pushing all dependencies into a single method?

public function overworkedFunction(Foo $foo, Bar $bar, Baz $baz, Quux $quux, Quuz $quuz, Corge $corge, Grault $grault, Garply $garply)
{
    // Sincerely hoping the function does not collapse under pressure!
}
Enter fullscreen mode Exit fullscreen mode

Bravo! Look at this function, trying to juggle like a circus performer. It's not a function anymore, it's a dependency party and everybody's invited!

I implore you, friends. Laravel, with its service container, is more like a magical butler. Its sole purpose is to elegantly manage our dependencies. Don't reduce it to a simple valet, carrying around your piles of objects. Let it shine in its true glory.

public function __construct(Repository $repository) 
{
    $this->repository = $repository;
}
Enter fullscreen mode Exit fullscreen mode

Behold the glory of a well-implemented constructor injection, the kind that doesn't make Laravel shed a tear behind the scenes.

And that, ladies and gentlemen, is our comedy of errors, a true 'how-not-to' guide on Dependency Injection in Laravel. While these examples might make you laugh (or cry, if you're a true Laravel enthusiast), remember the golden rule: Keep It Simple, Silly. That's what Laravel is all about. Now, let's put our hands together for the unsung heroes of Laravel who keep the dependencies flowing smoothly, and the code bugs at bay.

Top comments (1)

Collapse
 
xwero profile image
david duymelinck

I love the whimsical tone of the article.

But I think the meat of the article is not balanced.
After reading the article it seems you are saying put all dependencies in the constructor.

My view is only add dependencies in the constructor that are shared by multiple (most) methods.
Dependencies that are shared by a few/one method(s) belong in the method parameters.