尊龙凯时人生就是博

laravelÔõÑùʵÏֵǼºóÌøת¹¦Ð§

laravel ÊÇÏÖÔÚʹÓÃ×îÆÕ±éµÄ php web ¿ò¼Ü£¬ËüµÄµÇ¼¹¦Ð§·Ç¾­³£Ó㬶øµÇ¼ÀֳɺóÌøתµ½Ö¸¶¨Ò³ÃæÒ²Êdz£¼ûÐèÇóÖ®Ò»¡£±¾ÎĽ«ÏêϸÏÈÈÝ laravel ÖÐÔõÑùʵÏֵǼºóÌøת¹¦Ð§¡£

1. ×°ÖÃ Laravel

ÈôÊÇÄú»¹Ã»ÓÐ×°Öà Laravel£¬¿ÉÒÔƾ֤ÒÔÏ°취¾ÙÐÐ×°Öãº

×°Öà Laravel ×°ÖÃÆ÷£ºcomposer global require laravel/installer

½¨ÉèÐÂÏîÄ¿£ºlaravel new project_name

½øÈëÏîĿĿ¼£ºcd project_name

Æô¶¯ Laravel ÄÚÖÃЧÀÍÆ÷£ºphp artisan serve

ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ê¹ÓÃĬÈÏÉèÖþÙÐпª·¢£¬ÈôÊÇÄúʹÓÃÁËÆäËûÉèÖ㬿ÉÄÜ»áÉÔÓвî±ð¡£

2. ʵÏֵǼ¹¦Ð§

ÔÚ Laravel ÖÐʵÏֵǼ¹¦Ð§ºÜÊǼòÆÓ£¬Ö»ÐèҪʹÓà Auth ÖÐÐļþ¼´¿É¡£ÔÚ routes/web.php ÎļþÖÐÔöÌíÒÔÏ·ÓÉ£º

Route::get('/login', 'LoginController@showLoginForm')->name('login');
Route::post('/login', 'LoginController@login');

µÇ¼ºó¸´ÖÆ

ÕâÁ½¸ö·ÓÉ»®·Ö¶ÔÓ¦µÇ¼ҳÃæµÄÏÔʾºÍµÇ¼²Ù×÷µÄ´¦Àí¡£ÆäÖУ¬LoginController@showLoginForm ÒªÁìÓÃÓÚÏÔʾµÇ¼±íµ¥£¬LoginController@login ÒªÁìÓÃÓÚ´¦ÀíÓû§Ìá½»µÄµÇ¼ÇëÇó¡£

ÈôÊÇÄú»¹Ã»Óн¨Éè LoginController£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¿ìËÙ½¨É裺

php artisan make:controller LoginController

µÇ¼ºó¸´ÖÆ

ÔÚ LoginController ÖУ¬¿ÉÒÔʹÓà Laravel ÌṩµÄ AuthenticatesUsers trait ¿ìËÙʵÏֵǼ¹¦Ð§¡£Ö»ÐèÒªÔÚÀàÖÐÒýÈë´Ë trait ¼´¿É£º

use AuthenticatesUsers;

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

µÇ¼ºó¸´ÖÆ

ĬÈÏÇéÐÎÏ£¬µÇ¼Àֳɺó»áÌøתµ½ /home Ò³Ã棬¿ÉÒÔÔÚ Auth\LoginController ÖÐÐÞ¸Ä $redirectTo ÊôÐÔÀ´Ö¸¶¨Óû§µÇ¼ÀֳɺóÌøתµÄÒ³Ãæ¡£

protected $redirectTo = '/dashboard';//ÐÞ¸ÄΪÄãÒªÌøתµÄ·¾¶

µÇ¼ºó¸´ÖÆ

3. ʵÏֵǼºóÌøת

ÔÚ Laravel ÖÐʵÏֵǼºóÌøתҲºÜÊǼòÆÓ£¬Ö»ÐèÒªÔڵǼ¿ØÖÆÆ÷ÖÐÐÞ¸Ä $redirectTo ÊôÐÔ¼´¿É¡£¿ÉÊÇ£¬ÈôÊÇÄúÏ£ÍûµÇ¼ÀֳɺóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ã棬ÄÇôÐèÒª¾ÙÐÐÒÔϲÙ×÷£º

½¨Éè RedirectIfAuthenticated ÖÐÐļþ

ÔÚ app/Http/Middleware Ŀ¼Ï½¨Éè RedirectIfAuthenticated.php Îļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class RedirectIfAuthenticated
{
    public function handle(Request $request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect()-&gt;intended('/dashboard');//ÐÞ¸ÄΪµÇ¼ÀֳɺóÒªÌøתµÄ·¾¶
        }

        return $next($request);
    }
}

µÇ¼ºó¸´ÖÆ

´úÂëÖÐµÄ intended ÒªÌå»áʵÑ齫Óû§Öض¨Ïòµ½ËûÃÇ×î³õÇëÇóÒ³ÃæµÄλÖã¬ÈôÊÇûÓÐÏÈÇ°µÄλÖã¬ÔòĬÈÏÖض¨Ïòµ½Ö¸¶¨µÄ·¾¶¡£

ÔÚ app/Http/Kernel.php ÖÐ×¢²áÖÐÐļþ

ÔÚ app/Http/Kernel.php ÎļþÖÐµÄ $routeMiddleware Êý×éÖÐ×¢²áÐÂÔöµÄÖÐÐļþ£º

protected $routeMiddleware = [
    // ... ÆäËûÖÐÐļþÊ¡ÂÔ
    'guest' =&gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
];

µÇ¼ºó¸´ÖÆ

Ð޸ĵǼ¿ØÖÆÆ÷

ÔڵǼ¿ØÖÆÆ÷ÖУ¬½«µÇ¼ÀֳɺóÖض¨ÏòµÄÄ¿µÄµØµãÐÞ¸ÄΪ intended ÒªÁ죬¼´¿ÉʵÏֵǼºóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ã棺

protected $redirectTo = '/dashboard';//ɾ³ýÕâÐÐ

µÇ¼ºó¸´ÖÆ

public function login(Request $request)
{
    $this-&gt;validateLogin($request);

    if ($this-&gt;attemptLogin($request)) {
        // µÇ¼ÀֳɺóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ãæ
        return redirect()-&gt;intended('/dashboard');
    }

    $this-&gt;incrementLoginAttempts($request);

    return $this-&gt;sendFailedLoginResponse($request);
}

µÇ¼ºó¸´ÖÆ

4. ×ܽá

ÔÚ±¾ÎÄÖУ¬ÎÒÃÇѧϰÁËÔÚ Laravel ÖÐʵÏֵǼºóÌøתÐèÒª¾ÙÐÐÄÄЩ²Ù×÷¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪʹÓà Auth ÖÐÐļþʵÏֵǼ¹¦Ð§¡£È»ºó£¬ÎÒÃÇͨ¹ýÐ޸ĵǼ¿ØÖÆÆ÷ÖÐµÄ $redirectTo ÊôÐÔÀ´ÊµÏֵǼ֮ºóµÄÌøת¡£×îºó£¬ÎÒÃÇͨ¹ý½¨ÉèÖÐÐļþºÍÐ޸ĵǼ¿ØÖÆÆ÷ÖеĴúÂëÀ´ÊµÏֵǼºóÌøתµ½Óû§Ö®Ç°»á¼ûµÄÒ³Ãæ¡£

Ï£Íû±¾ÎÄÄܹ»×ÊÖúÄúÇáËÉʵÏÖ Laravel ÖеĵǼºóÌøת¹¦Ð§£¡

ÒÔÉϾÍÊÇlaravelÔõÑùʵÏֵǼºóÌøת¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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