尊龙凯时人生就是搏

laravel ʵÏֵǼע²á

laravel ÊÇÒ»ÖÖÊ¢ÐÐµÄ php ¿ò¼Ü£¬ËüÌṩÁËÒ»¸öÇ¿Ê¢µÄ¿ª·¢ÇéÐΣ¬¿ÉÒÔÈÃÄú¸üÈÝÒ׵ع¹½¨ web Ó¦ÓóÌÐò¡£ÆäÖÐÒ»¸öÖ÷ÒªµÄÌØÕ÷ÊÇ laravel ×Ô´øµÄÉí·ÝÑé֤ϵͳ£¬¿ÉÒÔÈÃÄú¿ìËÙʵÏÖÓû§ÈÏÖ¤£¬°üÀ¨µÇ¼ºÍ×¢²á¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÑÝʾÔõÑùʹÓà laravel ʵÏֵǼע²á¡£

°²ÅÅÇéÐÎ

ÔÚ×îÏÈʵÏÖÉí·ÝÑé֤֮ǰ£¬ÎÒÃÇÐèҪȷ±£ Laravel ÇéÐÎÒѾ­ÉèÖúò¢ÇÒÊý¾Ý¿âÅþÁ¬ÒѾ­ÉèÖá£ÈôÊÇÄú»¹Ã»ÓÐ×°Öà Laravel£¬¿ÉÒԲο¼¹Ù·½ÎĵµÖеÄ×°ÖÃÖ¸ÄÏ¡£ÔÚ Laravel Ó¦ÓóÌÐòÖУ¬ÎÒÃÇʹÓà Artisan ÏÂÁîÐй¤¾ß½¨ÉèÐëÒªµÄÎļþºÍĿ¼½á¹¹¡£ÔÚÏÂÁîÐÐÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÐ嵀 Laravel Ó¦ÓóÌÐò£º

composer create-project –prefer-dist laravel/laravel blog

½¨ÉèÖ®ºó£¬µ¼º½µ½Ó¦ÓóÌÐòµÄ¸ùĿ¼£¬ÔËÐÐÒÔÏÂÏÂÁîÌìÉúÓ¦ÓóÌÐòÃÜÔ¿£º

php artisan key:generate

×¢²á·ÓÉ

ÔÚ Laravel ÖУ¬Â·ÓÉÊÇÅþÁ¬ URI ºÍ¶ÔÓ¦¿ØÖÆÆ÷ÒªÁìµÄÇÅÁº¡£Òª×¢²áÎÒÃǵÄÉí·ÝÑé֤·ÓÉ£¬ÎÒÃÇÐèÒª¸üРroutes/web.php Îļþ¡£ÔÚÕâ¸öÎļþÖУ¬ÎÒÃǽç˵ÁËÓ¦ÓóÌÐòµÄ·ÓÉ£¬ÆäÖаüÀ¨µÇ¼ºÍ×¢²á·ÓÉ¡£

Ê×ÏÈ£¬ÎÒÃÇÐèÒª½ç˵·¢ËÍ POST ÇëÇóµ½ /register µÄ·ÓÉ£¬²¢½«Æä°ó¶¨µ½ RegisterController µÄ register() ÒªÁì¡£Õ⽫·ºÆðÒ»¸ö×¢²á±íµ¥£¬Óû§¿ÉÒÔÌîдÆäÓû§ÃûºÍÃÜÂë¡£

Route::post(‘/register’, ‘AuthRegisterController@register’)->name(‘register’);

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½ç˵·¢ËÍ POST ÇëÇóµ½ /login µÄ·ÓÉ£¬²¢½«Æä°ó¶¨µ½ LoginController µÄlogin() ÒªÁì¡£Õ⽫·ºÆðÒ»¸öµÇ¼±íµ¥£¬Óû§¿ÉÒÔÌîдµÇ¼ÃûºÍÃÜÂë¡£ÈôÊÇÓû§Ã»ÓÐÓÐÓõÄƾ֤¾ÙÐÐÑéÖ¤£¬ÔòÓ¦ÓóÌÐò»áÖض¨Ïòµ½µÇ¼±íµ¥¡£

Route::post(‘/login’, ‘AuthLoginController@login’)->name(‘login’);

×îºó£¬ÎÒÃÇÐèÒª½ç˵ËùÓÐÊܱ £»¤µÄ·ÓÉ¡£ÔÚ Laravel ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà auth ÖÐÐļþÀ´È·±£Óû§ÒѾ­Í¨¹ýÉí·ÝÑéÖ¤¡£ÕâÆäÖÐÐļþ½«×Ô¶¯Öض¨ÏòδÂÄÀúÖ¤µÄÓû§µ½ /login ·ÓÉ¡£

Route::middleware([‘auth’])->group(function () {

// your protected routes go here

µÇ¼ºó¸´ÖÆ

});

´¦Àí¿ØÖÆÆ÷

ÎÒÃÇÒѾ­½ç˵ÁËÐëÒªµÄ·ÓÉ£¬ÏÖÔÚÐèÒªÔÚ¿ØÖÆÆ÷Öд¦ÀíËüÃÇ¡£ÔÚ Laravel ÖУ¬¿ØÖÆÆ÷ÊÇ´¦ÀíÌض¨ HTTP ÇëÇóµÄÀ࣬¿ØÖÆÆ÷ÖеÄÒªÁì·µ»Ø HTTP ÏìÓ¦¡£ÔÚÓ¦ÓóÌÐòÖУ¬ÎÒÃÇÐèÒª½¨ÉèÁ½¸ö¿ØÖÆÆ÷ÓÃÓÚ´¦Àí×¢²áºÍµÇ¼ÇëÇó¡£

ÔÚ app/Http/Controllers/Auth Ŀ¼Ï£¬½¨Éè LoginController.php ºÍ RegisterController.php Á½¸öÎļþ£¬ÕâÁ½¸öÎļþÊÇ Laravel ×Ô´øµÄ¿ØÖÆÆ÷Àà¡£ÔÚÕâЩ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇÐèÒªÁýÕÖ Laravel µÄĬÈÏÒªÁ죬ΪµÇ¼ºÍ×¢²áÇëÇóÌṩ×Ô½ç˵µÄ¹¦Ð§¡£

µÇ¼¿ØÖÆÆ÷

ÎÒÃÇÊ×ÏÈÀ´¿´ LoginController.php ¿ØÖÆÆ÷¡£´Ë¿ØÖÆÆ÷°üÀ¨Á½¸öÒªÁ죺showLoginForm() ºÍ login()¡£

showLoginForm() ÒªÁì·µ»ØµÇ¼±íµ¥ÊÓͼ£¨resources/views/auth/login.blade.php£©£¬¸ÃÊÓͼ°üÀ¨Ò»¸ö±íµ¥£¬Óû§¿ÉÒÔÊäÈëµÇ¼ÃûºÍÃÜÂë¡£Õâ¸öÒªÁìÊÇÓÉ Laravel ËùÌṩµÄ¡£

public function showLoginForm()

{

return view('auth.login');

µÇ¼ºó¸´ÖÆ

}

login() ÒªÁìÊÇÕæÕýʵÏֵǼÂß¼­µÄµØ·½¡£Õâ¸öÒªÁìÎüÊÕÒ»¸ö IlluminateHttpRequest ʵÀý£¬²¢Ê¹ÓøÃʵÀýµÄ validate() ÒªÁìÑéÖ¤Óû§ÊäÈë¡£ÈôÊÇ±íµ¥ÑéÖ¤Àֳɣ¬Laravel ½«×Ô¶¯ËÑË÷ʹÓøø¶¨µÇ¼ÃûºÍÃÜÂëµÄÓû§£¬²¢½«ÆäµÇ¼µ½Ó¦ÓóÌÐòÖС£

public function login(Request $request)

{

$request->validate([
    'email' => 'required|string|email',
    'password' => 'required|string',
    'remember' => 'boolean'
]);

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

if (Auth::attempt($credentials, $request->remember)) {
    return redirect()->intended('dashboard');
}

return redirect()->back()->withInput($request->only('email', 'remember'));

µÇ¼ºó¸´ÖÆ

}

×¢ÖØ£¬attempt() ÒªÁ콫ƾ֤¸ø¶¨µÄƾ֤À´×Ô¶¯¼ì²éÃÜÂëÊÇ·ñ׼ȷ¡£ÈôÊÇÃÜÂ벻׼ȷ£¬Ôò»á·µ»Ø false¡£

ÈôÊÇÓû§ÒѾ­ÀÖ³ÉÑéÖ¤£¬²¢ÇÒËûÃÇ֮ǰÇëÇóµÄ URL ±»´æ´¢ÆðÀ´£¨Í¨³£Ö¸±» auth ÖÐÐļþ×èÖ¹µÄURL£©£¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel ¸¨Öúº¯Êý intended() À´½«ÆäÖض¨Ïòµ½¸Ã URL¡£ÈôÊÇδ´æ´¢ÈκΠURL£¬ÔòÖض¨Ïòµ½Ç°¶Ë¿Ø¼þ̨£¨/dashboard£©¡£

×¢²á¿ØÖÆÆ÷

½ÓÏÂÀ´ÎÒÃÇÀ´¿´ RegisterController.php ¿ØÖÆÆ÷¡£³ýÁË Laravel µÄĬÈÏÒªÁìÖ®Í⣬ÎÒÃÇ»¹ÐèҪʵÏÖ register() ÒªÁì¡£

register() ÒªÁìºÜÊÇÀàËÆÓÚ login() ÒªÁì¡£ËüÎüÊÕÒ»¸ö IlluminateHttpRequest ʵÀý£¬²¢Ê¹ÓøÃʵÀýµÄ validate() ÒªÁìÑéÖ¤Óû§ÊäÈë¡£ÈôÊÇ±íµ¥ÑéÖ¤Àֳɣ¬Laravel ½«½¨ÉèÒ»¸öÐÂÓû§£¬²¢½«ÆäÉúÑĵ½Êý¾Ý¿âÖС£È»ºó£¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄĬÈÏÐÐΪ½«Óû§µÇ¼µ½Ó¦ÓóÌÐòÖС£

public function register(Request $request)

{

$request->validate([
    'name' => 'required|string|max:255',
    'email' => 'required|string|email|max:255|unique:users',
    'password' => 'required|string|min:6|confirmed',
]);

$user = User::create([
    'name' => $request->name,
    'email' => $request->email,
    'password' => Hash::make($request->password),
]);

Auth::login($user);

return redirect()->intended('dashboard');

µÇ¼ºó¸´ÖÆ

}

ÔÚ×¢²á¿ØÖÆÆ÷ÖÐÎÒÃÇ¿ÉÒÔʹÓà Hash ¸¨Öúº¯ÊýÀ´¶ÔÃÜÂë¾ÙÐмÓÃÜ¡£ÈôÊÇÃÜÂëÑéÖ¤Àֳɣ¬ÎÒÃǽ«½¨ÉèÐÂÓû§²¢½«ÆäµÇ¼µ½Ó¦ÓóÌÐòÖС£

ÊÓͼ²ã

ÏÖÔÚÎÒÃÇÒѾ­½ç˵ÁËÐëÒªµÄ·ÓɺͿØÖÆÆ÷£¬½ÓÏÂÀ´ÐèÒª½¨ÉèÉí·ÝÑéÖ¤ÊÓͼ¡£

ÔÚ resources/views/auth Ŀ¼ÖУ¬ÎÒÃÇ¿ÉÒÔ½¨Éè login.blade.php ºÍ register.blade.php Á½¸öÊÓͼÎļþ¡£ÕâЩÊÓͼ°üÀ¨µÇ¼ºÍ×¢²á±íµ¥£¬Ê¹ÓÃÁËһЩ Laravel ×ÊÖú³ÌÐò£¬¿ÉÒÔ´¦Àí±íµ¥ÑéÖ¤²¢ÏÔʾ¹ýʧÐÂÎÅ¡£

µÇ¼ÊÓͼ

ÔÚ login.blade.php ÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄ Form ¸¨Öúº¯Êý½¨ÉèµÇ¼±íµ¥¡£Õâ¸öº¯Êý»á×Ô¶¯Îª±íµ¥Ìí¼Ó CSRF ÁîÅÆ£¬²¢ÎªÊäÈë×ֶαàд HTML ±ê¼Ç¡£

@csrf

<div class="form-group row">
    <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('Email Address') }}</label>

    <div class="col-md-6">
        <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>

        @if ($errors->has('email'))
            <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('email') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

    <div class="col-md-6">
        <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

        @if ($errors->has('password'))
            <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('password') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <div class="col-md-6 offset-md-4">
        <div class="form-check">
            <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

            <label class="form-check-label" for="remember">
                {{ __('Remember Me') }}
            </label>
        </div>
    </div>
</div>

<div class="form-group row mb-0">
    <div class="col-md-8 offset-md-4">
        <button type="submit" class="btn btn-primary">
            {{ __('Login') }}
        </button>

        @if (Route::has('password.request'))
            <a class="btn btn-link" href="{{ route('password.request') }}">
                {{ __('Forgot Your Password?') }}
            </a>
        @endif
    </div>
</div>

µÇ¼ºó¸´ÖÆ

×¢ÖØ£¬ÎÒÃÇʹÓÃÁË Blade Ä£°åÒýÇæµÄ @csrf ºÍ @if Óï¾äÀ´ÌìÉúÐëÒªµÄ CSRF ÁîÅƲ¢ÏÔʾÑéÖ¤¹ýʧ¡£

×¢²áÊÓͼ

ÔÚ register.blade.php ÎļþÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel µÄ Form ×ÊÖúÆ÷½¨Éè×¢²á±íµ¥¡£Õâ¸öº¯Êý×Ô¶¯Îª±íµ¥Ìí¼Ó CSRF ÁîÅÆ£¬²¢ÎªÊäÈë×ֶαàд HTML ±ê¼Ç¡£

@csrf

<div class="form-group row">
    <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

    <div class="col-md-6">
        <input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>

        @if ($errors->has('name'))
            <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('name') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

    <div class="col-md-6">
        <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

        @if ($errors->has('email'))
            <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('email') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

    <div class="col-md-6">
        <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

        @if ($errors->has('password'))
            <span class="invalid-feedback" role="alert">
                <strong>{{ $errors->first('password') }}</strong>
            </span>
        @endif
    </div>
</div>

<div class="form-group row">
    <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

    <div class="col-md-6">
        <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
    </div>
</div>

<div class="form-group row mb-0">
    <div class="col-md-6 offset-md-4">
        <button type="submit" class="btn btn-primary">
            {{ __('Register') }}
        </button>
    </div>
</div>

µÇ¼ºó¸´ÖÆ

×¢ÖØ£¬ÎÒÃÇʹÓÃÁË Blade Ä£°åÒýÇæµÄ @csrf ºÍ @if Óï¾äÀ´ÌìÉúÐëÒªµÄ CSRF ÁîÅƲ¢ÏÔʾÑéÖ¤¹ýʧÐÂÎÅ¡£

½áÂÛ

ͨ¹ý Laravel ¿ÉÒÔ¿ìËÙ¡¢Àû±ã¡¢Çå¾²µØʵÏÖ Web Ó¦ÓóÌÐòµÄµÇ¼ºÍ×¢²áÉí·ÝÑéÖ¤¹¦Ð§£¬´Ó¶ø± £»¤ÄúµÄÓ¦ÓóÌÐòÃâÊÜδÊÚȨµÄ»á¼û¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁË Laravel µÄÉí·ÝÑé֤ϵͳ²¢ÊµÏÖÁË×¢²áºÍµÇ¼¡£ÏÖÔÚ£¬Äú¿ÉÒÔʹÓÃÕâЩ»ù´¡ÖªÊ¶¹¹½¨¸üÍêÕûµÄÓû§Éí·ÝÑé֤ϵͳ£¬»ò½«Æ伯³Éµ½ÄúµÄÏÖÓÐÓ¦ÓóÌÐòÖС£

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

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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