laravelʵÏÖ×¢²áµÇ¼
laravelÊÇÒ»¿î³£ÓõÄphp¿ò¼Ü£¬¾ßÓÐÓÅÑŵÄÓï·¨¡¢Ç¿Ê¢µÄ¹¦Ð§ºÍÎĵµ¸»ºñµÄÌص㣬ÒѾ³ÉΪÖÚ¶àphp¿ª·¢Ö°Ô±µÄÊ×Ñ¡¿ò¼Ü¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃlaravel¿ò¼ÜʵÏÖ×¢²áºÍµÇ¼¹¦Ð§¡£
Ò»¡¢½¨ÉèLaravelÓ¦ÓóÌÐò
ÔÚ×îÏÈʵÏÖ×¢²áºÍµÇ¼¹¦Ð§Ö®Ç°£¬Ê×ÏÈÐèÒª½¨ÉèÒ»¸öLaravelÓ¦ÓóÌÐò¡£¿ÉÒÔʹÓÃLaravel¹Ù·½ÌṩµÄComposerÏÂÁîÀ´½¨ÉèÐÂÓ¦ÓóÌÐò£¬ÈçÏÂËùʾ£º
composer create-project --prefer-dist laravel/laravel your-project-name
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬your-project-nameÊÇÄãÏëÒª½¨ÉèµÄÓ¦ÓóÌÐòµÄÃû³Æ¡£
½¨ÉèÍê³Éºó£¬½øÈëÓ¦ÓóÌÐòĿ¼²¢Æô¶¯ÍâµØЧÀÍÆ÷£º
cd your-project-name php artisan serve
µÇ¼ºó¸´ÖÆ
ÔÚä¯ÀÀÆ÷ÖÐÊäÈëhttp://localhost:8000£¬¼´¿É»á¼ûÓ¦ÓóÌÐòµÄ½Ó´ýÒ³Ãæ¡£
¶þ¡¢½¨ÉèÈÏ֤ϵͳ
ÔÚʵÏÖ×¢²áºÍµÇ¼¹¦Ð§Ö®Ç°£¬ÐèÒªÏȽ¨ÉèÒ»¸ö»ù±¾µÄÈÏ֤ϵͳ¡£LaravelÌṩÁËmake:auth ArtisanÏÂÁ¿ÉÒÔ¿ìËÙÌìÉúÈÏÖ¤Ïà¹ØµÄÊÓͼºÍ¿ØÖÆÆ÷¡£
php artisan make:auth
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÉÏÊöÏÂÁîºó£¬Laravel»á×Ô¶¯½¨Éèregister¡¢loginºÍlogoutµÈÏà¹ØµÄÊÓͼºÍ¿ØÖÆÆ÷£¬²¢½«ËüÃÇÌí¼Óµ½Ó¦ÓóÌÐòÖС£±ðµÄ£¬Laravel»¹»áÔÚÊý¾Ý¿âÖн¨ÉèÏà¹ØµÄÓû§ºÍÃÜÂëÖØÖÃ±í¡£
Èý¡¢½¨ÉèÊý¾Ý¿â±í
ĬÈÏÇéÐÎÏ£¬LaravelʹÓÃMySQLÊý¾Ý¿â¡£ÔÚ±¾ÀýÖУ¬ÐèÒª½¨ÉèÒ»¸öÃûΪusersµÄÊý¾Ý±íÀ´´æ´¢Óû§Êý¾Ý¡£¿ÉÒÔʹÓÃÒÔÏÂArtisanÏÂÁîÀ´½¨Éè¸Ã±í£º
php artisan make:migration create_users_table --create=users
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÉÏÊöÏÂÁîºó£¬Laravel»áÔÚÓ¦ÓóÌÐòµÄdatabase/migrationsĿ¼Öн¨ÉèÒ»¸öÃûΪcreate_users_tableµÄǨáãÎļþ¡£·¿ª¸ÃÎļþ²¢ÐÞ¸ÄupÒªÁ죬ÈçÏÂËùʾ£º
public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
µÇ¼ºó¸´ÖÆ
ÉÏÊöǨáãÎļþ½ç˵ÁËusersÊý¾Ý±íµÄ×ֶΣ¬°üÀ¨name£¨Óû§Ãû£©¡¢email£¨µç×ÓÓʼþµØµã£©¡¢password£¨ÃÜÂ룩µÈµÈ¡£
ÔÚÐÞ¸ÄÍêǨáãÎļþÖ®ºó£¬ÐèÒªÖ´ÐÐÒÔÏÂArtisanÏÂÁÀ´ÏÖʵ½¨ÉèusersÊý¾Ý±í£º
php artisan migrate
µÇ¼ºó¸´ÖÆ
ËÄ¡¢½¨Éè×¢ÊéÒ³Ãæ
½ÓÏÂÀ´£¬ÐèÒª½¨ÉèÒ»¸ö×¢ÊéÒ³Ã棬ÈÃÓû§¿ÉÒÔÊäÈë×Ô¼ºµÄÓû§Ãû¡¢µç×ÓÓʼþµØµãºÍÃÜÂë¡£·¿ªresources/views/auth/register.blade.phpÎļþ£¬¿ÉÒÔ¿´µ½LaravelÒѾΪÎÒÃǽ¨ÉèÁËÒ»¸ö»ù±¾µÄ×¢²á±íµ¥¡£
´Ëʱ£¬ÎÒÃÇÐèҪɾ³ýһЩ×ֶΣ¬Í¬Ê±ÔöÌí±ØÐèÌîдµÄname×ֶκÍÈ·ÈÏÃÜÂëpassword_confirmation×ֶΡ£Ð޸ĺóµÄ´úÂëÈçÏÂËùʾ£º
<form method="POST" action="{{ route('register') }}"> @csrf <div> <label for="name">{{ __('Name') }}</label> <div> <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus> </div> </div> <div> <label for="email">{{ __('E-Mail Address') }}</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required> </div> </div> <div> <label for="password">{{ __('Password') }}</label> <div> <input id="password" type="password" name="password" required> </div> </div> <div> <label for="password-confirm">{{ __('Confirm Password') }}</label> <div> <input id="password-confirm" type="password" name="password_confirmation" required> </div> </div> <div> <button type="submit"> {{ __('Register') }} </button> </div> </form>
µÇ¼ºó¸´ÖÆ
Îå¡¢´¦Àí×¢²áÇëÇó
ÏÖÔÚ£¬×¢²á±íµ¥ÒѾ½¨É裬¿ÉÊǵ±Óû§Ìá½»±íµ¥Ê±²¢²»»áÉúЧ¡£ÒÔÊÇÎÒÃÇÐèÒªÐÞ¸Äapp/Http/Controllers/Auth/RegisterController.phpÎļþÀ´´¦Àí×¢²áÇëÇó¡£
public function store(Request $request) { $this->validate($request, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|unique:users|max:255', 'password' => 'required|string|min:8|confirmed', ]); User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => bcrypt($request->password), ]); return redirect()->intended('dashboard'); }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë½ç˵ÁËÒ»¸öÃûΪstore()µÄÒªÁ죬´ËÒªÁìÓÃÓÚ´¦Àí×¢²áÇëÇó¡£Ê×ÏÈ£¬validate()ÒªÌå»áÑéÖ¤ÇëÇóµÄÊý¾Ý¡£ÈôÊÇÑé֤ͨ¹ý£¬ÔòʹÓÃUserÄ£×ÓÀ´½¨ÉèÒ»¸öÐÂÓû§£¬²¢¶ÔÃÜÂë¾ÙÐмÓÃÜ¡£×îºó£¬Öض¨Ïòµ½Ó¦ÓóÌÐòµÄÒDZí°åÒ³Ãæ¡£
Áù¡¢½¨ÉèµÇ¼ҳÃæ
½ÓÏÂÀ´£¬ÐèÒª½¨ÉèÒ»¸öµÇ¼ҳÃ棬ÈÃÓû§¿ÉÒÔÊäÈë×Ô¼ºµÄÒÑ×¢²áµç×ÓÓʼþµØµãºÍÃÜÂë¡£·¿ªresources/views/auth/login.blade.phpÎļþ£¬¿ÉÒÔ¿´µ½LaravelÒѾΪÎÒÃǽ¨ÉèÁËÒ»¸ö»ù±¾µÄµÇ¼±íµ¥¡£
Æß¡¢´¦ÀíµÇ¼ÇëÇó
×¢²á±íµ¥ÒѾ½¨É裬¿ÉÊǵ±Óû§Ìá½»±íµ¥Ê±²¢²»»áÉúЧ¡£ÒÔÊÇÎÒÃÇÐèÒªÐÞ¸Äapp/Http/Controllers/Auth/LoginController.phpÎļþÀ´´¦ÀíµÇ¼ÇëÇó¡£
public function store(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { $request->session()->regenerate(); return redirect()->intended('dashboard'); } return back()->withErrors([ 'email' => 'The provided credentials do not match our records.', ]); }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë½ç˵ÁËÒ»¸öÃûΪstore()µÄÒªÁ죬´ËÒªÁìÓÃÓÚ´¦ÀíµÇ¼ÇëÇó¡£Ê×ÏÈ£¬attempt()ÒªÌå»áʵÑéʹÓÃÓû§ÌṩµÄƾ֤¾ÙÐÐÉí·ÝÑéÖ¤¡£ÈôÊÇÑéÖ¤Àֳɣ¬ÔòÌìÉúлỰ²¢½«Óû§Öض¨Ïòµ½Ó¦ÓóÌÐòµÄÒDZí°åÒ³Ãæ¡£ÈôÊÇʧ°Ü£¬Ôò»áʹÓÃback()ÒªÁ콫Óû§Öض¨Ïò»ØÔʼµÇ¼ҳÃ棬²¢ÏÔʾ¹ýʧÐÂÎÅ¡£
°Ë¡¢ÉèÖÃÈÏÖ¤ÖÐÐļþ
Ò»µ©×¢²áºÍµÇ¼¹¦Ð§ÒѾʵÏÖ£¬ÐèÒª±£»¤Ó¦ÓóÌÐòµÄijЩ·ÓÉ£¬Ö»ÓÐÒÑÈÏÖ¤µÄÓû§²Å»ª»á¼û¡£ÔÚLaravelÖУ¬¿ÉÒÔʹÓÃÖÐÐļþÀ´ÊµÏÖ´ËÄ¿µÄ¡£LaravelÌṩÁËÒ»¸öÃûΪauthµÄÖÐÐļþ£¬Ëü»áÑéÖ¤Óû§ÊÇ·ñÒѾµÇ¼µ½Ó¦ÓóÌÐò¡£
LaravelĬÈÏÇéÐÎϽ«authÖÐÐļþÓ¦ÓÃÓÚÈκÎʹÓÃauth·ÓÉÃû³ÆµÄ·ÓÉ¡£¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´Ö¸¶¨Ò»¸ö»ò¶àÆäÖÐÐļþ£º
Route::get('/dashboard', function () { // ½öÔÊÐíÒѵÇÈÎÃü»§»á¼û })->middleware(['auth']);
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬Ö»ÓÐÒÑÈÏÖ¤µÄÓû§²Å»ª»á¼û/dashboard·ÓÉ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃLaravel¿ò¼ÜʵÏÖ×¢²áºÍµÇ¼¹¦Ð§¡£½èÖúLaravel¿ò¼Ü£¬¿ÉÒÔ¿ìËÙÀû±ãµØ¹¹½¨Ò»¸öÇå¾²¿É¿¿µÄwebÓ¦ÓóÌÐò£¬Ôö½øwebÓ¦ÓóÌÐòµÄ¿ª·¢Àú³Ì¡£ÎÞÂÛÊdzõѧÕßÕÕ¾ÉÓÐÂÄÀúµÄ¿ª·¢Ö°Ô±£¬¶¼¿ÉÒÔʵÑéʹÓÃLaravel¿ò¼ÜÀ´¹¹½¨×Ô¼ºµÄwebÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇlaravelʵÏÖ×¢²áµÇ¼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡