laravel±íµ¥×¢²áÔõôд
laravel ÊÇÏÖÔÚ×îÊ¢ÐÐµÄ php ¿ª·¢¿ò¼ÜÖ®Ò»£¬ÆäÇ¿Ê¢¶øÎÞаµÄÌØÕ÷Ϊ web ¿ª·¢ÕßÌṩÁËÇ¿Ê¢µÄ¿ª·¢¹¤¾ßºÍ api¡£ÔÚ laravel ÖУ¬±íµ¥×¢²áÊÇÒ»¸öºÜ³£¼ûµÄ¹¦Ð§£¬½ñÌìÎÒÃǾÍÀ´Ñ§Ï°ÔõÑùÓà laravel ʵÏÖ±íµ¥×¢²á¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ Laravel Öн¨ÉèÒ»¸ö·ÓÉ£¬ÓÃÓÚ´¦Àí±íµ¥Ìá½»ºÍÏÔʾ±íµ¥Ò³Ãæ¡£ÎÒÃÇ¿ÉÒÔÔÚ routes/web.php ÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
Route::get('/register', 'AuthRegisterController@showRegistrationForm')->name('register'); Route::post('/register', 'AuthRegisterController@register')->name('register');
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀÎÒÃǽç˵ÁËÁ½¸ö·ÓÉ£¬Ò»¸öÊÇ GET ÇëÇó·ÓÉ£¬ÓÃÓÚÏÔʾע²á±íµ¥Ò³Ã棬ÁíÒ»¸öÊÇ POST ÇëÇó·ÓÉ£¬ÓÃÓÚ´¦Àí±íµ¥Ìá½»Êý¾Ý¡£ÎÒÃÇ»¹¸øÕâÁ½¸ö·ÓÉ»®·ÖÆðÁËÃû×ÖΪ register¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½ç˵һ¸ö¿ØÖÆÆ÷£¬À´´¦ÀíºÍäÖȾ±íµ¥Ò³Ã棬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÌìÉúÒ»¸ö¿ØÖÆÆ÷£º
php artisan make:controller AuthRegisterController
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇÐèÒªÔڸÿØÖÆÆ÷Öбàд´¦Àí±íµ¥ºÍäÖȾ±íµ¥µÄÒªÁ죬´úÂëÈçÏ£º
namespace AppHttpControllersAuth; use AppHttpControllersController; use IlluminateFoundationAuthRegistersUsers; use IlluminateSupportFacadesValidator; use IlluminateHttpRequest; class RegisterController extends Controller { use RegistersUsers; protected $redirectTo = '/home'; public function __construct() { $this->middleware('guest'); } public function showRegistrationForm() { return view('auth.register'); } public function register(Request $request) { $this->validator($request->all())->validate(); $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']), ]); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀÎÒÃÇʹÓÃÁË Laravel ÄÚÖÃµÄ RegistersUsers trait£¬À´Ìṩע²áÓû§¹¦Ð§£¬Ëü°üÀ¨ÁËһЩ³£ÓÃÒªÁ죬Èç validator() ÒªÁìºÍ create() ÒªÁì¡£ÕâЩҪÁì»®·ÖÓÃÓÚÑéÖ¤±íµ¥Êý¾ÝºÍ½¨ÉèÐÂÓû§¡£
ÔÚ showRegistrationForm() ÒªÁìÖУ¬ÎÒÃÇ·µ»ØÁËÒ»¸öÄ£°åÊÓͼ£¬¸ÃÊÓͼ°üÀ¨ÁËÎÒÃÇµÄ±íµ¥ÄÚÈÝ¡£ÔÚ register() ÒªÁìÖУ¬ÎÒÃÇÊ×ÏÈʹÓà validator() ÒªÁìÀ´ÑéÖ¤±íµ¥Êý¾Ý£¬ÈôÊÇÑé֤ͨ¹ý£¬ÎÒÃǾÍʹÓà create() ÒªÁìÀ´½¨ÉèÐÂÓû§£¬²¢Ê¹Óà guard() ÒªÁìÀ´µÇ¼¸ÃÓû§¡£
×îºó£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸ö±íµ¥ÊÓͼ£¬À´·ºÆð¸øÓû§×¢²á±íµ¥¡£ÎÒÃÇ¿ÉÒÔÔÚ resources/views/auth/register.blade.php ÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
<div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Register</div> <div class="card-body"> <form method="POST" action="{{ route('register') }}"> @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 @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus> @error('name') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </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 @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email"> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </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 @error('password') is-invalid @enderror" name="password" required autocomplete="new-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </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 autocomplete="new-password"> </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> </form> </div> </div> </div> </div> </div>
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀÎÒÃÇʹÓÃÁË Bootstrap ¿ò¼ÜÀ´ÃÀ»¯±íµ¥Ñùʽ¡£ÆäÖУ¬ÎÒÃÇʹÓÃÁË @csrf Ö¸ÁÀ´ÌìÉúÒ»¸öÒþ²ØµÄ CSRF token ÊäÈëÓò£¬Í¬Ê±Ò²ÔÚ form ±êÇ©µÄ action ÊôÐÔÖÐÖ¸¶¨ÁË×¢²á·ÓÉ¡£
µ½´ËΪֹ£¬Laravel ±íµ¥×¢²á¾ÍÍê³ÉÁË¡£ÎÒÃÇ¿ÉÒÔʹÓà php artisan serve ÏÂÁîÀ´Æô¶¯ÍâµØ¿ª·¢Ð§ÀÍÆ÷£¬²¢»á¼û http://localhost:8000/register À´Éó²é×¢²á±íµ¥Ò³Ãæ¡£µ±ÎÒÃÇÌîд׼ȷµÄ±íµ¥Êý¾Ý²¢Ìá½»±íµ¥Ê±£¬ÏµÍ³¾Í»á×¢²áÐÂÓû§²¢×Ô¶¯µÇ¼ËûÃÇ¡£
×ÜÖ®£¬Óà Laravel ʵÏÖ±íµ¥×¢²á¾ÍÊÇÕâô¼òÆÓ¡£Laravel ÌṩÁËÐí¶àÄÚÖõĹ¦Ð§ºÍ¹¤¾ß£¬À´×ÊÖúÎÒÃÇʵÏÖÖÖÖÖ¸÷ÑùµÄͨË׺ÍÖØ´óµÄ Web Ó¦Óá£ÈôÊÇÄãÉÆÓÚʹÓÃÕâЩ¹¦Ð§ºÍ¹¤¾ß£¬¾Í¿ÉÒÔ¿ìËÙ¹¹½¨³ö¸ßЧ¡¢Îȹ̡¢Ò×ά»¤µÄ Web Ó¦Óá£
ÒÔÉϾÍÊÇlaravel±íµ¥×¢²áÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡