Beyond app()->make(): The Container Features Most Developers Skip
Most Laravel developers register a singleton, call app(MyService::class), and move on. The container, however, ships with three underused features that dramatically improve how you model competing implementations, group related services, and inject dependencies into arbitrary callables.
Contextual Binding
Contextual binding lets you resolve different concrete implementations of the same interface depending on which class is requesting it.
// AppServiceProvider::register()
$this->app
->when(OrderController::class)
->needs(PaymentGatewayInterface::class)
->give(StripeGateway::class);
$this->app
->when(SubscriptionController::class)
->needs(PaymentGatewayInterface::class)
->give(BraintreeGateway::class);
Both controllers type-hint PaymentGatewayInterface. The container resolves the correct concrete at build time — no service locator, no if branches in the constructor.
You can also pass a closure for runtime logic:
$this->app
->when(ReportExporter::class)
->needs(StorageInterface::class)
->give(fn ($app) => $app->make(
config('exports.driver') === 's3' ? S3Storage::class : LocalStorage::class
));
This keeps environment-specific wiring in the service provider, not scattered across business logic.
Tagging
Tagging lets you group multiple bindings under a label and resolve them all at once — perfect for plugin systems, notification channels, or rule pipelines.
// Register
$this->app->bind(SlackNotifier::class);
$this->app->bind(EmailNotifier::class);
$this->app->bind(SmsNotifier::class);
$this->app->tag(
[SlackNotifier::class, EmailNotifier::class, SmsNotifier::class],
'notifiers'
);
// Resolve all tagged services
class NotificationDispatcher
{
public function __construct(
private iterable $notifiers
) {}
public static function register(Application $app): self
{
return new self($app->tagged('notifiers'));
}
public function dispatch(Notification $note): void
{
foreach ($this->notifiers as $notifier) {
$notifier->send($note);
}
}
}
$app->tagged() returns a lazy TaggedIterator, so concrete classes are only instantiated when the loop runs. Adding a new notifier is one line in the service provider — zero changes to NotificationDispatcher.
Method Injection
The container can resolve and call any callable, injecting type-hinted parameters automatically via call().
$result = app()->call([InvoiceService::class, 'generate'], [
'orderId' => $order->id,
]);
The container builds InvoiceService, resolves every type-hinted parameter of generate(), and merges in the explicitly passed $orderId. This is how route model binding and controller method injection work internally.
A practical use case — a command bus that dispatches to handler methods without a rigid interface:
class CommandBus
{
public function dispatch(object $command): mixed
{
$handler = app(get_class($command) . 'Handler');
return app()->call([$handler, 'handle'], [
'command' => $command,
]);
}
}
Handlers declare whatever services they need as additional parameters; the container satisfies them. No abstract HandleInterface required.
Testing Implications
All three features make swapping implementations in tests trivial:
// In a Pest test
beforeEach(function () {
$this->app->bind(PaymentGatewayInterface::class, FakeGateway::class);
});
Contextual bindings can be overridden the same way — re-register in setUp and the container prefers the last binding for that context.
Takeaways
- Contextual binding eliminates gateway/driver
ifbranches from constructors; wiring lives in the service provider. - Tagging enables open/closed plugin systems — add implementations without touching consumers.
app()->call()powers method injection for command buses, action classes, and any callable that needs container-resolved dependencies.- All three features are fully compatible with Laravel's standard
bind/singletonlifecycle and test overrides. - Prefer explicit contextual bindings over environment checks inside constructors; it keeps business classes environment-agnostic.