laravel serviceдʲô
Ëæ×Å laravel µÄÒ»Ö±Éú³¤£¬service£¨Ð§ÀÍ£©³ÉΪÁË laravel ¿ò¼ÜµÄÒ»¸öÖ÷Òª×é¼þ¡£service µÄ×÷ÓÃÊÇʲô£¿ÔÚ laravel ÖÐÔõÑùʹÓà service£¿ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏêϸÏÈÈÝ laravel service µÄÏà¹Ø֪ʶ¡£
Ò»¡¢Service µÄ×÷ÓÃ
ЧÀÍÊÇÖ¸ÔÚÏîÄ¿ÖÐʹÓõÄÓªÒµÂß¼£¬ºÃ±ÈһЩ¸úÖ§¸¶¡¢Ô±¹¤ÖÎÀí¡¢Êý¾ÝÆÊÎöµÈÏà¹ØµÄÓªÒµÂß¼¡£ÔÚ Laravel ¿ò¼ÜÖУ¬Service Ö÷ÒªÊÇΪÁË´¦ÀíһЩ·±ËöµÄÓªÒµÂß¼£¬²¢½«Æä·â×°³ÉÒ»¸ö¿É¸´ÓõÄ×é¼þ£¬ÒÔ±ãÓÚÔÚ¶à¸ö Controller¡¢Repository¡¢Service Öб»Å²Óã¬Ìá¸ß´úÂ븴ÓÃÂÊ£¬ïÔÌ´úÂëÈßÓà¡£
ͨ³£ÇéÐÎÏ£¬Service ºÍ Repository ¶¼ÊÇΪÁËʵÏÖÊý¾ÝµÄһϵÁÐÓªÒµ²Ù×÷£¬¿ÉÊÇ Service »¹Òª½øÒ»²½·â×°£¬ÊµÏÖÓªÒµµÄ¸ß¶ÈÁýͳºÍ¼ò»¯¡£Service ½ÓÊÜ¿Í»§¶ËµÄÇëÇ󣬴¦ÀíÓªÒµÂß¼ºó£¬·µ»ØЧ¹û»òÅ׳öÒì³£¡£
²î±ðÓÚ Repository£¬Service ·â×°ÁËÐí¶àµÄÓªÒµÂß¼£¬²¢ÇÒ¹ØÓÚ²î±ðµÄÓªÒµ¹¦Ð§£¬¿ÉÒÔ½¨Éè¶à¸ö²î±ðµÄ Service À࣬ÒÔ±ãÓÚ´úÂëµÄÂß¼»®·Ö¡£
¶þ¡¢Service µÄʵÏÖ·½·¨
ʹÓà Service ҪעÖØÁ½¸ö·½Ã棺½¨Éè Service ÀàÒÔ¼°ÔÚÐèÒªµÄµØ·½Å²Óà Service Àà¡£ÏÂÃ潫ÓôúÂë¾ÙÐÐÏêϸ˵Ã÷¡£
½¨Éè Service
½¨Éè Service ʱ£¬ÐèҪ˼Á¿µ½ Service µÄ¹¦Ð§ºÍŲÓ÷½¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬ÔÚ Service ÖÐʵÏÖһЩ»ù´¡µÄÔöɾ¸Ä²é²Ù×÷£¬ÒÔ¼°ÆäËûµÄһЩ¸üÏêϸµÄÓªÒµÂß¼¡£
ÏÂÃæÒÔÒ»¸öÓû§×¢²áµÄ Service ΪÀý£¬Õ¹Ê¾ Service µÄͨÀýʵÏÖ·½·¨£º
<?php namespace AppServices; use AppRepositoriesUserRepository; class RegisterService { protected $userRepository; /** * RegisterService constructor. * * @param UserRepository $userRepository */ public function __construct(UserRepository $userRepository) { $this->userRepository = $userRepository; } /** * Óû§×¢²á. * * @param array $data * * @return mixed */ public function register(array $data) { // TODO: ½øÒ»²½ÑéÖ¤ $this->userRepository->create($data); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬Ê×ÏÈÃüÃû¿Õ¼ä AppServices£¬Service µÄÀàÃûΪ RegisterService¡£½á¹¹ÒªÁìÖÐÐèҪעÈë UserRepository ¹¤¾ß¡£ÔÚ Service ÖУ¬ÎÒÃÇʵÏÖÁËÓû§×¢²áµÄÂß¼£¬²¢ÇÒʹÓÃÁË UserRepository µÄ create() ÒªÁ죬ÔÚÊý¾Ý¿âÖн¨ÉèÁËÒ»¸öÐÂÓû§¡£
ŲÓà Service
Service ÓÐÁËÖ®ºó£¬»¹ÐèÒªÔÚ Controller¡¢Repository ÕâЩµØ·½Å²Óá£ÏÂÃæÒÔÔÚ Controller ÖÐŲÓà RegisterService ΪÀý£¬Õ¹Ê¾Å²ÓôúÂ룺
<?php namespace AppHttpControllers; use AppHttpRequestsRegisterRequest; use AppServicesRegisterService; class RegisterController extends Controller { public function register(RegisterRequest $request, RegisterService $registerService) { // TODO: ŲÓà Service ¾ÙÐÐ×¢²á $registerService->register($request->validated()); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬Ê¹Óà RegisterService ʱÐèÒªÔÚÆä½á¹¹º¯ÊýÖд«Èë UserRepository ¹¤¾ß£¬²¢ÇÒÔÚŲÓÃʱ£¬ÐèÒªÔÚ RegisterController µÄÒªÁì²ÎÊýÖÐ×¢Èë RegisterService ¹¤¾ß¡£
Èý¡¢Service ÖÐÆäËûÐèҪעÖصÄÎÊÌâ
ÔÚ Laravel Õë¶Ô Service µÄʹÓû¹ÐèҪעÖØÒÔÏÂÎÊÌ⣺
Service Ö»ÊÇÒ»¸öÀ࣬¿ÉÒÔÒÀÀµÓÚÆäËû Service¡¢Repository¡¢»òÕßÆäËû Class£¬Ê¹ÓÃʱÈÔÐèÒªÔڽṹº¯ÊýÖÐ×¢ÈëÒÀÀµ¡£
Service ÐèҪʵÏÖÏêϸµÄÓªÒµÂß¼£¬ÕâÒ²¾ÍÒªÇó Service ÐèÒª¼á³Ö¾«Á·¡¢ÇáÁ¿¡£ÈôÊÇ Service ʵÏÖµÄÓªÒµ¹ýÓÚÅÓÔÓ£¬ÐèҪ˼Á¿²ð·Ö Service¡£Ò»Ñùƽ³£½¨Ò飬ÿ¸ö Service ֻʵÏÖ¼òµ¥¹¦Ð§µÄÓªÒµ¡£
Service Öв»Òª°üÀ¨ÊÓͼ²ãÃæµÄÂß¼´¦Àí£¬Service ÖеÄÂß¼Ó¦¸ÃÓë¿ØÖÆÆ÷ºÍÆäËû±»Å²ÓõÄЧÀÍÂß¼Î޹ء£
ËÄ¡¢½áÓï
Service ×÷Ϊ Laravel µÄÖ÷Òª×é¼þÖ®Ò»£¬Æä×÷ΪӪҵÂß¼×é¼þÓÅÊÆÊ®Ã÷È·ÏÔ¡£ÔÚ Laravel 5.8 / 6 °æ±¾Ö®ºó£¬Laravel ¹Ù·½ÒýÈëÁ˸üΪÀû±ãµÄ Service ×Ô¶¯×¢²á»úÖÆ£¬¿ÉÒÔ¸üÇáËɵؼ¯³É Service¡£¹ØÓÚÈÕÒæÖØ´óµÄÓªÒµÂß¼£¬Service ×éÖ¯´úÂëÔ½·¢ÇåÎúÒ׶®£¬¼ò»¯´úÂëÖØƯºó£¬Ò²¾ßÓнϺõĿÉÀ©Õ¹ÐԺͿÉά»¤ÐÔ¡£
ÒÔÉϾÍÊÇlaravel serviceдʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡