// public/index.php <?php require __DIR__.'/../bootstrap/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; $kernel = $app->make(IlluminateContractshttpKernel::class); $response = $kernel->handle( $request = IlluminateHttpRequest::capture() ); $response->send(); $kernel->terminate($request, $response);
// bootstrap/app.php <?php $app = new IlluminateFoundationApplication( realpath(__DIR__.'/../') ); $app->singleton( IlluminateContractsHttpKernel::class, AppHttpKernel::class ); $app->singleton( IlluminateContractsconsoleKernel::class, AppConsoleKernel::class ); $app->singleton( IlluminateContractsDebugExceptionHandler::class, AppExceptionsHandler::class ); return $app;
//使用閉包 $container->bind('BarInterface', function(){ return new Bar(); }); //或者使用字符串 $container->bind('FooInterface', 'Foo');
$container->singleton('BarInterface', function(){ return new Bar(); });
$bar = new Bar(); $bar->setSomething(new Something); $container->instance('Bar', $bar);
$container->when('Man') ->needs('PartnerInterface') ->give('Woman'); $container->when('Woman') ->needs('PartnerInterface') ->give('Man');
$container->bind('Father', function () { // }); $container->bind('Mother', function () { // }); $container->bind('Daughter', function () { // }); $container->bind('Son', function () { // }); $container->tag(['Father', 'Mother', 'Daughter', 'Son'], 'familyMembers'); $container->bind('Family', function ($container) { return new Family($container->tagged('familyMembers')); });
$foo = $container->make('Foo');
$bar = $container['Bar'];
$familyMembers = $container->tagged('familyMembers'); foreach ($familyMembers as $inpidual) { $inpidual->doSomething(); }
$container->resolving(function ($object, $container) { // 當容器解析任何類型的對象時會被調(diào)用... }); $container->resolving('Foo', function (Foo $foo, $container) { // 當容器解析「Foo」類型的對象時會被調(diào)用... });
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END
喜歡就支持一下吧
相關(guān)推薦