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

ÔõÑùʹÓÃlaravelµÄÒÀÀµ×¢Èë

ÒÀÀµ×¢È루dependency injection£©ÊÇÒ»ÖÖÉè¼Æģʽ£¬Ëü¿ÉÒÔ½µµÍ´úÂëµÄÖØƯºó£¬Ìá¸ß´úÂëµÄ¿Éά»¤ÐԺͿɲâÊÔÐÔ£¬ÊÇÏÖ´ú php Ó¦Óÿª·¢Öбز»¿ÉÉÙµÄÒ»ÏîÊÖÒÕ¡£laravel ÊÇÒ»¸ö¹ãÊܽӴýµÄ php ¿ò¼Ü£¬Ëü×Ô´øÁËÒÀÀµ×¢ÈëÈÝÆ÷£¬¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµØÖÎÀí¹¤¾ßµÄ½¨ÉèºÍÊÍ·Å¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ñ§Ï°ÔõÑùʹÓà laravel µÄÒÀÀµ×¢È빦Ч¡£

Ò»¡¢Ê²Ã´ÊÇ Laravel ÒÀÀµ×¢È룿

ÒÀÀµ×¢ÈëÊÇÒ»ÖÖ±à³Ìģʽ£¬Ëü¿ÉÒÔ½«Ò»¸ö¹¤¾ßµÄÒÀÀµ¹Øϵת´ï¸øŲÓÃËüµÄÆäËû¹¤¾ß£¬´Ó¶øïÔÌ­ñîºÏ¡£ÔÚÃæÏò¹¤¾ß±à³ÌÖУ¬×é³ÉÒ»¸ö¹¤¾ßµÄ¸÷¸ö²¿·ÖÖ®¼äÊDZ£´æÒÀÀµ¹ØϵµÄ£¬Ã¿¸ö¹¤¾ß¶¼ÐèÒªÒÀÀµÆäËû¹¤¾ßµÄʵÀý²Å»ªÕý³£ÊÂÇé¡£ÒÀÀµ×¢ÈëµÄÄ¿µÄ¾ÍÊǽ«ÕâЩÒÀÀµ¹Øϵ´Ó¹¤¾ßÄÚ²¿½âñʹµÃÒ»¸ö¹¤¾ßµÄÒÀÀµ¹ØϵÓÉÍⲿÈÝÆ÷À´ÖÎÀí¡£

¹ØÓÚÒ»¸ö PHP Ó¦ÓóÌÐò£¬ÒÀÀµ×¢ÈëµÄÀú³ÌÒ»Ñùƽ³£·ÖΪÒÔϼ¸¸ö°ì·¨£º

½ç˵ÒÀÀµ¹Øϵ£º½ç˵ÿ¸ö¹¤¾ßËùÒÀÀµµÄÆäËû¹¤¾ß»òÖµ¡£

ÉèÖÃÈÝÆ÷£º½«ÐèÒªµÄ¹¤¾ßʵÀý×¢²áµ½ÈÝÆ÷ÖС£

ÆÊÎöÒÀÀµ¹Øϵ£ºÔÚÐèҪŲÓÃÒ»¸ö¹¤¾ßµÄÒªÁìʱ£¬ÓÉÈÝÆ÷×Ô¶¯ÆÊÎö³öÆäËùÒÀÀµµÄÆäËû¹¤¾ßʵÀý¡£

Laravel µÄÒÀÀµ×¢ÈëÈÝÆ÷ÊÇÒ»¸öºÜÊÇÇ¿Ê¢µÄ¹¤¾ß£¬¿ÉÒÔÀû±ãµØ½â¾öÒÀÀµÖÎÀíµÄÎÊÌâ¡£

¶þ¡¢Laravel ÒÀÀµ×¢ÈëµÄʹÓÃ

ÏÂÃæÎÒÃÇÀ´¿´Ò»ÏÂÔõÑùÔÚ Laravel ÖÐʹÓÃÒÀÀµ×¢Èë¡£

×¢²á¹¤¾ßʵÀý

ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÈÝÆ÷µÄ bind ÒªÁìÀ´×¢²á¹¤¾ßʵÀý¡£ÀýÈ磬ÎÒÃÇҪע²áÒ»¸ö UserService ¹¤¾ß£¬ÔÚЧÀÍÌṩÕßÖÐÌí¼ÓÈçÏ´úÂ룺

public function register()
{
    $this->app->bind(UserService::class, function($app) {
        return new UserService($app->make(UserRepository::class));
    });
}

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃÇʹÓà bind ÒªÁ콫 UserService ×¢²áµ½ÈÝÆ÷ÖУ¬²¢½ç˵ÁËÒ»¸ö±Õ°üº¯ÊýÀ´½¨É蹤¾ßʵÀý¡£ÔÚ±Õ°üº¯ÊýÖУ¬ÎÒÃÇʹÓÃÁËÈÝÆ÷µÄ make ÒªÁìÀ´»ñÈ¡ UserService ËùÒÀÀµµÄ UserRepository ¹¤¾ß¡£

ʹÓÃÒÀÀµ×¢Èë

ÏÖÔÚÎÒÃÇÒѾ­½« UserService ¹¤¾ß×¢²áµ½ÁËÈÝÆ÷ÖУ¬½ÓÏÂÀ´ÔõÑùÔÚ¿ØÖÆÆ÷ÖÐʹÓÃÄØ£¿ºÜ¼òÆÓ£¬ÎÒÃÇÖ»ÐèÒªÔÚ¿ØÖÆÆ÷µÄ½á¹¹º¯ÊýÖнç˵һ¸ö UserService µÄ²ÎÊý¼´¿É¡£ÀýÈ磺

use App\Services\UserService;

class UserController extends Controller
{
    private $user;

    public function __construct(UserService $user)
    {
        $this->user = $user;
    }
}

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃÇÔÚ UserController µÄ½á¹¹º¯ÊýÖнç˵ÁËÒ»¸ö UserService ¹¤¾ß²ÎÊý¡£ÎÒÃDz»ÐèÒªÊÖ¶¯½¨Éè UserService ¹¤¾ß£¬Laravel »á×Ô¶¯½«Æä×¢Èëµ½½á¹¹º¯Êý²ÎÊýÖС£

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

Laravel µÄÒÀÀµ×¢ÈëÈÝÆ÷¿ÉÒÔ×Ô¶¯ÆÊÎöÒÀÀµ¹Øϵ£¬Ê¹µÃÎÒÃÇ¿ÉÒÔÇáËɵØʹÓÃÒÀÀµ×¢Èë¡£ÀýÈçÎÒÃÇÔÚЧÀÍÌṩÕßÖÐ×¢²áÁËÈçÏ´úÂ룺

public function register()
{
    $this->app->bind(UserService::class, function($app) {
        return new UserService($app->make(UserRepository::class));
    });
}

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

ÄÇôÎÒÃÇÔÚ UserController ÖеĽṹº¯ÊýÖоͿÉÒÔÖ±½ÓʹÓà UserService£º

use App\Services\UserService;

class UserController extends Controller
{
    private $user;

    public function __construct(UserService $user)
    {
        $this->user = $user;
    }
}

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

µ±ÎÒÃǽṹ UserController ¹¤¾ßʱ£¬Laravel »á×Ô¶¯¼ì²âµ½ËüÐèÒªÒ»¸ö UserService ¹¤¾ß£¬È»ºó»á×Ô¶¯½¨ÉèÒ»¸ö UserService ¹¤¾ß£¬²¢×¢Èëµ½ UserController ÖС£

ת´ï²ÎÊý

ÓÐʱ¼äÎÒÃÇÐèÒªÔÚ¹¤¾ß½¨ÉèµÄÀú³ÌÖÐת´ï²ÎÊý£¬ÀýÈçÎÒÃÇÔÚ½¨Éè UserService ¹¤¾ßʱÐèҪת´ïÒ»¸ö UserRepository ¹¤¾ß¡£

ÎÒÃÇ¿ÉÒÔͨ¹ýÔڰ󶨹¤¾ßʱת´ï²ÎÊý£¬À´ÊµÏÖÕâ¸ö¹¦Ð§¡£ÀýÈ磺

public function register()
{
    $this->app->bind(UserService::class, function($app, $parameters) {
        return new UserService($parameters['userRepository']);
    });
}

$userService = app(UserService::class, ['userRepository' => new UserRepository()]);

µÇ¼ºó¸´ÖÆ

ÕâÑù¾Í¿ÉÒÔ½¨ÉèÒ»¸ö UserService ¹¤¾ß£¬²¢½«Ò»¸öÐ嵀 UserRepository ¹¤¾ßת´ï¸øËü¡£

¼ÌÐøÒÀÀµ¹Øϵ

ÓÐʱ¼ä¶à¸öÀà»áÒÀÀµÍ³Ò»¸öÀ࣬ÎÒÃÇ¿ÉÒÔʹÓüÌÐøÀ´¼ò»¯ÒÀÀµ×¢ÈëµÄ´úÂë¡£ÀýÈ磺

class BaseService
{
    protected $repository;

    public function __construct(BaseRepository $repository)
    {
        $this->repository = $repository;
    }
}

class UserService extends BaseService
{
    public function __construct(UserRepository $userRepository)
    {
        parent::__construct($userRepository);
    }
}

µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃǽç˵ÁËÒ»¸ö BaseService ÀàÀ´ÖÎÀí BaseRepository ¹¤¾ßµÄÒÀÀµ¹Øϵ£¬²¢ÔÚ UserService ÀàÖмÌÐø BaseService Àà¡£ÕâÑùÔÚ UserService ÖоͿÉÒÔÖ±½ÓʹÓà BaseRepository ¹¤¾ßÁË¡£

Èý¡¢×ܽá

Laravel µÄÒÀÀµ×¢ÈëÈÝÆ÷ÊÇÒ»¸öºÜÊÇÇ¿Ê¢µÄ¹¤¾ß£¬¿ÉÒÔÀû±ãµØ½â¾öÒÀÀµÖÎÀíµÄÎÊÌâ¡£ÔÚʹÓà Laravel µÄÒÀÀµ×¢Èëʱ£¬ÐèҪעÖØһЩϸ½Ú£º

½ç˵ÒÀÀµ¹Øϵ£º½ç˵ÿ¸ö¹¤¾ßËùÒÀÀµµÄÆäËû¹¤¾ß»òÖµ£¬²¢ÔÚЧÀÍÌṩÕßÖÐʹÓà bind ÒªÁ콫ËüÃÇ×¢²áµ½ÈÝÆ÷ÖС£

ʹÓÃÒÀÀµ×¢È룺ÔÚ¿ØÖÆÆ÷µÈÐèҪʹÓù¤¾ßʵÀýµÄµØ·½£¬½ç˵һ¸ö¹¤¾ß²ÎÊý£¬ÔÚÖ´ÐÐʱ Laravel »á×Ô¶¯ÆÊÎöÒÀÀµ¹Øϵ²¢×¢È빤¾ß¡£

ת´ï²ÎÊý£ºÔÚ¹¤¾ß½¨ÉèµÄÀú³ÌÖпÉÒÔʹÓòÎÊýÀ´×ª´ïÒÀÀµ¹¤¾ß¡£

¼ÌÐøÒÀÀµ¹Øϵ£º¿ÉÒÔʹÓüÌÐøÀ´¼ò»¯ÒÀÀµ×¢ÈëµÄ´úÂë¡£

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

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ