×ðÁú¿­Ê±

ÏêϸÏÈÈÝ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-&gt;execute($credentials);
        } catch (\Exception $e) {
            return response()-&gt;json([
                'message' =&gt; $e-&gt;getMessage()
            ], 401);
        }

        return response()-&gt;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µÄ±àд°ì·¨ºÍʵ¼ùµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿