Laravel 使用模式的默认身份验证
Laravel Default Auth with Modals
我正在尝试通过使用模态 windows 并添加一些新字段来修改 laravel 的默认身份验证系统。
所以,我有一个模态 window 来注册新用户,如下所示:
<!-- Modal Register area -->
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myRegisterLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form id="formRegister" class="login-form" action="{{ route('register') }}" method="POST">
{{ csrf_field() }}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myRegisterLabel">Sign Up</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>First Name:</label>
<input type="text" name="first_name" placeholder="first name..." value="{{ old('first_name') }}" required autofocus>
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" name="last_name" placeholder="last name..." value="{{ old('last_name') }}" required autofocus>
</div>
<div class="form-group">
<label>Email:</label>
<input type="email" name="email" placeholder="email..." value="{{ old('email') }}" required>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" name="password" required>
</div>
<div class="form-group">
<label>Password Confirmation:</label>
<input type="password" name="password_confirmation" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
@if(count($errors))
@foreach ($errors->all() as $error)
<div class="alert alert-danger">
<p>{{ $error }}</p>
</div>
@endforeach
@endif
</div>
</div>
</div>
<!-- End Modal Register area -->
然后我修改了 RegisterController 验证规则和创建方法以包含这个新字段以及迁移文件。 (所以,我在数据库中有文件,那里一切都很好)
注册控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
在您可以看到的表单中,我将其发布到 route('register') 但即使我在提交表单时没有收到任何错误并且被重定向到 / 用户没有保存在数据库中..
问题是,我不太确定这个默认的身份验证系统是如何工作的..
感谢您的帮助!
我认为提供的代码非常正确。确保您在迁移中删除了 name
字段表单 users
table。
使用您的代码,我能够使用您的模式和控制器注册用户。很新鲜 Laravel 5.4,您的模态被放置在 welcome.blade.php
。还添加了 css/js 个文件和一个显示模态的按钮。
这是我的 up()
更新迁移 users
table
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('name');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
});
}
还用新填充物更新了 User
模型:
protected $fillable = [
'email', 'password', 'first_name', 'last_name'
];
我正在尝试通过使用模态 windows 并添加一些新字段来修改 laravel 的默认身份验证系统。 所以,我有一个模态 window 来注册新用户,如下所示:
<!-- Modal Register area -->
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myRegisterLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form id="formRegister" class="login-form" action="{{ route('register') }}" method="POST">
{{ csrf_field() }}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myRegisterLabel">Sign Up</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>First Name:</label>
<input type="text" name="first_name" placeholder="first name..." value="{{ old('first_name') }}" required autofocus>
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" name="last_name" placeholder="last name..." value="{{ old('last_name') }}" required autofocus>
</div>
<div class="form-group">
<label>Email:</label>
<input type="email" name="email" placeholder="email..." value="{{ old('email') }}" required>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" name="password" required>
</div>
<div class="form-group">
<label>Password Confirmation:</label>
<input type="password" name="password_confirmation" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
@if(count($errors))
@foreach ($errors->all() as $error)
<div class="alert alert-danger">
<p>{{ $error }}</p>
</div>
@endforeach
@endif
</div>
</div>
</div>
<!-- End Modal Register area -->
然后我修改了 RegisterController 验证规则和创建方法以包含这个新字段以及迁移文件。 (所以,我在数据库中有文件,那里一切都很好)
注册控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
在您可以看到的表单中,我将其发布到 route('register') 但即使我在提交表单时没有收到任何错误并且被重定向到 / 用户没有保存在数据库中.. 问题是,我不太确定这个默认的身份验证系统是如何工作的..
感谢您的帮助!
我认为提供的代码非常正确。确保您在迁移中删除了 name
字段表单 users
table。
使用您的代码,我能够使用您的模式和控制器注册用户。很新鲜 Laravel 5.4,您的模态被放置在 welcome.blade.php
。还添加了 css/js 个文件和一个显示模态的按钮。
这是我的 up()
更新迁移 users
table
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('name');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
});
}
还用新填充物更新了 User
模型:
protected $fillable = [
'email', 'password', 'first_name', 'last_name'
];