laravelÔõô×Ô¶¯ÉèÖÃ
laravelÊÇÏÖÔÚ×îÊ¢ÐеÄphp¿ò¼ÜÖ®Ò»¡£ËüµÄÇ¿Ê¢ºÍÎÞаÐÔÒѾӮµÃÁË¿í´ó¿ª·¢ÕßµÄÇàíù¡£ÆäÖÐÒ»¸ölaravelµÄÓŵã¾ÍÊÇËüµÄ×Ô¶¯ÉèÖá£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«Ì½ÌÖlaravelµÄ×Ô¶¯ÉèÖÃÊÇÔõÑùÊÂÇéµÄ£¬ÒÔ¼°ÔõÑùʹÓÃËüÀ´Ìá¸ßÄúµÄ¿ª·¢Ð§ÂÊ¡£
Ò»¡¢LaravelµÄ×Ô¶¯ÉèÖøÅÊö
LaravelµÄ×Ô¶¯ÉèÖÿÉÒÔ×ÊÖúÄú¿ìËÙÉèÖÃÖÖÖÖЧÀͺÍ×é¼þ£¬¶øÎÞÐèÊÖ¶¯±àд´ó×ڵĴúÂë¡£ÕâЩ×é¼þ°üÀ¨Êý¾Ý¿âÅþÁ¬¡¢»º´æ¡¢ÐÐÁС¢Óʼþ¡¢ÈÏÖ¤¡¢ÊÚȨ¡¢ÊÂÎñµÈµÈ¡£ÕâÒâζ×ÅÄú¿ÉÒÔʹÓÃLaravelÄÚÖõĹ¦Ð§À´¿ìËٴһ¸ö¹¦Ð§Ç¿Ê¢µÄWebÓ¦ÓóÌÐò£¬¶øÎÞÐè×Ô¼ºÊµÏÖÕâЩ×é¼þ¡£
¶þ¡¢LaravelµÄЧÀÍÌṩÕß
LaravelµÄ×Ô¶¯ÉèÖÃÖ÷ÒªÒÀÀµÓÚЧÀÍÌṩÕß¡£Ð§ÀÍÌṩÕßÊÇÒ»¸öÀ࣬Ëü¿ÉÒÔ×¢²áÓ¦ÓóÌÐòÖеÄЧÀÍ¡£ÕâЩЧÀÍ°üÀ¨µ«²»ÏÞÓÚÒÔÏÂÄÚÈÝ£º
LaravelÄÚÖÃЧÀÍ£ºÀýÈçÊý¾Ý¿âÅþÁ¬¡¢»º´æ¡¢ÐÐÁеȡ£
×Ô½ç˵ЧÀÍ£ºÄú¿ÉÒÔ±àд×Ô¼ºµÄЧÀÍÌṩÕߣ¬ÒÔ±ãÔÚÓ¦ÓóÌÐòÖÐ×¢²á×Ô½ç˵ЧÀÍ¡£
ЧÀÍÌṩÕß±ØÐè¼ÌÐøLaravel¿ò¼ÜÖеÄServiceProviderÀà¡£ServiceProviderÖÐÓÐÁ½¸ö½¹µãÒªÁìÐèҪʵÏÖ¡£»®·ÖÊÇregister()ºÍboot()¡£
register()ÒªÁì
register()ÒªÁìÖ÷ÒªÊÇÓÃÓÚ×¢²áЧÀÍ¡£ÔÚregister()ÒªÁìÖУ¬Äú¿ÉÒÔ½«Ð§ÀͰ󶨵½ÈÝÆ÷ÖУ¬ÒÔ±ãÔÚÓ¦ÓóÌÐòµÄÆäËüµØ·½Ê¹Óá£ÀýÈ磺
use IlluminateSupportServiceProvider; class YourServiceProvider extends ServiceProvider { public function register() { $this->app->bind('YourService', function ($app) { return new YourService($app['config']); }); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃǽ«Ð§ÀͰ󶨵½¡°YourService¡±Ãû³ÆÉÏ¡£µ±Ó¦ÓóÌÐòÐèҪʹÓøÃЧÀÍʱ£¬¿ÉÒÔͨ¹ýÈÝÆ÷À´»ñÈ¡¡£
boot()ÒªÁì
boot()ÒªÁìÖ÷ÒªÊÇÓÃÓÚÖ¸µ¼Ð§ÀÍ¡£ÔÚboot()ÒªÁìÖУ¬Äú¿ÉÒÔÖ´ÐÐһЩ³õʼ»¯²Ù×÷£¬²¢ÎªÓ¦ÓóÌÐòÆô¶¯Ð§ÀÍ¡£ÀýÈ磺
use IlluminateSupportServiceProvider; class YourServiceProvider extends ServiceProvider { public function boot() { $this->publishes([ __DIR__.'/path/to/config' => config_path('your-config.php'), ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃÇʹÓÃpublishes()ÒªÁ콫ÉèÖÃÎļþÐû²¼µ½configĿ¼Ï¡£ÕâÑù£¬Ó¦ÓóÌÐò¾Í¿ÉÒÔʹÓøÃÉèÖá£
Èý¡¢LaravelµÄ×Ô¶¯·¢Ã÷
ËäÈ»LaravelµÄЧÀÍÌṩÕßÊǺÜÇ¿Ê¢ºÍÎÞаµÄ£¬¿ÉÊÇÊÖ¶¯×¢²áÿ¸öЧÀÍÌṩÕß»á±äµÃºÜÊÇ·±Ëö¡£LaravelµÄ×Ô¶¯·¢Ã÷¹¦Ð§Í¨¹ý¼ì²âÓ¦ÓóÌÐòÖеÄЧÀÍÌṩÕßÀ´×Ô¶¯×¢²áËüÃÇ¡£ÕâʹµÃ¿ª·¢Ö°Ô±¿ÉÒÔ¸üÇáËɵؼ¯³ÉµÚÈý·½ÌṩµÄ°ü¡£
LaravelµÄ×Ô¶¯·¢Ã÷¹¦Ð§Í¨¹ýcomposer.jsonÎļþÖеġ°extra¡±ÊôÐÔÀ´Íê³É¡£ÀýÈ磺
{ "extra": { "laravel": { "providers": [ "YourServiceProvider" ], "aliases": { "YourAlias": "YourFacade" } } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃǽ«Ð§ÀÍÌṩÕß¡°YourServiceProvider¡±Ìí¼Óµ½×Ô¶¯·¢Ã÷µÄЧÀÍÌṩÕßÁбíÖС£
ËÄ¡¢LaravelµÄÓÖÃû
ÔÚLaravelÖУ¬ÓÖÃûÌṩÁËÒ»ÖÖ¸üÇáÓ¯µÄ·½·¨À´»á¼ûÓ¦ÓóÌÐòÖеÄÀà¡£Äú¿ÉÒÔʹÓÃÓÖÃûÀ´»á¼ûЧÀÍÌṩÕß¡¢ÃÅÃæ»òÈκÎÆäËüÀà¡£ÓÖÃû¿ÉÒÔÔÚЧÀÍÌṩÕßÖÐͨ¹ýaliasesÊôÐÔ½ç˵£¬Ò²¿ÉÒÔÔÚcomposer.jsonÎļþÖÐͨ¹ý¡°extra¡±µÄ¡°aliases¡±ÊôÐÔ¾ÙÐнç˵¡£ÀýÈ磺
{ "extra": { "laravel": { "aliases": { "YourAlias": "YourFacade" } } } } // »òÕß use IlluminateSupportServiceProvider; class YourServiceProvider extends ServiceProvider { public function register() { $this->app->alias(YourFacade::class, 'YourAlias'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃǸøYourFacadeÌí¼ÓÁËÒ»¸öÌåÃû¡°YourAlias¡±¡£
Îå¡¢×Ô½ç˵ÏÂÁî
LaravelµÄ×Ô¶¯ÉèÖù¦Ð§»¹¿ÉÒÔÈÃÄúÇáËɵؽ¨ÉèºÍ×¢²á×Ô½ç˵ÏÂÁî¡£ÄúÖ»ÐèÒª¼ÌÐøLaravelµÄArtisan¿ØÖÆ̨ÏÂÁîÀ࣬²¢½«ËüÃÇ´æ´¢ÔÚÓ¦ÓóÌÐòµÄ¡°app/Console/Commands¡±Îļþ¼ÐÖм´¿É¡£Laravel»á×Ô¶¯É¨Ãè¸ÃÎļþ¼Ð²¢×¢²áÄã½ç˵µÄËùÓÐ×Ô½ç˵ÏÂÁî¡£
Áù¡¢×ܽá
LaravelµÄ×Ô¶¯ÉèÖù¦Ð§ÉÁ¿ª·¢Ö°Ô±¿ÉÒÔ¸üÇáËɵؽ¨ÉèÖØ´óµÄWebÓ¦ÓóÌÐò¡£Ê¹ÓÃЧÀÍÌṩÕß¡¢ÓÖÃû¡¢×Ô¶¯·¢Ã÷ºÍ×Ô½ç˵ÏÂÁÄú¿ÉÒÔÌá¸ß¿ª·¢Ð§ÂÊ£¬²¢ïÔÌÊÖ¶¯±àд´ó×ڵĴúÂë¡£ÕÆÎÕLaravelµÄ×Ô¶¯ÉèÖÃÊÖÒÕ½«ÊÇÄúÀֳɿª·¢»ùÓÚLaravelµÄWebÓ¦ÓóÌÐòµÄÒªº¦¡£
ÒÔÉϾÍÊÇlaravelÔõô×Ô¶¯ÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡