ÏêϸÏÈÈÝLaravel ActionµÄ±àд°ì·¨ºÍʵ¼ù
laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ÆäÒÀ¸½Æ侫Á·¶øÇ¿Ê¢µÄapiºÍÖÜÈ«µÄ¹¤¾ß°ü¶ø±¸ÊܽӴý¡£ÔÚlaravelÖУ¬¡°actions¡±ÊÇÒ»ÖÖ¿´·¨£¬Ëü¿ÉÒÔÈÃÄãÔ½·¢ÇáËɵع¹½¨¿ØÖÆÆ÷ºÍ¼ò»¯ÄãµÄÂß¼²ã¡£ÏÂÃæÊÇÏêϸÏÈÈÝlaravel actionÔõÑù±àдµÄ°ì·¨ºÍʵ¼ù¡£
½¨ÉèActionÀà
Ê×ÏÈ£¬ÄãÐèÒª½¨ÉèÒ»¸öActionÀà¡£ÔÚLaravel 8¼°ÒÔÉÏ°æ±¾ÖУ¬¿ÉÒÔͨ¹ýArtisanÏÂÁîÀ´½¨É裺
php artisan make:action MyAction
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚapp/ActionĿ¼ÏÂΪÄ㽨ÉèÒ»¸öеÄÀàÎļþ¡°MyAction.php¡±¡£ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ×îÏȱàдActionµÄÂß¼ÁË¡£
±àдActionµÄÂß¼
ÔÚÄãµÄActionÖУ¬Äã¿ÉÒÔΪ¿ØÖÆÆ÷ÖеÄÿ¸öÒªÁì½ç˵ÏêϸµÄÓªÒµÂß¼¡£ÔÚÕâÀÎÒÃǽ«½¨ÉèÒ»¸öÒªÁìÀ´´¦Öóͷ£Óû§µÇ¼µÄÂß¼¡£
<?php namespace App\Actions; use Illuminate\Support\Facades\Auth; class LoginUserAction { public function execute(array $credentials) { $attempt = Auth::attempt($credentials); if (!$attempt) { throw new \Exception('Invalid login credentials'); } return Auth::user(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪLoginUserActionµÄAction¡£ËüÎüÊÕÒ»¸ö°üÀ¨Óû§µÇ¼ƾ֤µÄÊý×飬ִÐеǼʵÑ飬ÈôÊÇÓйýʧÔò»áÅ׳öÒì³££¬²»È»·µ»ØÈÏÖ¤ÀֳɵÄÓû§¡£
ÖµµÃ×¢ÖصÄÊÇ£¬ÎÒÃÇʹÓÃLaravelµÄAuthÃÅÃæÀ´Ö´ÐеǼ²Ù×÷¡£ÕâÔÊÐíÎÒÃÇÇáËɵØʹÓÃLaravelµÄÈÏ֤ϵͳ¡£
ÔÚ¿ØÖÆÆ÷ÖÐʹÓÃAction
ÏÖÔÚ£¬ÎÒÃÇÒѾ½¨ÉèÁËÒ»¸öActionÀಢ½ç˵ÁËÆäÂß¼¡£ÏÂÒ»²½ÊÇÔÚ¿ØÖÆÆ÷ÖÐʹÓÃËü¡£
ʹÓÃActionµÄµÚÒ»²½ÊÇ·¿ª¿ØÖÆÆ÷²¢Ìí¼ÓÒ»¸öuseÓï¾ä£¬ÒÔÒýÈëActionÀࣺ
<?php namespace App\Http\Controllers; use App\Actions\LoginUserAction; use Illuminate\Http\Request; class AuthController extends Controller { public function login(Request $request, LoginUserAction $loginAction) { $credentials = $request->only(['email', 'password']); try { $user = $loginAction->execute($credentials); } catch (\Exception $e) { return response()->json([ 'message' => $e->getMessage() ], 401); } return response()->json(compact('user')); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇ´ÓÎÒÃÇ×Ô¼ºµÄÃüÃû¿Õ¼äÖÐÒýÓÃÁË LoginUserAction¡£È»ºóÎÒÃÇÌí¼ÓÁË login() ÒªÁ죬ÆäÊ׸ö²ÎÊýÊÇ Laravel µÄ Request ¹¤¾ß£¬µÚ¶þ¸ö²ÎÊýÊÇÎÒÃÇÉÏÃ潨ÉèµÄ LoginUserAction ʵÀý¡£
ÔÚ login() ÖУ¬ÎÒÃÇÊ×ÏÈҲƾ֤ÇëÇóµÄÄÚÈÝÌáÈ¡³ö email ºÍ password£¬½Ó×ÅŲÓÃ×ðÁú¿Ê± Action ͨ¹ý $credentials µÄ²ÎÊýÀ´Ö´ÐÐÂß¼¡£ÈôÊÇÀֳɣ¬ÎÒÃǽ«Óû§ÒÔ JSON ÃûÌ÷µ»Ø£¬²»È»ÎÒÃǽ«¹ýʧÐÅÏ¢·µ»Ø¸ø¿Í»§¶Ë¡£
ŲÓÃAction
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ»á¼û×ðÁú¿Ê±login()ÒªÁ죬²¢Å²ÓÃLoginUserActionÀàÖеÄexecuteÒªÁì¡£ÎÒÃǽ«ÔÚHTTPÇëÇóÖÐת´ïÒ»¸öÊý×飬°üÀ¨ÁËһЩµÇ¼ƾ֤ÐÅÏ¢£¬²¢·µ»ØЧ¹û¡£
ÔÚ·ÓÉÖнç˵¶ÔÓ¦µÄµÇ¼·ÓÉ£º
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\AuthController; Route::post('/login', [AuthController::class, 'login'])->name('login');
µÇ¼ºó¸´ÖÆ
ÏÖÔÚÎÒÃÇ¿ÉÒÔ»á¼û×ðÁú¿Ê±laravelÓ¦ÓõÄ/login·ÓÉ£¬»áÏñÕâÑù£º
POST /login HTTP/1.1 Host: localhost:8000 Content-Type: application/json { "email": "user@example.com", "password": "my_password" }
µÇ¼ºó¸´ÖÆ
Õ⽫´¥·¢ÎÒÃÇÔÚ¿ØÖÆÆ÷Öнç˵µÄ login() ÒªÁ죬ŲÓà LoginUserAction£¬´«Èë email ºÍ password ×÷Ϊ²ÎÊý¡£
×ܽá
Laravel Action ÌṩÁËÒ»ÖÖ¼òÆÓ¡¢Çå½àµÄÒªÁìÀ´×éÖ¯ÄãµÄÓªÒµÂß¼¡£Í¨¹ý½«ÓªÒµÂß¼´Ó¿ØÖÆÆ÷ÖÐÒƳö²¢·ÅÈë Action ÀàÖУ¬Äã¿ÉÒÔ´øÀ´Ðí¶à´úÂëÖØÓúͿÉά»¤ÐÔµÄÌáÉý¡£
ÔÚʹÓà Laravel Action ʱ£¬ÄãÓ¦¸Ã×ñÕÕÒÔÏÂÁ½Ìõ×î¼Ñʵ¼ù£º
ÿ¸öÒªÁìÓ¦¸ÃÖ»¹ÜÖ»×öÒ»¼þÊÂÇé¡£
×èÖ¹ÔÚ Action ÀàÖÐÖ±½Ó»á¼ûÍⲿÀà»ò¹¤¾ß£¬Ê¹ÓÃת´ï±äÁ¿µÄ·½·¨À´ÊµÏÖÊý¾ÝµÄ¹²Í¨ºÍÖظ´µÄ´¦Öóͷ£¡£
×îºó£¬ÐèҪעÖصÄÊÇ£¬ÔÚ Laravel 8 ÖÐÒýÈëÁË Action ¹¦Ð§£¬ÈôÊÇÄãʹÓõÄÊǽÏÀÏ°æ±¾µÄ Laravel£¬¿ÉÄÜÐèÒªÊÖ¶¯½¨Éè Action À࣬²¢½«ÀàÎļþ´æ´¢ÔÚÒ»¸öºÏÊʵÄλÖã¬È»ºóÔÚ¿ØÖÆÆ÷ÖÐÊÖ¶¯ÊµÀý»¯ºÍŲÓá£
ÒÔÉϾÍÊÇÏêϸÏÈÈÝLaravel ActionµÄ±àд°ì·¨ºÍʵ¼ùµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡