×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravelÒÀÀµ×¢ÈëÔõôд

laravel×÷Ϊһ¿îÊ¢ÐеÄphp¿ò¼Ü£¬Æä½¹µãÌØÕ÷Ö®Ò»¾ÍÊÇÒÀÀµ×¢È루dependency injection£¬ÒÔϼò³Ædi£©»úÖÆ¡£Ê¹ÓÃÒÀÀµ×¢Èë¿ÉÒÔ½«¹¤¾ßµÄÒÀÀµ¹Øϵ´Ó¹¤¾ß×Ô¼ºÖнâñî³öÀ´£¬´Ó¶øÌá¸ß´úÂëµÄ¿É¶ÁÐÔ¡¢¿É²âÊÔÐԺͿÉά»¤ÐÔ¡£±¾ÎĽ«ÏÈÈÝlaravelÒÀÀµ×¢ÈëµÄʵÏÖ·½·¨ÒÔ¼°Ê¹ÓÃÒªÁì¡£

»ù´¡¿´·¨

ÒÀÀµ×¢ÈëÊÇÒ»ÖÖÉè¼Æģʽ£¬Æä½¹µãÍ·ÄÔÊÇͨ¹ý½«¹¤¾ßËùÐèÒªµÄÒÀÀµ¹Øϵ´Ó¹¤¾ß×Ô¼ºÖнâñî³öÀ´£¬ÒÔµÖ´ï½µµÍñîºÏÐÔ¡¢Ìá¸ß¿É²âÊÔÐÔ¡¢Ìá¸ß¿Éά»¤ÐÔµÈÄ¿µÄ¡£ÔÚLaravel¿ò¼ÜÖУ¬DI»úÖƵÄʵÏÖ»ùÓÚÒÀÀµ×¢ÈëÈÝÆ÷£¨Dependency Injection Container£¬ÒÔϼò³ÆDIÈÝÆ÷£©£¬Ö÷Òª°üÀ¨ÒÔϼ¸¸öÒªµã£º

¹¤¾ßÊôÐÔµÄ×Ô¶¯×¢È룺DIÈÝÆ÷»á¶ÔÐèҪעÈ빤¾ßµÄÊôÐÔ¾ÙÐÐ×Ô¶¯×¢Èë¡£

ÒÀÀµ¹ØϵµÄ×Ô¶¯°ó¶¨ÆÊÎö£ºDIÈÝÆ÷»á¶ÔÀàÖ®¼äµÄÒÀÀµ¹Øϵ¾ÙÐÐ×Ô¶¯°ó¶¨Ï¢ÕùÎö¡£

µ¥ÀýģʽµÄÖ§³Ö£ºDIÈÝÆ÷¿ÉÒÔÖ§³Öµ¥ÀýģʽµÄʹÓá£

ÏàʶÁËDIÈÝÆ÷µÄ»ù±¾¿´·¨Ö®ºó£¬ÎÒÃǾͿÉÒÔ×îÏÈ̽ÌÖÆäÔÚLaravel¿ò¼ÜÖеÄÏêϸʵÏÖ·½·¨¡£

DIÈÝÆ÷µÄʵÏÖ

ÔÚLaravel¿ò¼ÜÖУ¬DIÈÝÆ÷µÄʵÏÖ»ùÓÚЧÀÍÈÝÆ÷£¨Service Container£©£¬¼´AppÀà¡£AppÀàÊÇÕû¸öÓ¦ÓõĽ¹µãÀ࣬ËüÈÏÕæÖÎÀí¡¢×¢²áÏ¢ÕùÎöËùÓÐÐèҪʹÓõÄЧÀͺÍÒÀÀµ£¬Òò´ËËüÒ²ÊÇLaravelÖÐDIÈÝÆ÷µÄ½¹µãÀà¡£ÏÂÃæÎÒÃÇÀ´¿´¿´Ð§ÀÍÈÝÆ÷µÄÏêϸʵÏÖ·½·¨¡£

2.1 ЧÀ͵Ä×¢²áÓëÆÊÎö

LaravelÖеÄЧÀÍ×¢²áÖ÷Òª°üÀ¨Á½¸ö°ì·¨£º×¢²áЧÀÍÌṩÕߺÍ×¢²áЧÀÍ¡£×¢²áЧÀÍÌṩÕßÊÇΪÁËÀû±ãͳһÖÎÀíºÍ×¢²áЧÀÍ£¬¶ø²»ÊÇÿ´Î¶¼ÒªÊÖ¶¯×¢²áЧÀÍ¡£¶ø×¢²áЧÀÍÔòÊÇΪÁ˽«¹¤¾ß×¢ÈëDIÈÝÆ÷£¬ÒÔ¹©Ö®ºóµÄÆÊÎöʹÓá£

×¢²áЧÀÍÌṩÕß¿ÉÒÔÔÚconfig/app.phpÉèÖÃÎļþÖоÙÐÐÉèÖá£ÔÚprovidersÊý×éÖÐÌí¼ÓЧÀÍÌṩÕß¼´¿É¡£ÀýÈ磺

'providers' => [
    // Laravel Framework Service Providers...
    IlluminateAuthAuthServiceProvider::class,
    IlluminateBroadcastingBroadcastServiceProvider::class,
    IlluminateBusBusServiceProvider::class,
    IlluminateCacheCacheServiceProvider::class,

    // Application Service Providers...
    AppProvidersAppServiceProvider::class,
    AppProvidersAuthServiceProvider::class,
    AppProvidersEventServiceProvider::class,
    AppProvidersRouteServiceProvider::class,
],

µÇ¼ºó¸´ÖÆ

×¢²áЧÀÍÔò¿ÉÒÔͨ¹ýAppÀàµÄbindÒªÁì¾ÙÐÐ×¢²á¡£ÀýÈ磺

// ½«FooÀà×¢²áµ½DIÈÝÆ÷ÖÐ
app()->bind('foo', function() {
    return new Foo();
});

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄʾÀý´úÂëÖУ¬ÎÒÃÇͨ¹ýbindÒªÁ콫FooÀà×¢²áµ½DIÈÝÆ÷ÖУ¬²¢Ö¸¶¨Æä¶ÔÓ¦µÄÓÖÃûΪfoo¡£×¢ÖØ£¬ÔÚLaravelÖпÉÒÔΪЧÀÍÖ¸¶¨ÓÖÃû£¬ÒÔ±ãÓÚ¸üÀû±ãµØ¾ÙÐÐÆÊÎöºÍŲÓá£

ÆÊÎöЧÀÍÔò¿ÉÒÔͨ¹ýAppÀàµÄmakeÒªÁì¾ÙÐÐÆÊÎö¡£ÀýÈ磺

// ͨ¹ýÓÖÃûÆÊÎöFooÀàʵÀý
$foo = app('foo');

µÇ¼ºó¸´ÖÆ

2.2 ЧÀ͵Ä×Ô¶¯ÆÊÎö

ÔÚLaravelÖУ¬DIÈÝÆ÷»á×Ô¶¯ÆÊÎöÀàÖ®¼äµÄÒÀÀµ¹Øϵ£¬Òò´ËÎÒÃÇÎÞÐèÊÖ¶¯×ª´ïÒÀÀµ¹Øϵ£¬¾Í¿ÉÒÔÈÃDIÈÝÆ÷×Ô¶¯°ïÎÒÃǽâ¾öÒÀÀµ×¢ÈëµÄÎÊÌâ¡£ÀýÈ磬ÎÒÃÇÓÐÈçÏÂÁ½¸öÀࣺ

class Foo {
    public function __construct(Bar $bar) {}
}

class Bar {
    public function __construct(Baz $baz) {}
}

µÇ¼ºó¸´ÖÆ

ÕâÀFooÀàÒÀÀµÓÚBarÀ࣬BarÀàÓÖÒÀÀµÓÚBazÀà¡£ÈôÊÇÎÒÃÇÏëÒª½¨ÉèÒ»¸öFooÀàµÄʵÀý£¬ÄÇôÎÒÃÇÖ»ÐèҪͨ¹ýDIÈÝÆ÷ÆÊÎö¼´¿É£º

// ×Ô¶¯ÆÊÎöÒÀÀµ¹Øϵ£¬²¢Í¨¹ýDIÈÝÆ÷½¨ÉèFooÀàʵÀý
$foo = app(Foo::class);

µÇ¼ºó¸´ÖÆ

ÕâÀDIÈÝÆ÷»á×Ô¶¯½¨ÉèBarºÍBazÀàµÄʵÀý£¬²¢½«ËüÃÇ×¢Èëµ½FooÀàµÄ½á¹¹º¯ÊýÖС£

2.3 ЧÀ͵ĵ¥Àýģʽ

ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýsingletonÒªÁ콫ЧÀÍ×¢²áΪµ¥Àýģʽ£¬ÕâÒâζ×Åÿ´ÎŲÓøÃЧÀÍʱ¶¼»á·µ»Øͳһ¸öʵÀý¡£ÀýÈ磺

// ½«FooÀà×¢²áΪµ¥Àýģʽ
app()->singleton('foo', function() {
    return new Foo();
});

µÇ¼ºó¸´ÖÆ

ÕâÀÎÒÃÇͨ¹ýsingletonÒªÁ콫FooÀà×¢²áΪµ¥Àýģʽ£¬ÕâÒâζ×Åÿ´ÎŲÓÃfooЧÀÍʱ¶¼»á·µ»Øͳһ¸öFooÀàµÄʵÀý¡£

×ܽá

LaravelµÄDIÈÝÆ÷»úÖÆ´ó´ó¼ò»¯ÁËÒÀÀµ×¢ÈëµÄʵÏÖÀú³Ì£¬Ê¹µÃÎÒÃÇ¿ÉÒÔÔ½·¢ÎÞа¡¢Àû±ãµØÖÎÀíºÍʹÓÃÒÀÀµ¹Øϵ¡£ÔÚʹÓÃLaravel¿ò¼Üʱ£¬ÊìÁ·ÕÆÎÕDIÈÝÆ÷µÄʹÓý«»á¼«´óµØÌá¸ßÎÒÃǵĿª·¢Ð§ÂÊ£¬²¢ÇÒÒ²½«´úÂëµÄ¿É²âÊÔÐÔ¡¢¿Éά»¤ÐԵȷ½Ãæ´øÀ´Ðí¶àÀûÒæ¡£±¾ÎÄÏÈÈÝÁËLaravelÖÐDIÈÝÆ÷µÄ»ù´¡¿´·¨¡¢ÊµÏÖ·½·¨ÒÔ¼°Ê¹ÓÃÒªÁ죬ϣÍûÄܹ»¶Ô¸÷ÈËÓÐËù×ÊÖú¡£

ÒÔÉϾÍÊÇlaravelÒÀÀµ×¢ÈëÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ