尊龙凯时人生就是搏

laravel ʵÏÖapp¶ËµÇ¼

Ëæ×ÅÖÇÄÜÊÖ»úÓ¦ÓÃÒ»Ö±ÔöÌí £¬Òƶ¯Ó¦Óÿª·¢µÄÐèÇóÈÕÒæÔöÌí £¬Óû§µÄµÇ¼ÒѾ­³ÉΪÒƶ¯Ó¦ÓÃÖкÜÊÇÖ÷ÒªµÄ¹¦Ð§Ö®Ò» £¬ÓÉÓÚÓû§µÄµÇ¼²»µ«¿ÉÒÔ±£»¤Óû§ÐÅÏ¢µÄÇå¾² £¬»¹¿ÉÒÔÀû±ãÓû§ÔÚ²î±ð×°±¸ÉÏ¿ìËٵĻá¼ûËûÃǵÄÐÅÏ¢¡£laravel ÊÇÒ»¿îÊ¢ÐÐµÄ php ¿ò¼Ü £¬ËüÌṩÁËÐí¶àÄÚÖõĹ¦Ð§ºÍÀ©Õ¹°üÀ´×ÊÖú¿ª·¢ÕßʵÏÖÓ¦ÓõÄÖÖÖÖ¹¦Ð§ £¬°üÀ¨ app ¶ËµÄµÇ¼¹¦Ð§¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓà laravel ʵÏÖ app ¶ËµÇ¼¹¦Ð§¡£

Ò». Laravel µÄ×°ÖúÍÉèÖÃ

ÔÚ×îÏÈʵÏÖ Laravel ÖÐµÄ APP ¶ËµÇ¼¹¦Ð§Ö®Ç° £¬ÐèҪװÖúÍÉèÖà Laravel ÇéÐΡ£Ê×ÏÈÐèÈ·±£ÒѾ­×°Öà Composer ¹¤¾ß£¨https://getcomposer.org/£©¡£½ÓÏÂÀ´ £¬ÔËÐÐÒÔÏÂÏÂÁîÔÚÄãµÄÅÌËã»úÖÐ×°Öà Laravel¡£

composer create-project --prefer-dist laravel/laravel myapp

µÇ¼ºó¸´ÖÆ

ÆäÖÐ £¬¡°myapp¡±ÊÇÄãÏëÒª½¨ÉèµÄ Laravel Ó¦ÓóÌÐòµÄÃû³Æ¡£

×°ÖÃÍê³Éºó £¬¿ÉÒÔ½øÈë myapp Ŀ¼²¢ÔËÐÐÒÔÏÂÏÂÁîÀ´Æô¶¯Ò»¸öÍâµØ¿ª·¢Ð§ÀÍÆ÷¡£

php artisan serve

µÇ¼ºó¸´ÖÆ

»á¼û http://localhost:8000 ¿ÉÒÔ¿´µ½ Laravel µÄ½Ó´ýÒ³Ãæ¡£½Ó×Å £¬ÐèҪװÖúÍÉèÖà Passport À©Õ¹°ü £¬ÒÔÖ§³Ö APP ¶ËµÄµÇ¼¹¦Ð§¡£

ÔËÐÐÒÔÏÂÏÂÁîÀ´×°Öà Passport À©Õ¹°ü£º

composer require laravel/passport

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê³Éºó £¬ÔËÐÐÒÔÏÂÏÂÁîÀ´ÌìÉúÐëÒªµÄ Passport ÃÜÔ¿£º

php artisan passport:keys

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´ £¬ÔËÐÐÒÔÏÂÏÂÁîÀ´ÌìÉú Passport µÄÊý¾Ý¿âǨá㣺

php artisan migrate

µÇ¼ºó¸´ÖÆ

¶þ. ʵÏÖ APP ¶ËµÇ¼¹¦Ð§

½¨ÉèÓû§Ä£×ӺͿØÖÆÆ÷

Ê×ÏÈ £¬ÐèÒª½¨ÉèÒ»¸öÓû§Ä£×Ó £¬ÒÔ±ãÓ¦ÓóÌÐò¿ÉÒÔÖÎÀíÓû§¡£ÔËÐÐÒÔÏÂÏÂÁîÀ´½¨ÉèÃûΪ User µÄÄ£×Ó£º

php artisan make:model User

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´ £¬½¨ÉèÒ»¸öÃûΪ AuthController µÄ¿ØÖÆÆ÷ £¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏ´úÂ룺

<?php

namespace AppHttpControllers;

use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use Validator;

class AuthController extends Controller
{
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $user = new User;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();

        $token = $user->createToken('MyApp')->accessToken;

        return response()->json(['token' => $token], 200);
    }

    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            $user = Auth::user();
            $token = $user->createToken('MyApp')->accessToken;
            return response()->json(['token' => $token], 200);
        } else {
            return response()->json(['error' => 'Unauthorized'], 401);
        }
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖÐ £¬register ÒªÁìÔÊÐíÓû§¾ÙÐÐ×¢²á £¬login ÒªÁìÔÊÐíÓû§¾ÙÐеǼ¡£ÈôÊÇÓû§ÀֳɾÙÐÐ×¢²á»òµÇ¼ £¬Ôò»á·µ»ØÒ»¸ö»á¼ûÁîÅÆ £¬¸ÃÁîÅƽ«ÓÃÓÚ¾ÙÐкóÐøµÄÓû§ÇëÇó¡£

½¨Éè API ·ÓÉ

½ÓÏÂÀ´ £¬ÐèÒª½¨ÉèÏìÓ¦µÄ API ·ÓÉÀ´´¦ÀíÓû§µÄµÇ¼ºÍ×¢²áÇëÇó¡£ÔÚ routes/api.php ÎļþÖÐ £¬Ìí¼ÓÒÔÏ´úÂ룺

<?php

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpControllersAuthController;

Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂ뽫ע²áºÍµÇ¼ÇëÇóÓ³Éäµ½ AuthController ¿ØÖÆÆ÷ÖеÄÏìÓ¦ÒªÁì¡£

Èý. ²âÊԵǼ¹¦Ð§

ÏÖÔÚ £¬ËùÓеĴúÂ붼ÒѾ­×¼±¸ºÃÁË £¬¿ÉÒÔͨ¹ý»á¼ûÒÔÏ url À´²âÊԵǼºÍ×¢²á¹¦Ð§¡£

POST /api/register

×¢²áÐÂÓû§¡£²ÎÊý£ºname¡¢email¡¢password¡£

POST /api/login

¾ÙÐÐÓû§µÇ¼¡£²ÎÊý£ºemail¡¢password¡£

µ±Óû§µÇ¼Àֳɺó £¬»á·µ»ØÒ»¸ö token¡£¿ÉÒÔʹÓøà token À´¾ÙÐкóÐøµÄ API ÇëÇó £¬ÒÔ±ãÑéÖ¤Óû§Éí·Ý¡£

$token = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImZlYTcwMDU3NzhlZWUyMTU3MzliNTM3ODQyNTI0NWJiMGM5YjA3ZWIyMWNlMDhhMDM5ODUyYjczMzVjNWE1NjJlN2UwMGU4ZTQ5MjhhNzg1In0.eyJhdWQiOiIxMSIsImp0aSI6ImZlYTcwMDU3NzhlZWUyMTU3MzliNTM3ODQyNTI0NWJiMGM5YjA3ZWIyMWNlMDhhMDM5ODUyYjczMzVjNWE1NjJlN2UwMGU4ZTQ5MjhhNzg1IiwiaWF0IjoxNjMwMzkwNTM3LCJuYmYiOjE2MzAzOTA1MzcsImV4cCI6MTYzMDM5NDAzNywiZGF0YSI6eyJ1c2VyIjp7ImlkIjoiMSJ9fX0.XsIFsVCc3N1oZ70vzWn4wHQSv3Q-JVdlfu4LJkUPL0he_53bpgJrq5jw8mHygB-1-kpq62N7eG_r08fXKkmbiqeITmUiaGKa0prKzXCoexuYKuxYqG8j-HY2UyDp5g_TE6M4eW5FrcDpTJ0WcQ5ShDhdszUFQUH_qR52ypeEUmsF6CFwx1YJt4AmCFEy2CjsKOMl9K9o0mV_ytvBZ9drY6w6HTRfYfU7q5yuM-W3Qp6bbS-ljzY_2M0RWzwZJm6QvO3Byjhw1TNUrPHFs6xGlY4M7zgkDZxas5srQgpFjKof2LztN7QAJaiT_d6XAKpS24JA2-v2U7x3g';
$response = $this->withHeaders([
    'Authorization' => $token,
])->json('GET', '/api/user');

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖÐ £¬Ê¹Óà token ·¢ËÍÒ»¸ö GET ÇëÇóµ½ /api/user £¬¸ÃÇëÇ󽫷µ»ØÓû§µÄÐÅÏ¢¡£ÈôÊǵǼ²»ÀÖ³É £¬API ½«·µ»Ø 401 ¹ýʧ´úÂë¡£ÈôÊǵǼÀÖ³É £¬Ôò»á·µ»ØÓû§µÄÐÅÏ¢¡£

Laravel ÖÐʹÓà Passport À©Õ¹°üÀ´ÊµÏÖ APP ¶ËµÄµÇ¼¹¦Ð§ºÜÊÇÈÝÒ× £¬Ö»ÐèҪװÖò¢ÉèÖÃÏìÓ¦µÄÀ©Õ¹°ü¼´¿É¡£ÔÚ±¾ÎÄÖÐ £¬ÎÒÃÇÑÝʾÁËÔõÑùʹÓà Laravel Íê³ÉÒ»¸öÍêÕûµÄ APP ¶ËµÄµÇ¼¹¦Ð§ £¬ÊµÏÖÁËÓû§µÄ¿ìËٵǼºÍÇå¾²±£»¤¡£

ÒÔÉϾÍÊÇlaravel ʵÏÖapp¶ËµÇ¼µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

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