laravel di·´×ªÔÀí
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüÒÔÆ侫Á·¡¢ÓÅÑÅ¡¢Ò×ÓÚʹÓõÄÌصã±ã¹ãÊÜ¿ª·¢ÕßµÄÇàíù¡£×Åʵ£¬laravel¿ò¼ÜµÄÒ»¸öÖ÷ÒªÌØÕ÷¾ÍÊÇÒÀÀµ×¢È루di£©£¬·´×ª¿ØÖÆ£¨ioc£©£¬ÕâʹµÃlaravelÔÚ´¦ÀíÓ¦ÓóÌÐòÒÀÀµÊ±±äµÃÔ½·¢ÓÅÑźÍÈÝÒס£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«ÉîÈë̽ÌÖlaravelµÄdiºÍ·´×ªÔÀí¡£
ʲôÊÇÒÀÀµ×¢È룿
ÔÚÒ»¸öÓ¦ÓóÌÐòÖУ¬Ò»¸öÀàͨ³£»áÒÀÀµÁíÒ»¸öÀà»ò¹¤¾ß²Å»ªÍê³ÉÆäʹÃü¡£ÔڹŰåµÄ±à³ÌÄ£×ÓÖУ¬ÎÒÃÇͨ³£»áÔÚÀàÖÐʵÀý»¯ÕâЩÒÀÀµÏî¡£ÕâÖÖ·½·¨ËäÈ»¼òÆÓÃ÷Îú£¬¿ÉÊÇËü»áʹµÃÀàÓëÒÀÀµÏîÖ®¼äϸÃÜñîºÏ¡£
ÒÀÀµ×¢ÈëÊÇÒ»ÖÖÉè¼Æģʽ£¬¿ÉÒÔ½«ÀàºÍËüËùÒÀÀµµÄ¹¤¾ßÊèÉ¢£¬²¢Í¨¹ý½Ó¿Ú½«ËüÃÇÁªÏµÆðÀ´¡£ÒÀÀµÏîÔڽṹº¯ÊýÖÐת´ï¸øÀ࣬´Ó¶øʹµÃÀà¿ÉÒÔÔÚÔËÐÐʱʹÓÃËüÃÇ¡£Òò´Ë£¬ÒÀÀµ×¢Èë¿ÉÒÔʹµÃÓ¦ÓóÌÐòºÍÀàÖ®¼äµÄ¹ØϵԽ·¢ÎÞаºÍ¿ÉÀ©Õ¹¡£
LaravelµÄÒÀÀµ×¢Èë
ÔÚLaravel¿ò¼ÜÖУ¬ÒÀÀµ×¢Èëͨ¹ýЧÀÍÈÝÆ÷ºÍ°ó¶¨ÊµÏÖ¡£Ð§ÀÍÈÝÆ÷ÊÇLaravelµÄÒÀÀµ×¢ÈëÈÝÆ÷£¬ËüÔÊÐíÄúÖÎÀíºÍ×¢ÈëÒÀÀµÏî¡£
°ó¶¨Êǽ«½Ó¿Ú»òÀà°ó¶¨µ½Ð§ÀÍÈÝÆ÷ÖеÄʵÀý¡£Ò»µ©°ó¶¨Íê³É£¬Äú¿ÉÒÔʹÓÃÈÝÆ÷ÆÊÎöÆ÷À´´ÓÈÝÆ÷ÖÐʵÀý»¯ÕâЩ¹¤¾ß¡£
ÔÚLaravel¿ò¼ÜÖУ¬Äú¿ÉÒÔʹÓÃÈýÖÖ°ó¶¨ÀàÐÍ£º°ó¶¨ÊµÀý¡¢°ó¶¨ÉÏÏÂÎÄ¡¢°ó¶¨½Ó¿Ú»òÁýͳÀà¡£ÏÂÃæÎÒÃÇ»®·ÖÀ´ÏàʶһÏÂËüÃÇ¡£
2.1 °ó¶¨ÊµÀý
µ±ÄúÐèÒª½«Ò»¸ö¹¤¾ß×¢²áµ½Ð§ÀÍÈÝÆ÷ʱ£¬Ê¹Óðó¶¨ÊµÀýºÜÊÇÓÐÓá£ÕâÖÖÇéÐÎͨ³£ÔÚ¿ØÖÆÆ÷ÖзºÆð£¬ÓÉÓÚÎÒÃÇÐèÒª½«Ðí¶à²î±ðµÄ¹¤¾ß×¢Èëµ½ÆäÖС£
ÀýÈ磬¼ÙÉèÎÒÃÇÓÐÒ»¸öTaskControllerÀ࣬ÐèҪעÈëÒ»¸öTaskRepository¹¤¾ßÀ´´¦ÀíʹÃü£º
class TaskController extends Controller { protected $taskRepository; public function __constructor(TaskRepository $taskRepository) { $this->taskRepository = $taskRepository; } }
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔʹÓÃÒÀÀµ×¢ÈëÀ´×¢ÈëTaskRepositoryÒÀÀµÏΪ´Ë£¬ÎÒÃÇÐèÒªÔÚЧÀÍÈÝÆ÷Öаó¶¨TaskRepositoryµÄʵÀý£º
$this->app->bind('TaskRepository', function($app) { return new TaskRepository(); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃ$appʵÀýÀ´½¨ÉèTaskRepositoryµÄÐÂʵÀý£¬È»ºó½«Æä·µ»Ø¡£Ò»µ©ÎÒÃǽ«TaskRepository°ó¶¨µ½ÈÝÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃTaskControllerµÄ½á¹¹º¯Êý½«Æä×¢Èëµ½¿ØÖÆÆ÷ÖС£
2.2 °ó¶¨ÉÏÏÂÎÄ
°ó¶¨ÉÏÏÂÎÄÔÊÐíÎÒÃÇÔÚЧÀÍÈÝÆ÷Öаó¶¨Ò»¸öÀàµÄʵÀý¡£µ±Àà±»ÆÊÎöʱ£¬Ð§ÀÍÈÝÆ÷»áÏÈʵÑé´Ó°ó¶¨ÉÏÏÂÎÄÖвéÕÒ¸ÃÀ࣬ȻºóÔÚÐèҪʱ½«Æä×¢Èë¡£
ÀýÈ磬ÔÚÎÒÃǵÄÓ¦ÓóÌÐòÖпÉÄÜ»áʹÓõ½¶à¸öTaskRepository¹¤¾ß¡£ÈôÊÇÎÒÃǽ«ËüÃÇËùÓа󶨵½ÈÝÆ÷ÖУ¬ÄÇôÿ¸ö¹¤¾ß¶¼»áÕ¼ÓÃÄÚ´æ¿Õ¼ä£¬²¢ÇÒÎÒÃÇÔÚ¿ØÖÆÆ÷ÖÐʹÓÃTaskRepositoryʱ£¬ÎÞ·¨Çø·ÖʹÓÃÄĸö¹¤¾ß¡£
ΪÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔʹÓÃÉÏÏÂÎİ󶨡£ÕâʹÎÒÃÇÄܹ»½«²î±ðµÄTaskRepositoryʵÀý°ó¶¨µ½ÈÝÆ÷ÉÏÏÂÎÄÖУ¬Í¨¹ýKeyÖµÇø·ÖÊÇʹÓÃÄĸöʵÀý¡£
ÀýÈ磺
$this->app->when('AppHttpControllersTaskController') ->needs('TaskRepository') ->give(function ($app) { if (session('useMock')) { return new TaskRepositoryMock(); } else { return new TaskRepository(); } });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËЧÀÍÈÝÆ÷µÄwhen()ÒªÁ죬Õâ¸öÒªÁìÎüÊÕÒ»¸öÀàÃû×÷Ϊ²ÎÊý£¬ÌåÏÖÔÚÄĸöÀàµÄ½á¹¹º¯ÊýÖÐÐèҪעÈëÒÀÀµÏî¡£
needs()ÒªÁì¸æËßÈÝÆ÷ÎÒÃÇÐèÒªÄĸöÒÀÀµÏ¶øgive()ÒªÁìÏÖʵ·µ»ØÎÒÃÇÐèÒªµÄʵÀý¡£
2.3 °ó¶¨½Ó¿Ú»òÁýͳÀà
ÔÚLaravelÖУ¬ÎÒÃÇͨ³£ÓýӿڻòÁýͳÀàÀ´½ç˵ÍⲿÒÀÀµÏî¡£ÔÚÓ¦ÓóÌÐòÖУ¬ÎÒÃÇ°ÑÕâЩ½Ó¿Ú»òÁýͳÀà°ó¶¨µ½ÏÖʵµÄʵÏÖÀàÉÏ¡£ÕâʹµÃÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷»òÆäËûÀàÖÐʹÓÃÕâЩÒÀÀµÏ¶øÎÞÐèÌåÌùËüÃǵÄÏêϸʵÏÖ¡£
ÀýÈ磬ÎÒÃÇ¿ÉÄÜÓÐÒ»¸öTaskRepository½Ó¿Ú£¬ÓÃÓÚ´¦Àí¹ØÓÚʹÃüµÄһЩ²Ù×÷¡£ÎÒÃÇ¿ÉÒÔ½«Õâ¸ö½Ó¿Ú°ó¶¨µ½Ò»¸öÏêϸµÄÀà¡£ÕâÑù£¬µ±ÎÒÃÇÔÚ¿ØÖÆÆ÷ÖÐÐèÒªÒ»¸öTaskRepository¹¤¾ßʱ£¬Ð§ÀÍÈÝÆ÷¾Í»á×Ô¶¯µØ·µ»ØËüµÄÒ»¸öʵÀý¡£
$this->app->bind('TaskRepository', 'TaskRepositoryImpl');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ«TaskRepository½Ó¿Ú°ó¶¨µ½Ò»¸öÃûΪTaskRepositoryImplµÄÏêϸÀàÉÏ¡£
·´×ª¿ØÖÆ
ÔÚÒÀÀµ×¢ÈëÖУ¬·´×ª¿ØÖÆ£¨IoC£©ÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ¿´·¨¡£ÔڹŰåµÄ±à³ÌÄ£×ÓÖУ¬¿ª·¢Ö°Ô±Í¨³£ÈÏÕ潨É蹤¾ßÒÔ¼°½â¾ö¹¤¾ßÖ®¼äµÄÒÀÀµ¹Øϵ¡£Õâ¾ÍÊÇ¿ØÖÆÁ÷¡£
ÔÚ·´×ª¿ØÖÆÖУ¬¿ØÖÆÁ÷±»·´×ªÁË¡£Ó¦ÓóÌÐò²»ÔÙÈÏÕ潨ÉèºÍÖÎÀí¹¤¾ß£¬¶øÊÇÓÉ¿ò¼Ü»òÈÝÆ÷À´½â¾öÕâ¸öÎÊÌâ¡£ËùÓеÄÒÀÀµ¹Øϵ¶¼ÓÉÈÝÆ÷À´´¦Àí£¬¶ø²»ÊÇÓÉ¿ª·¢Ö°Ô±À´ÖÎÀí¡£
·´×ª¿ØÖƵÄÀûÒæ¾ÍÔÚÓÚ£¬Ëü¿ÉÒÔ´ó´óïÔÌ´úÂëÖеÄñîºÏÐÔ£¬ÈôúÂëÔ½·¢ÎÞаºÍ¿ÉÀ©Õ¹¡£ÕâʹµÃÎÒÃǵĴúÂë¸üÈÝÒײâÊÔºÍά»¤¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËLaravel¿ò¼ÜµÄÒÀÀµ×¢ÈëºÍ·´×ª¿ØÖÆ»úÖÆ¡£Ê¹ÓÃÕâЩ»úÖÆ£¬ÎÒÃÇ¿ÉÒÔʹÎÒÃǵÄÓ¦ÓóÌÐòÔ½·¢ÎÞаºÍ¿ÉÀ©Õ¹£¬Ê¹Æä¸üÒ×ÓÚ²âÊÔºÍά»¤¡£ÏàʶLaravelµÄDIºÍ·´×ªÔÀí¹ØÓÚ¿ª·¢Ö°Ô±¶øÑÔÊǺÜÊÇÓÐ×ÊÖúµÄ£¬ÈôÊÇÄúÏÖÔÚÕýʹÓÃLaravel¿ª·¢Ó¦ÓóÌÐò£¬Ï£Íûͨ¹ý±¾ÎĶÔDIºÍ·´×ªÓÐÒ»¸ö´óÖµÄÏàʶ¡£
ÒÔÉϾÍÊÇlaravel di·´×ªÔÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡