尊龙凯时人生就是搏

ÏêϸÏÈÈÝ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¡¢ÍøÕ¾µØͼ