Mateus Guimarães Profile picture
Oct 31, 2022 8 tweets 4 min read Read on X
Let’s see how the service container works!
The container’s job is to give you objects. When you request an object, it might do that in 2 ways (in Laravel, specifically): either by checking if what you requested is bound already, or by instantiating it.
Okay, cool, but what is a binding?
Imagine the container as a central place that has all the dependencies of your app in an array.
The key is a string: it could be any you want, or it can be a class FQN (which is a string).
The value could be a class FQN, a closure, or null.
So, what are the advantages of using a service container?
First of all, the Laravel framework and packages use it internally — in user land, that means you can swap implementations used by the framework on your app, for example.
It allows us to “Invert control” (it is sometimes called an Inversion of Control Container). What does that mean?
First, let’s see what coupling and dependency injection is.
The code below is coupled to TwilioService and hard to unit test and change.
Dependency injection is injecting the dependencies through arguments, instead of instantiating them. See how that achieves inversion of control.
That has several benefits — you’re not tied to a specific implementation anymore, making changes is easier, testing is easier, etc.
In this situation, we could use the container to give us the class we want.
This is what Laravel does internally to instantiate a controller (that’s why you can type hint a Request class, a service, etc, for example) and many (most) other things.
Laravel’s container offers automatic resolution — that means it will try to instantiate a class even if it is NOT bound. Not all containers do this, and it has limitations (see the previous example type hinting an interface).
If you're interested in learning more, I have a couple videos on it — here's the last one:

And, as always, there's really good documentation around it: laravel.com/docs/9.x/conta…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mateus Guimarães

Mateus Guimarães Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mateusjatenee

Jul 25, 2022
Scaling Laravel to 100M+ jobs and 30,000 reqs/smin(pre-Octane)
If you prefer to read this as a blog post: subscribe.mateusguimaraes.com/posts/scaling-…

@JackEllis already wrote a very interesting blog on how Fathom Analytics scaled Laravel, but my story is about different.
@JackEllis This is how we scaled to over a hundred million jobs and peaks of 30,000 requests/minute a timespan of only twelve hours, using nothing but Laravel, MySQL and Redis.
@JackEllis First, I must give some context: in 2019 I joined a pretty cool product: a SaaS that allowed companies to send SMS marketing campaigns.
Read 54 tweets
Jul 24, 2022
Escalando uma aplicação para >100M jobs diários e dezenas de milhares de requisições por minuto

Nessa thread eu vou mostrar como escalamos uma aplicação usando um stack que muita gente rolaria os olhos: Laravel, Redis e MySQL. Só.
Segue o fio 👇🏻
Cc @sseraphini
@sseraphini Em 2019 eu entrei num projeto que era um gerenciador de campanhas SMS.
Basicamente, grandes marcas pagavam um valor mensal + um markup por mensagem enviada.
Eles faziam upload das suas próprias listas e depois segmentavam usando as campanhas.
@sseraphini Nessa época, a plataforma enviava, por dia, entre 1 e 2M de mensagens. Uma coisa importante é que enviar uma mensagem envolvia algumas outras coisas: pra cada uma, nós recebíamos um webhook do provedor informando o “delivery status” daquela mensagem.
Read 40 tweets
Jan 22, 2021
I compiled a list of good videos/posts for Laravel and PHP developers. It's not completed but I'll add new tweets later.
If you're working with Laravel and PHP, check this out — you might know some stuff already but I'm sure there are new things to learn here. Thread 👉
From @mateusjatenee (me)

What is dependency Injection and the Container:
Repository Pattern:
How to start writing tests:
Data transfer objects:
From @PovilasKorop

Auth in Laravel 8:
Refactoring duplicate controller code:
Many-to-Many relationships:
Multi-Tenancy:
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(