laravelÔõô½¨ÉèЧÀÍÈÝÆ÷
laravelÊÇÒ»¸öÊ¢ÐеÄphp web¿ª·¢¿ò¼Ü£¬ÆäЧÀÍÈÝÆ÷ÊÇÆä×îÖ÷ÒªµÄ¿´·¨Ö®Ò»¡£Ð§ÀÍÈÝÆ÷ʹµÃlaravelÓ¦ÓóÌÐò¸üÒ×ÓÚÀ©Õ¹ºÍά»¤¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖн¨ÉèºÍʹÓÃЧÀÍÈÝÆ÷£¬ÒÔ¼°ÔõÑùÔÚ´úÂëÖÐʹÓÃËüÃÇ¡£
ЧÀÍÈÝÆ÷¼ò½é
ЧÀÍÈÝÆ÷ÊÇLaravelµÄ½¹µã×é¼þÖ®Ò»£¬ËüÈÏÕæÖÎÀíÓ¦ÓóÌÐòÖÐËùÓÐÀàµÄÒÀÀµ¹ØϵºÍʵÀý»¯¡£ÔÚLaravelÖУ¬Ð§ÀÍÈÝÆ÷ʹÓ÷´Ïò¿ØÖÆÈÝÆ÷ģʽÀ´ÖÎÀí¹¤¾ßºÍËüÃÇÖ®¼äµÄÒÀÀµ¹Øϵ¡£µ±Ó¦ÓóÌÐòÇëÇóij¸öÀàʱ£¬Ð§ÀÍÈÝÆ÷»á¼ì²éÕâ¸öÀàµÄ½á¹¹º¯Êý£¬²¢Æ¾Ö¤ÕâЩҪÇóÀ´¼ÓÔØËùÐèµÄ²Ù×÷¹¤¾ß¡£
ЧÀÍÈÝÆ÷ÏÕЩ¿ÉÒÔÊÂÇéÓÚÈκι¤¾ß£¬°üÀ¨¿ØÖÆÆ÷¡¢Ä£×Ó¡¢ÊÓͼ¡¢ÇëÇóºÍÏìÓ¦µÈµÈ¡£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬Ð§ÀÍÈÝÆ÷¿ÉÒÔʹÓÃÒÀÀµ×¢ÈëÀ´½â¾öÀàÖ®¼äµÄÒÀÀµ¹Øϵ¡£µ±ÄãÐèҪʹÓÃÒ»¸öÀàÀ´´¦Öóͷ£Ä³¸öʹÃüµÄʱ¼ä£¬Ð§ÀÍÈÝÆ÷»á×Ô¶¯µØ¼ì²éÕâ¸öÀàµÄ½á¹¹º¯Êý£¬²¢×Ô¶¯½¨ÉèËüËùÐèÒªµÄÒÀÀµÏî¡£
ΪʲôҪʹÓÃЧÀÍÈÝÆ÷£¿
ЧÀÍÈÝÆ÷ÔÚÒ»¸öÏÖ´úµÄÓ¦ÓóÌÐòÖÐÊǺÜÊÇÖ÷ÒªµÄ£¬ÓÉÓÚËüÄܹ»ÈÃÄãÔ½·¢ÇáËɵØÖÎÀíÀàÖ®¼äµÄÒÀÀµ¹Øϵ£¬Ìá¸ß´úÂëµÄÎÞаÐԺͿÉά»¤ÐÔ¡£ÈôÊÇÄãµÄÓ¦ÓóÌÐòÖб£´æ¶à¸öÀ࣬²¢ÇÒÆäÖÐһЩÀàÒÀÀµÓÚÆäËûÀ࣬ÄÇôЧÀÍÈÝÆ÷¿ÉÒÔ×Ô¶¯½â¾öÕâÖÖÒÀÀµ¹Øϵ£¬´Ó¶øʹÄúµÄ´úÂëÔ½·¢Ä£¿é»¯ºÍ¿É²âÊÔ¡£
ÔÚûÓÐЧÀÍÈÝÆ÷µÄÇéÐÎÏ£¬Äã¿ÉÄÜÐèÒªÔÚһЩµØ·½ÊÖ¶¯ÊµÀý»¯ÀàºÍ¹¤¾ß£¬²¢½«ËüÃÇת´ï¸øÐèÒªÒÀÀµµÄÆäËûÀà¡£ÕâÑù×öµÄÎÊÌâÊÇ£¬Äã×îÖջᷢÃ÷ÄãµÄ´úÂë±äµÃÏ൱ËÉÉ¢£¬²¢ÇÒÄÑÒÔά»¤ºÍ²âÊÔ¡£Ð§ÀÍÈÝÆ÷¿ÉÒÔ×ÊÖúÄã×èÖ¹ÕâÖÖÇéÐΡ£
ЧÀÍÈÝÆ÷µÄ½¨Éè
ÔÚLaravelÖУ¬½¨ÉèЧÀÍÈÝÆ÷ÊÇÒ»¸öÏà¶Ô¼òÆÓµÄÀú³Ì¡£Ä¬ÈÏÇéÐÎÏ£¬LaravelÓ¦ÓóÌÐòÖеÄЧÀÍÈÝÆ÷ÊÇÒѾԤÏÈÉèÖúõġ£µ±Ä㽨ÉèÒ»¸öеÄLaravelÓ¦ÓóÌÐòʱ£¬Äã¿ÉÒÔÏòLaravel¿ò¼ÜµÄЧÀÍÈÝÆ÷Ìí¼Ó×Ô¼ºµÄЧÀÍÌṩ³ÌÐò¡£
ЧÀÍÌṩÕßÊÇÒ»ÖÖÌØÊâÀàÐ͵ÄÀ࣬ËüÏòЧÀÍÈÝÆ÷ÖÐÌí¼ÓÒ»¸ö»ò¶à¸öЧÀÍ¡£Ã¿¸öЧÀÍÌṩ³ÌÐòÓÐÁ½¸ö±ØÐèʵÏÖµÄÒªÁ죺register()ºÍboot()¡£
register()ÒªÁìÓÃÓÚÉèÖÃЧÀÍÈÝÆ÷¡£ÔÚÕâ¸öÒªÁìÖУ¬Äã¿ÉÒÔ½«Laravel×¢²áµÄЧÀÍ»òÄã×Ô¼ºµÄЧÀÍ£¬È»ºóÔÚÓ¦ÓóÌÐòµÄÆäËûµØ·½Ê¹ÓÃËüÃÇ¡£
boot()ÒªÁìÊÇ¿ÉÑ¡µÄ¡£ÔÚÕâ¸öÒªÁìÖУ¬Äú¿ÉÒÔ¶ÔLaravelÓ¦ÓóÌÐò¾ÙÐÐÈ«¾ÖÉèÖã¬ÀýÈç×¢²á·ÓÉ£¬×¢²áÊÂÎñºÍ¼àÌýÆ÷µÈµÈ¡£
ÏÂÃæÎÒÃÇÀ´¿´Ò»¸ö¼òÆÓµÄÀý×Ó¡£¼ÙÉèÎÒÃÇÓÐÒ»¸öÃûΪUserServiceµÄЧÀÍÀ࣬ËüÒÀÀµÓÚÒ»¸öÃûΪUserRepositoryµÄ´æ´¢¿âÀà¡£ÎÒÃÇÐèÒª½«UserServiceºÍUserRepository×¢²áµ½LaravelЧÀÍÈÝÆ÷ÖУ¬²¢Ê¹ÓÃÒÀÀµ×¢ÈëÔÚ×ðÁú¿Ê±´úÂëÖÐʹÓÃËüÃÇ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪΪUserServiceºÍUserRepository½¨ÉèÒ»¸öЧÀÍÌṩÕßÀà¡£ÔÚapp/ProvidersĿ¼Ï½¨ÉèÒ»¸öÃûΪUserServiceServiceProviderµÄÀ࣬¸ÃÀà¼ÌÐøServiceProvide¡£
namespace AppProviders;
use IlluminateSupportServiceProvider;
use AppRepositoriesUserRepository;
use AppServicesUserService;
Class UserServiceServiceProvider extends ServiceProvider
{
public function register() { $this->app->bind(UserService::class, function ($app) { return new UserService($app->make(UserRepository::class)); }); $this->app->bind(UserRepository::class, function ($app) { return new UserRepository(); }); }
µÇ¼ºó¸´ÖÆ
}
½ÓÏÂÀ´£¬½«×ðÁú¿Ê±Ð§ÀÍÌṩÕßÌí¼Óµ½config/app.phpÎļþµÄprovidersÊý×éÖС£
‘providers’ => [
AppProvidersUserServiceServiceProvider::class,
µÇ¼ºó¸´ÖÆ
]
ºÃÁË£¬Ð§ÀÍÌṩÆ÷ÒѾÍê³ÉÁË¡£ÏÖÔÚÎÒÃÇ¿ÉÒÔÔÚ×ðÁú¿Ê±´úÂëÖÐʹÓÃUserServiceºÍUserRepository¡£ÀýÈ磬ÔÚ¿ØÖÆÆ÷ÖÐʹÓÃUserServiceÀ´»ñÈ¡Óû§Êý¾Ý£º
namespace AppHttpControllers;
use AppServicesUserService;
class UserController extends Controller
{
protected $userService; public function __construct(UserService $userService) { $this->userService = $userService; } public function index() { $users = $this->userService->getAllUsers(); return view('users.index', compact('users')); }
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ«UserService×¢Èëµ½ÁËUserControllerÖУ¬²¢ÔÚÆäindex()ÒªÁìÖУ¬Í¨¹ýUserService»ñÈ¡ÁËËùÓÐÓû§Êý¾Ý£¬²¢½«Æäת´ï¸øÒ»¸öÊÓͼ¡£
×ܽá
ÔÚÏÖ´úµÄÓ¦ÓóÌÐòÖУ¬Ð§ÀÍÈÝÆ÷ÊÇÒ»¸ö±Ø²»¿ÉÉٵĹ¦Ð§¡£Ëü¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµÄÖÎÀíÀàÖ®¼äµÄÒÀÀµ¹Øϵ£¬Ìá¸ß´úÂëÎÞаÐԺͿÉά»¤ÐÔ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÌÖÂÛÁËÔõÑùÔÚLaravelÓ¦ÓóÌÐòÖн¨ÉèºÍʹÓÃЧÀÍÈÝÆ÷¡£Í¬Ê±£¬ÎÒÏ£ÍûÄú¿ÉÒÔÃ÷ȷЧÀÍÈÝÆ÷¼°ÆäÔÚLaravel¿ò¼ÜÖеÄʹÓÃÒªÁ죬ÒÔ±ãÄãÔÚÓ¦ÓóÌÐòµÄ¹¹½¨Àú³ÌÖÐÔ½·¢¼ÝÇá¾ÍÊìºÍÌñ¾²¡£
ÒÔÉϾÍÊÇlaravelÔõô½¨ÉèЧÀÍÈÝÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡