Service Container
1. Introduction
<? php namespace App\Http\Controllers; use App\User; use App\Repositories\UserRepository; use App\Http\Controllers\Controller; class UserController extends Controller { /** * The user repository implementation. * * @var UserRepository */ protected $users; /** * Create a new controller instance. * * @param UserRepository $users * @return void */ public function __construct(UserRepository $users) { $this->users = $users; } /** * Show the profile for the given user. * * @param int $id * @return Response */ public function show($id) { $user = $this->users->find($id); return view('user.profile', ['user' => $user]); } }
2. Binding
Binding foundation
Note: If a class is not based on any interface, it is unnecessary to bind it to the container. The container does not need to be told how to build objects, because it will automatically parse out specific objects using PHP's reflection service.
$this->app->bind('HelpSpot\API', function ($app) { return new HelpSpot\API($app->make('HttpClient')); });
$this->app->singleton('HelpSpot\API', function ($app) { return new HelpSpot\API($app->make('HttpClient')); });
$api = new HelpSpot\API(new HttpClient); $this->app->instance('HelpSpot\Api', $api);
$this->app->when('App\Http\Controllers\UserController') ->needs('$variableName') ->give($value);
Bind interface to implementation
$this->app->bind( 'App\Contracts\EventPusher', 'App\Services\RedisEventPusher' );
use App\Contracts\EventPusher; /** *Create a new class instance * * @param EventPusher $pusher * @return void */ public function __construct(EventPusher $pusher){ $this->pusher = $pusher; }
Context binding
use Illuminate\Support\Facades\Storage; use App\Http\Controllers\VideoController; use App\Http\Controllers\PhotoControllers; use Illuminate\Contracts\Filesystem\Filesystem; $this->app->when(PhotoController::class) ->needs(Filesystem::class) ->give(function () { return Storage::disk('local'); }); $this->app->when(VideoController::class) ->needs(Filesystem::class) ->give(function () { return Storage::disk('s3'); });
label
$this->app->bind('SpeedReport', function () { // }); $this->app->bind('MemoryReport', function () { // }); $this->app->tag(['SpeedReport', 'MemoryReport'], 'reports');
$this->app->bind('ReportAggregator', function ($app) { return new ReportAggregator($app->tagged('reports')); });
3. Parse
Make method
$fooBar = $this->app->make('HelpSpot\API');
$api = resolve('HelpSpot\API');
Automatic injection
<? php namespace App\Http\Controllers; use App\Users\Repository as UserRepository; class UserController extends Controller{ /** *User warehouse instance */ protected $users; /** *Create a controller instance * * @param UserRepository $users * @return void */ public function __construct(UserRepository $users) { $this->users = $users; } /** *Display users by specifying IDs * * @param int $id * @return Response */ public function show($id) { // } }
4. Container Events
$this->app->resolving(function ($object, $app) { // Called when container resolves object of any type... }); $this->app->resolving(HelpSpot\API::class, function ($api, $app) { // Called when container resolves objects of type "HelpSpot\API"... });