Laravel: Registering the Manager in the Service Provider

Nothing better than an intelligent way to one line everything

Three keys for success

Registering the Manager

$this->app->singleton('transport', function ($app) {
return new TransportManager($app);
});
$this->app->singleton('transport', TransportManager::class);

Registering the Default Driver

$this->app->singleton('transport.driver', function ($app) {
return $app['transport']->driver();
});

Aliasing the Driver

$this->app->alias('transport.driver', TransportContract::class);
public function send(TransportContract $transport, Package $package)
{
$transport->send($package)->to('Ireland');
}

Bonus: Aliasing the Manager

$this->app->alias('transport', TransportManager::class);

Word of Advice: Always prefer a Class FQN

public function send()
{
app('transport.driver')->send($package)->to('Ireland');
}
Image for post
Image for post
Photo by Jon Tyson on Unsplash
$this->app->singleton(TransportManager::class, function ($app) {
return new TransportManager($app);
});
public function send(Package $package)
{
if ($package->cantBeSent()) {
throw new PackageCantSendException($package);
}
// Now we can call the default transport driver using a Facade
Transport::send($package)->to('Ireland');
session()->flash('alert', trans('package.sent')); return back();
}

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store