尊龙凯时人生就是搏

laravel¿ò¼ÜµÇ¼ע²áÁ÷³Ì

laravel¿ò¼ÜÊÇÒ»ÖÖ»ùÓÚphpÓïÑÔ¿ª·¢µÄÓÅÒì¶øÊܽӴýµÄwebÓ¦ÓóÌÐò¿ª·¢¿ò¼Ü¡£laravel¿ò¼ÜÓкÜÊÇÍêÉƵÄÓû§µÇ¼ºÍ×¢²áϵͳ£¬Õâ¸ö×¢²áºÍµÇ¼ϵͳ¿ÉÒÔʵÏÖÓû§µÄ»ù±¾ÈÏÖ¤¹¦Ð§£¬²¢ÇÒ¿ÉÒÔ¶ÔÓû§¾ÙÐÐһЩÁеÄÊÚȨ²Ù×÷£¬È磺Óû§È¨ÏÞÖÎÀí¡¢ÖØÖÃÃÜÂë¡¢ÓÊÏäÑéÖ¤µÈµÈ¡£ÔÚÏÂÃæµÄÎÄÕÂÖУ¬ÎÒÃǽ«ÏêϸÐðÊölaravel¿ò¼ÜµÄµÇ¼ºÍ×¢²áÁ÷³Ì¡£

Óû§×¢²áÁ÷³Ì

Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÓû§±í£¬ÓÃÓÚÉúÑÄÓû§µÄ»ù±¾ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÌṩµÄ¡°make:model¡±ÏÂÁîÀ´½¨Éè¸ÃÄ£×Ó¡£ÔÚÖÕ¶ËÖÐÊäÈëÈçÏÂÏÂÁ

php artisan make:model User -m

µÇ¼ºó¸´ÖÆ

ÕâÌõÏÂÁ»áÔÚappĿ¼ÏÂÌìÉúÒ»¸öUserÄ£×Ó£¬²¢ÇÒÔÚÊý¾Ý¿âÌìÉúÒ»¸öusers±í¡£ÔÚUserÄ£×ÓÖУ¬ÎÒÃÇÐèÒªÖ¸¶¨Óû§Ê¹ÓõÄÈÏÖ¤·½·¨¡£Laravel¿ò¼ÜÌṩÁ˶àÖÖÓû§ÈÏÖ¤·½·¨£¬ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÖеÄguardÊôÐÔÖоÙÐÐÉèÖãº

protected $guard = 'web';

µÇ¼ºó¸´ÖÆ

½¨ÉèºÃÓû§Ä£×ÓÖ®ºó£¬ÎÒÃÇÐèÒªÔÙ½¨ÉèÒ»¸öÓÃÓÚÕ¹Êլû§×¢ÊéÒ³ÃæµÄ¿ØÖÆÆ÷£¬ÎÒÃÇʹÓÃÒÔÏÂÏÂÁîÀ´½¨Éè¸Ã¿ØÖÆÆ÷£º

php artisan make:controller AuthRegisterController --resource

µÇ¼ºó¸´ÖÆ

ÔËÐÐÍêÕâ¸öÏÂÁîÖ®ºó£¬Laravel¿ò¼Ü½«»áÔÚappHttpControllersAuthĿ¼Ï½¨ÉèÒ»¸öÃûΪRegisterController.phpµÄÎļþ¡£ÔڸÿØÖÆÆ÷ÖУ¬ÎÒÃÇÐèҪʵÏÖÒÔÏÂÒªÁ죺

use AppUser;
use IlluminateFoundationAuthRegistersUsers;
public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return redirect($this->redirectTo);
}

// ÑéÖ¤Óû§ÊäÈëµÄÊý¾Ý
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

// ÉúÑÄ×¢²áÓû§ÐÅÏ¢µ½Êý¾Ý¿â
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÔÚroutes/web.phpÖÐÌí¼Ó¸Ã¿ØÖÆÆ÷µÄ·ÓÉ£º

Route::get('register', 'AuthRegisterController@showRegistrationForm')->name('register');
Route::post('register', 'AuthRegisterController@register');

µÇ¼ºó¸´ÖÆ

ÔÚǰ̨ҳÃæÉÏ£¬ÎÒÃÇʹÓÃÒÔÏÂ±íµ¥À´ÍøÂçÓû§Êý¾Ý£º

<form method="POST" action="{{ route('register') }}">
    @csrf
    <div>
        <label for="name">̞</label>
        <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus>
    </div>

    <div>
        <label for="email">ÓÊÏä</label>
        <input id="email" type="email" name="email" value="{{ old('email') }}" required>
    </div>

    <div>
        <label for="password">ÃÜÂë</label>
        <input id="password" type="password" name="password" required>
    </div>

    <div>
        <label for="password-confirm">È·ÈÏÃÜÂë</label>
        <input id="password-confirm" type="password" name="password_confirmation" required>
    </div>

    <div>
        <button type="submit">×¢²á</button>
    </div>
</form>

µÇ¼ºó¸´ÖÆ

È»ºóÎÒÃǾÍÀֳɵؽ¨ÉèÁËÓû§×¢²áÁ÷³Ì¡£

Óû§µÇ¼Á÷³Ì

LaravelÌṩÁ˷dz£¿ì½ÝµÄÓû§µÇ¼ҪÁ죬ÎÒÃÇ¿ÉÒÔʹÓøÃÒªÁìÀ´¾ÙÐÐÓû§µÇ¼¡£ÔÚroutes/web.phpÖÐÌí¼ÓÒÔÏ·ÓÉ£º

Route::get('login', 'AuthLoginController@showLoginForm')->name('login');
Route::post('login', 'AuthLoginController@login');
Route::post('logout', 'AuthLoginController@logout')->name('logout');

µÇ¼ºó¸´ÖÆ

È»ºó½¨ÉèÒ»¸öÓëUserÄ£×Ó¶ÔÓ¦µÄLoginController¿ØÖÆÆ÷¡£ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇÐèҪʵÏÖÒÔÏÂÒªÁ죺

use IlluminateFoundationAuthAuthenticatesUsers;
public function login(Request $request)
{
    $this->validateLogin($request);

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    return $this->sendFailedLoginResponse($request);
}

// ÈÏÖ¤Óû§ÃûºÍÃÜÂë
protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        $this->credentials($request), $request->filled('remember')
    );
}

// »ñÈ¡Óû§ÃûºÍÃÜÂë
protected function credentials(Request $request)
{
    return $request->only($this->username(), 'password');
}

public function username()
{
    return 'email';
}

// Óû§Í˳öµÇ¼ҪÁì
public function logout(Request $request)
{
    $this->guard()->logout();

    $request->session()->invalidate();

    return redirect('/');
}

µÇ¼ºó¸´ÖÆ

½Ó×Å£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÓû§µÇ¼ÊÓͼ¡£ÔÚÊÓͼÖУ¬ÎÒÃÇʹÓÃÒÔÏÂ±íµ¥À´ÎüÊÕÓû§Êý¾Ý£º

<form method="POST" action="{{ route('login') }}">
    @csrf
    <div>
        <label for="email">ÓÊÏä</label>
        <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus>
    </div>

    <div>
        <label for="password">ÃÜÂë</label>
        <input id="password" type="password" name="password" required>
    </div>

    <div>
        <input type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
        <label for="remember">¼Ç×ÅÎÒ</label>
    </div>

    <div>
        <button type="submit">怬</button>
    </div>
</form>

µÇ¼ºó¸´ÖÆ

ÔÚ½¨ÉèºÃÒÔÉÏÊÓͼºÍ¿ØÖÆÆ÷Ö®ºó£¬ÎÒÃǾÍʵÏÖÁËLaravel¿ò¼ÜµÄÓû§µÇ¼ºÍ×¢²áÁ÷³Ì¡£ÐèҪעÖصÄÊÂÏîÊÇÓû§µÇ¼ÀÖ³ÉÖ®ºó£¬ÐèÒª½«Óû§ÐÅÏ¢´æÈësessionÖУ¬ÒÔ±ãÓÚºóÐø¹¦Ð§µÄŲÓá£ÔÚ¿ØÖÆÆ÷µÄµÚ¶þ¸öÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravel¿ò¼ÜÌṩµÄattemptLoginÒªÁìÀ´ÑéÖ¤Óû§µÄµÇ¼ÐÅÏ¢£¬ÈôÊÇÑé֤ͨ¹ý¾Í½«Óû§ÐÅÏ¢´æÈësession£¬²¢ÇÒ½«Óû§Öض¨Ïòµ½×ÓÍøÖ·¡£

×ÛÉÏ£¬Laravel¿ò¼ÜµÄÓû§µÇ¼ºÍ×¢²áÁ÷³ÌºÜÊǼòÆÓÒ׶®£¬Í¨¹ýÒÔÉϰ취ʵÏÖÓû§µÇ¼ºÍ×¢²á¹¦Ð§Ö»ÐèÒª¼òÆӵļ¸ÐдúÂë¼´¿É¡£ËäÈ»£¬ÈôÊÇÓû§ÐèÒª¸üΪ¸ß¼¶µÄÈÏÖ¤ºÍȨÏÞÖÎÀí£¬Ò²¿ÉÒÔʹÓÃLaravel¿ò¼ÜµÄÀ©Õ¹ÐÔ£¬ÔÚÒÔÉÏÄ£×ӺͿØÖÆÆ÷µÄ»ù´¡ÉÏÌí¼ÓһЩ×Ô½ç˵µÄÒªÁì¡£

ÒÔÉϾÍÊÇlaravel¿ò¼ÜµÇ¼ע²áÁ÷³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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