尊龙凯时人生就是博

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
sitemap¡¢ÍøÕ¾µØͼ