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

ʲôÊÇIOCÈÝÆ÷£¿ÎªÊ²Ã´LaravelÖØÊÓËü£¿

laravel ÊÇÒ»¸öÊ¢ÐÐµÄ php ¿ª·¢¿ò¼Ü£¬ËüÉîÊÜ¿ª·¢ÕßµÄϲ»¶¡£laravel ½ÓÄÉÁËÐí¶àÏÖ´ú»¯µÄ¿ª·¢ÒªÁìºÍÉè¼Æģʽ£¬ÆäÖаüÀ¨ ioc ÈÝÆ÷£¨inversion of control container£¬¿ØÖÆ·´×ªÈÝÆ÷£©¡£ÔÚ±¾ÆªÎÄÕÂÖУ¬ÎÒÃǽ«Ì½ÌÖʲôÊÇ ioc ÈÝÆ÷ÒÔ¼°ÎªÊ²Ã´ laravel ÔÆÔÆÖØÊÓËü¡£

ʲôÊÇ IOC ÈÝÆ÷£¿

IOC£¨Inversion of Control£¬¿ØÖÆ·´×ª£©ÊÇÒ»ÖÖÉè¼Æģʽ£¬Ö÷ÒªÓÃÓÚ¼ò»¯´úÂëÖ®¼äµÄñîºÏ¹Øϵ¡£ËüµÄÀíÄîÊǽ«´úÂëÖеÄÒªº¦²¿·Ö½âñʹµÃ´úÂë½á¹¹Ô½·¢ÎÞа¡£ÕâÖÖÍ·ÄÔÖð½¥µØÑÝ»¯³öÁËÒÀÀµ×¢È루Dependency Injection, DI£©ÕâÒ»ÊÖÒÕ¡£ÒÀÀµ×¢Èë¼òÆÓµØ˵¾ÍÊǽ«Ò»¸ö¹¤¾ßËùÒÀÀµµÄÆäËû¹¤¾ßµÄ½¨ÉèºÍÖÎÀí½»Óɸü¸ß¼¶±ðµÄ´úÂëÀ´Íê³É¡£ÕâÑùÒ»À´£¬ÎÒÃÇÿ´ÎÐèÒª»á¼ûij¸öÒѾ­½¨ÉèµÄ¹¤¾ßʱ£¬¾Í¿ÉÒÔͨ¹ýÒÀÀµ×¢ÈëµÄ·½·¨À´»ñµÃÕâ¸ö¹¤¾ß£¬×èÖ¹ÁËÿ´Î¶¼ÐèÒªÊÖ¶¯ÊµÀý»¯Ò»¸ö¹¤¾ßµÄ·±ËöÊÂÇé¡£

¶ø IOC ÈÝÆ÷¾ÍÊÇʵÏÖÒÀÀµ×¢ÈëÊÖÒÕµÄÒ»ÖÖ¹¤¾ß£¬Ëü¿ÉÒÔÖÎÀí¸÷¸öÀàÖ®¼äµÄÒÀÀµ¹Øϵ£¬²¢½«ÕâЩÒÀÀµ¹ØϵעÈëµ½¹¤¾ßÖС£ÔÚ Laravel ÖУ¬IOC ÈÝÆ÷ÊǺÜÊÇÖ÷ÒªµÄ×é¼þ£¬¿ÉÒÔ˵ËùÓеÄЧÀͶ¼Í¨¹ý IOC ÈÝÆ÷×¢Èëµ½Ó¦ÓóÌÐòÖС£

Óŵã

1. ¿Éά»¤ÐÔ¸ü¸ß

IOC ÈÝÆ÷ʹµÃ´úÂëÖ®¼äµÄÒÀÀµÔ½·¢¹Ø×¢½Ó¿Ú¶ø²»ÊÇÌض¨ÊµÏÖµÄϸ½Ú¡£ÕâÑù£¬µ±Ä³¸öÌض¨ÊµÏÖ·ºÆðÎÊÌâµÄʱ¼ä£¬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ׵ؽ«Ëü´ÓϵͳÖÐÒƳý²¢Ì滻Ϊһ¸öеÄʵÏÖ£¬¶ø²»»á¶Ô´úÂëÖÐÆäËû²¿·ÖÔì³ÉÌ«´óµÄÓ°Ïì¡£

2. Ò×ÓÚ²âÊÔ

ÓÉÓÚÎÒÃÇ¿ÉÒÔÇáËɵؽ«ÊµÏÖ´Ó×ÅʵÀý»¯µãÊèÉ¢¿ªÀ´£¬ÒÔÊÇÎÒÃÇ¿ÉÒÔÇáËɵØÄ£ÄâÕâЩʵÏÖÀ´²âÊÔÓëËüÃǽ»»¥µÄ´úÂë¡£

3. Ô½·¢ÎÞа

ʹÓà IOC ÈÝÆ÷¿ÉÒÔʹÄúµÄ´úÂëÔ½·¢ÎÞа¡£ÀýÈ磬Äú¿ÉÒÔÇáËɵØÔÚÉú²úÇéÐκͿª·¢ÇéÐÎÖн»Á÷¹¤¾ßʵÏÖ¡£

Laravel ÖÐµÄ IOC ÈÝÆ÷

ÔÚ Laravel ÖУ¬IOC ÈÝÆ÷ÊÇÒ»¸öÖ§³ÖÒÀÀµ×¢ÈëµÄЧÀÍÈÝÆ÷¡£Í¨¹ýʹÓà Laravel µÄ IOC ÈÝÆ÷£¬ÎÒÃÇ¿ÉÒÔ¿ìËٵؽ«ÒÀÀµÏî×¢Èëµ½ÐèÒªËüÃǵÄÿ¸öÀàÖС£

×¢²áЧÀÍ

ÔÚ Laravel ÖУ¬ÎÒÃÇͨ¹ý½«Àà×¢²áµ½ IOC ÈÝÆ÷ÖÐÀ´¸æËßÈÝÆ÷ÔõÑùʵÀý»¯ºÍ×¢ÈëËüÃÇ¡£Õâ¿ÉÒÔͨ¹ý ServiceProvider ÀàÍê³É¡£ServiceProvider ÊÇÒ»¸öÁýͳÀ࣬±ØÐèʵÏÖ register() ÒªÁìÀ´¸æËß IOC ÈÝÆ÷ÔõÑù°ó¶¨Ð§ÀÍ£¬ÒÔ¼° boot() ÒªÁìÀ´ÔÚЧÀÍÈÝÆ÷×¢²áºóÖ´ÐвÙ×÷¡£

ÒÔÏÂÊÇÒ»¸öʾÀý ServiceProvider Àࣺ

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Services\ExampleService;

class ExampleServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app-&gt;bind('example', function () {
            return new ExampleService();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸ö ServiceProvider ÀàÖУ¬ÎÒÃÇÔÚ register() ÒªÁìÖаó¶¨ÁËÒ»¸öЧÀÍ example¡£ ¸ÃЧÀ͵ÄΨһ±êʶ·ûÊÇ ‘example’£¬°ó¶¨ÁËÒ»¸ö±Õ°ü£¬¸Ã±Õ°ü½«ÔÚÿ´ÎÇëÇó¸ÃЧÀÍʱ·µ»Ø ExampleService µÄÐÂʵÀý¡£

ÆÊÎöЧÀÍ

ÒªÆÊÎöÔÚ IOC ÈÝÆ÷ÖÐ×¢²áµÄЧÀÍ£¬¿ÉÒÔʹÓà Laravel µÄ Facades »òÕßÊÖ¶¯µØÆÊÎöЧÀÍ¡£Ê¹Óà Facade ¿ÉÒÔ¸üΪÀû±ãµØ»á¼ûЧÀÍ£¬ÓÉÓÚËü¿ÉÒÔÔÚ¸üÉٵĴúÂëÖÐÍê³É IOC ÈÝÆ÷ÖÎÀíµÄЧÀ͹¤¾ßµÄÆÊÎö¡£

ÀýÈ磬ҪÔÚ¿ØÖÆÆ÷ÖÐʹÓà ExampleService£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂëÆÊÎöЧÀÍ£º

use Illuminate\Support\Facades\App;
use App\Services\ExampleService;

class ExampleController extends Controller
{
    public function index()
    {
        $example = App::make(ExampleService::class);
        // »òÕߣº$example = app()-&gt;make(ExampleService::class);
        // »òÕߣº$example = resolve(ExampleService::class);
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇŲÓÃÁË App Facade µÄ make() ÒªÁìÀ´ÆÊÎö ExampleService¡£Äú»¹¿ÉÒÔʹÓà app() º¯Êý»ò resolve() º¯ÊýÒÔÏàͬµÄ·½·¨ÊµÏָù¦Ð§¡£

×Ô¶¯ÆÊÎöÒÀÀµÏî

Laravel µÄ IOC ÈÝÆ÷»¹ÄÜ×Ô¶¯ÆÊÎöÒÀÀµÏͨ¹ýÕâÖÖ·½·¨£¬µ±ÄãÐèÒªÒ»¸öÀàµÄÐÂʵÀýʱ£¬Laravel µÄ IOC ÈÝÆ÷Äܹ»×Ô¶¯²éÕÒËüËùÒÀÀµµÄÀ࣬²¢ÊµÀý»¯ËüÃÇ¡£Õ⼫´óµØïÔÌ­ÁË¿ª·¢Õߵļ縺¡£

ÀýÈ磬¼ÙÉèÎÒÃÇÓÐÒÔÏÂÁ½¸öÀࣺ

namespace App\Services;

class ExampleService
{
    public function __construct(ExampleRepository $repository)
    {
        //
    }
}

class ExampleRepository
{
    public function findAll()
    {
        //
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖУ¬ExampleService ÒÀÀµ ExampleRepository¡£Òª½¨Éè ExampleService µÄʵÀý£¬Laravel µÄ IOC ÈÝÆ÷½«×Ô¶¯²éÕÒ ExampleRepository Àಢ½«ËüµÄʵÀý×¢Èëµ½ ExampleService ÖС£

½áÂÛ

Laravel ÖÐµÄ IOC ÈÝÆ÷ʹµÃЧÀ͵Ä×¢²áÏ¢ÕùÎö±äµÃ¼òÆÓÃ÷Îú£¬Ëõ¶ÌÁË¿ª·¢Ê±¼ä£¬Ìá¸ßÁËÓ¦ÓóÌÐòµÄ¿Éά»¤ÐԺͿɲâÊÔÐÔ¡£Í¨¹ý IOC ÈÝÆ÷£¬ÎÒÃÇ¿ÉÒÔÔ½·¢×¨×¢ÓÚ±àд¸ßÖÊÁ¿µÄÓªÒµ´úÂ룬¶ø²»±ØÌåÌùЧÀ͵ÄʵÀý»¯ºÍÖÎÀíϸ½Ú¡£Òò´Ë£¬Ñ§Ï°ºÍÕÆÎÕ Laravel ÖÐµÄ IOC ÈÝÆ÷ÊÖÒÕ£¬¹ØÓÚ Laravel ¿ª·¢ÕßÀ´ËµÊǺÜÊÇÖ÷ÒªµÄ¡£

ÒÔÉϾÍÊÇʲôÊÇIOCÈÝÆ÷£¿ÎªÊ²Ã´LaravelÖØÊÓËü£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ