PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
130: Laravel Service Providers and Facades
If you've been building in Laravel for a while, you've probably noticed that you rarely ever use the new keyword to instantiate your own services. You just type Cache::get() or Log::info() and things just work. It feels like magic, but as a professional engineer, "magic" is usually just a shorthand for "someone else wrote a lot of boilerplate so I didn't have to." Today, we're going to pull back the curtain on how that actually works by looking at Service Providers and Facades.
The Hard-Coded Headache
Let's imagine you're building a system that integrates with a third-party payment API, like Stripe. The naive approach—the one I see juniors do all the time—is to instantiate the payment client directly inside the controller where the payment happens.
public function store(Request $request)
{
$payment = new StripePaymentGateway(config('services.stripe.secret'));
$payment->charge($request->amount);
return response()->json(['status' => 'success']);
}
On the surface, this is fine. It works. But you've just created a "tight coupling" nightmare. Your controller now knows exactly how to construct a StripePaymentGateway, including which config key it needs. If you decide to switch to Braintree or Paddle next month, you have to hunt down every single controller that uses Stripe and manually change the class name and the constructor arguments. Even worse, testing this is a pain; you can't easily swap that real Stripe client for a "mock" version during a test, meaning your test suite will actually try to hit the Stripe API every time it runs.
Centralizing the Blueprint
This is why we use Service Providers. Think of a Service Provider as the "instruction manual" for the Laravel container. Instead of the controller deciding how to build the payment gateway, we tell Laravel once, in one place, how to do it. We do this by "binding" the class into the container.
// In App\Providers\PaymentServiceProvider.php
public function register()
{
$this->app->singleton(PaymentGatewayInterface::class, function ($app) {
return new StripePaymentGateway(config('services.stripe.secret'));
});
}
By using a singleton, I'm telling Laravel: "The first time someone asks for a payment gateway, build it using this logic, and then just keep that same instance around for the rest of the request." Now, my controller doesn't need to know about Stripe at all. I just type-hint the interface in the constructor, and Laravel's container automatically injects the correct object.
The trade-off here is a bit more upfront boilerplate. You have to create the provider and register it in config/app.php (or let Laravel's auto-discovery handle it). But the payoff is massive: if I switch to Braintree, I change one line in my Service Provider, and the rest of my application never even knows the difference.
Syntactic Sugar vs. Architectural Purity
Now, dependency injection (type-hinting in the constructor) is the "pure" way to do things. But let's be honest: sometimes you're in a deep piece of logic or a quick helper function where adding a constructor just feels like overkill. That's where Facades come in.
A Facade isn't actually a static class; it's a "static proxy" to a class that is already bound in the service container. When you call Payment::charge(), Laravel looks at the Facade, sees it's linked to the PaymentGatewayInterface, grabs the instance from the container, and calls the method on it.
// Creating the Facade
class Payment extends Facade {
protected static function getFacadeAccessor() { return 'payment.gateway'; }
}
I'll be candid: some developers hate Facades because they hide dependencies, making it less obvious what a class is actually doing. However, when used sparingly, they make your code significantly more readable. The key is remembering that because the Facade is just a proxy to the container, you can still mock it in your tests using Payment::shouldReceive('charge')->once(). You get the brevity of a static call without losing the testability of a decoupled architecture.
📋 Practical Task
Implementing a Twilio SMS Wrapper with a Custom Facade
Your goal is to move a hard-coded SMS notification system into a proper Laravel architectural pattern. You will need to:
- Create an
SmsServiceInterfaceand a concreteTwilioSmsServiceclass that takes an API key from the config. - Create a
SmsServiceProviderthat binds theSmsServiceInterfaceto theTwilioSmsServiceas a singleton. - Create an
SmsFacade that allows you to callSms::send($number, $message)from anywhere in the app. - Verify the setup by creating a temporary route that uses the Facade to trigger a send method (you can just
Log::info()the message inside the service to prove it works).
There are no comments for now.