Laravel 5.1基本身份验证(注册)不起作用


Laravel 5.1 basic authentication (register) does not work

我对Laravel和PHP 5.4非常陌生,来自CI。请原谅我这个关于基本认证的愚蠢问题。

我从实现登录/注册(身份验证)开始,遵循文档。

我的迁移已经到位:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->string('first_name');
    $table->string('last_name');
    $table->rememberToken();
    ...
});

我的 config'auth.php :

'driver' => 'eloquent',
'model' => App'User::class,
'table' => 'users',
'password' => [
    'email' => 'emails.password',
    'table' => 'password_resets',
    'expire' => 60,
],

app'User.php :

protected $table = 'users';
protected $fillable = ['first_name', 'last_name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];

app'Http'Controllers'Auth'AuthController.php :

protected function validator(array $data) {
    return Validator::make($data, [
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}
protected function create(array $data) {
   return User::create([
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
public function getLogin() {
    return view('admin/login');
}
public function getRegister() {
    return view('admin/register');
}

app'Http'routes.php :

Route::get('admin/login', 'Auth'AuthController@getLogin');
Route::post('admin/login', 'Auth'AuthController@postLogin');
Route::get('admin/register', 'Auth'AuthController@getRegister');
Route::post('admin/register', 'Auth'AuthController@postRegister');

我的视图显示正常:

resources/views/admin/login.blade.php
resources/views/admin/register.blade.php
与形式:

<form action="/admin/login" method="POST">
<form action="/admin/register" method="POST">

我只能到这里了。Register表单在提交时,只是重定向到自身,没有错误。没有在users数据库表中创建任何条目。

我错过了什么?

编辑

我不认为我需要添加一个postRegister(),因为它已经在AuthController使用的AuthenticatesAndRegistersUsers使用的RegistersUsers特征中定义了。

另外,不确定这是否有帮助,但我有2个虚拟主机指向相同的项目目录。

肯定是验证错误。

把这段代码放到你的注册视图中,然后再试一次:

<!--  Error handle -->
        @if($errors->any())
        <div class="row collapse">
            <ul class="alert-box warning radius">
                @foreach($errors->all() as $error)
                    <li> {{ $error }} </li>
                @endforeach
            </ul>
        </div>
        @endif

你应该添加postRegister到你的AuthController:

public function postRegister(Request $request) {
    $this->validate($request, [
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
    return $this->create($request->input());
}

还要注意,validator函数并不是真正需要的。在5.1中,Controller对象具有validate方法,该方法将自动返回失败。

哦,你可能也应该加上postLogin。我把它留给你们作为练习

对于基本安装,注册时将运行验证规则。如果没有进一步的错误消息,这有点令人困惑。

这是AuthController.php中的密码验证规则,希望密码长度至少为6个字符:

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

请确保输入6个或更多字符:)