ÔõÑùʹÓÃLaravel 5À´ÊµÏÖÉÏ°¶×¢²á¹¦Ð§
laravelÊÇÒ»ÖÖ»ùÓÚphpÓïÑԵĿªÔ´web¿ò¼Ü£¬±»Ô½À´Ô½¶àµÄ¿ª·¢ÕßʹÓá£laravel¿ò¼ÜÄÚÖÃÁËÐí¶àÓÐÓõĹ¦Ð§£¬ÈçÊÓͼ¡¢Â·ÓÉ¡¢Êý¾Ý¿â²Ù×÷µÈ£¬Í¬Ê±»¹Ö§³ÖÍØÕ¹¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel 5À´ÊµÏÖÉÏ°¶×¢²á¹¦Ð§¡£
×°ÖÃLaravel
Ê×ÏÈÐèÒªÔÚϵͳÖÐ×°ÖÃLaravel¡£×°ÖÃÒªÁì¿ÉÒԲο¼Laravel¹Ù·½Îĵµ»ò×ÔÐÐËÑË÷¡£
½¨ÉèÊý¾Ý¿â
ÉÏ°¶×¢²á¹¦Ð§ÐèҪʹÓÃÊý¾Ý¿âÀ´´æ´¢Óû§ÐÅÏ¢¡£ÔÚ´Ë֮ǰÐèÒª½¨ÉèÒ»¸öÃûΪ¡°laravel_login¡±µÄÊý¾Ý¿â¡£¿ÉÒÔÔÚÏÂÁîÐÐÖÐÊäÈëÈçÏÂÖ¸Á
mysql -u username -p CREATE DATABASE laravel_login;
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬¡°username¡±ÊÇÄãµÄMySQLÓû§Ãû¡£Õ⽫½¨ÉèÒ»¸ö¿ÕµÄÃûΪ¡°laravel_login¡±µÄÊý¾Ý¿â¡£
½¨ÉèÓû§±í
ÔÚLaravel 5ÖУ¬Í¨¹ýǨáãÀ´½¨ÉèºÍÖÎÀíÊý¾Ý¿â±í¡£Ê×ÏÈÐèÒªÌìÉúÒ»¸öǨáãÎļþ¡£ÏÂÁîÐÐÖÐÊäÈ룺
php artisan make:migration create_users_table
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áÌìÉúÒ»¸öǨáãÎļþ£¬Ä¬ÈÏ»áÉúÑÄÔÚ¡°database\migrations¡±Îļþ¼ÐÖС£
±à¼Õâ¸öǨáãÎļþ£¬ÔÚ¡°up()¡±ÒªÁìÖнç˵Óû§±íµÄ×ֶΣº
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
µÇ¼ºó¸´ÖÆ
È»ºóÊäÈëÒÔÏÂÏÂÁîÒÔÔËÐÐÕâ¸öǨáã²¢ÌìÉúÓû§±í£º
php artisan migrate
µÇ¼ºó¸´ÖÆ
½¨Éè¿ØÖÆÆ÷ºÍÊÓͼ
½ÓÏÂÀ´½¨Éè¿ØÖÆÆ÷ºÍÊÓͼ£¬ÓÃÓÚÏÔʾע²áºÍÉÏ°¶Ò³Ã棬²¢´¦ÀíÓû§ÊäÈëµÄÐÅÏ¢¡£ÔÚÏÂÁîÐÐÖÐÊäÈ룺
php artisan make:controller AuthController
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ½¨ÉèÒ»¸öÃûΪ¡°AuthController¡±µÄ¿ØÖÆÆ÷£¬Ä¬ÈϻᱻÉúÑÄÔÚ¡°app\Http\Controllers¡±Îļþ¼ÐÖС£
¿ØÖÆÆ÷´úÂëÈçÏ£º
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class AuthController extends Controller { public function showLoginForm() { return view('auth.login'); } public function login(Request $request) { // ÉÏ°¶Âß¼ } public function showRegistrationForm() { return view('auth.register'); } public function register(Request $request) { // ×¢²áÂß¼ } public function logout(Request $request) { // ×¢ÏúÂß¼ } }
µÇ¼ºó¸´ÖÆ
È»ºóÐèÒª½¨ÉèÊÓͼģ°å£¬ÓÃÓÚäÖȾ±íµ¥¡£ÔÚ¡°resources\views\auth¡±Îļþ¼ÐÖн¨ÉèÃûΪ¡°login.blade.php¡±ºÍ¡°register.blade.php¡±µÄÎļþ£¬»®·ÖÊÇÉÏ°¶ºÍ×¢²áµÄÒ³ÃæÄ£°å¡£
Ä£°å´úÂëÈçÏ£º
ÉÏ°¶Ä£°å£º
@extends('layouts.app') @section('content') <div> <div> <div> <div> <div>Login</div> <div> <form> {{ csrf_field() }} <div>has('email') ? ' has-error' : '' }}"> <label>E-Mail Address</label> <div> <input> @if ($errors->has('email')) <span> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> </div> <div>has('password') ? ' has-error' : '' }}"> <label>Password</label> <div> <input> @if ($errors->has('password')) <span> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div> <div> <div> <label> <input> Remember Me </label> </div> </div> </div> <div> <div> <button> Login </button> <a> Forgot Your Password? </a> </div> </div> </form> </div> </div> </div> </div> </div> @endsection
µÇ¼ºó¸´ÖÆ
×¢²áÄ£°å£º
@extends('layouts.app') @section('content') <div> <div> <div> <div> <div>Register</div> <div> <form> {{ csrf_field() }} <div>has('name') ? ' has-error' : '' }}"> <label>Name</label> <div> <input> @if ($errors->has('name')) <span> <strong>{{ $errors->first('name') }}</strong> </span> @endif </div> </div> <div>has('email') ? ' has-error' : '' }}"> <label>E-Mail Address</label> <div> <input> @if ($errors->has('email')) <span> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> </div> <div>has('password') ? ' has-error' : '' }}"> <label>Password</label> <div> <input> @if ($errors->has('password')) <span> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> </div> <div> <label>Confirm Password</label> <div> <input> </div> </div> <div> <div> <button> Register </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection
µÇ¼ºó¸´ÖÆ
Íê³É·ÓÉ
×îºóÒ»²½ÊÇÉèÖ÷ÓÉ£¬ÒÔ±ãLaravel¿ÉÒÔ׼ȷŲÓÿØÖÆÆ÷ºÍÊÓͼ¡£ÔÚ¡°routes\web.php¡±ÎļþÖмÓÈëÒÔÏ´úÂ룺
Route::get('login', 'AuthController@showLoginForm')->name('login'); Route::post('login', 'AuthController@login'); Route::get('register', 'AuthController@showRegistrationForm')->name('register'); Route::post('register', 'AuthController@register'); Route::post('logout', 'AuthController@logout')->name('logout');
µÇ¼ºó¸´ÖÆ
ÏÖÔÚÄã¿ÉÒÔ»á¼ûÈçϵصãÀ´Éó²é×¢²áºÍÉÏ°¶µÄÒ³Ã棺
http://yourdomain.com/register http://yourdomain.com/login
µÇ¼ºó¸´ÖÆ
ÒÔ¼°ÈçϵصãÀ´Íê³ÉÉÏ°¶ºÍ×¢Ïú²Ù×÷£º
http://yourdomain.com/login http://yourdomain.com/logout
µÇ¼ºó¸´ÖÆ
¿¢ÊÂÓï
ͨ¹ý±¾½Ì³Ì£¬Äãѧ»áÁËÔõÑùÔÚLaravel 5ÖÐʵÏÖÉÏ°¶×¢²áµÄ¹¦Ð§¡£ËäÈ»£¬ÕâÖ»ÊÇ×î»ù±¾µÄʵÏÖ·½·¨¡£ÔÚ²Ù×÷¡¢Çå¾²ÐÔ·½ÃæµÈµÈ£¬ÉÐÓÐÐí¶àҪעÖصĵط½£¬ÐèÒªÔÚÏÖʵ¿ª·¢ÖÐһֱѧϰºÍÍêÉÆ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravel 5À´ÊµÏÖÉÏ°¶×¢²á¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡