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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32344282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:17:12  来源:igfitidea点击:

Laravel 5.1 basic authentication (register) does not work

phplaravellaravel-5.1

提问by Obay

I'm very new to Laravel and PHP 5.4, coming from CI. Please forgive this stupid question about very basic authentication.

我是来自 CI 的 Laravel 和 PHP 5.4 的新手。请原谅这个关于非常基本的身份验证的愚蠢问题。

I am beginning by implementing login/registration (authentication), following the docs.

我首先按照docs实施登录/注册(身份验证)。

My migration is in place:

我的迁移已经到位:

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();
    ...
});

Here's my config\auth.php:

这是我的config\auth.php

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

app\User.php:

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:

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:

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');

My views are displaying properly:

我的观点显示正确:

resources/views/admin/login.blade.php
resources/views/admin/register.blade.php

with forms:

与形式:

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

This is as far as I get. The Register form, when submitted, just redirects to itself, no errors. No entries are created in the usersdatabase table.

这是我所得到的。注册表单在提交时,只会重定向到自身,没有错误。在users数据库表中不创建任何条目。

What am I missing?

我错过了什么?

EDIT

编辑

I don't think I need to add a postRegister()because it's already defined in the RegistersUserstrait used by AuthenticatesAndRegistersUsersused by AuthController.

我认为我不需要添加 apostRegister()因为它已经在RegistersUsersused byAuthenticatesAndRegistersUsers使用的特征中定义AuthController

Also, not sure if this helps but I have 2 virtual hosts pointing to the same project directory.

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

回答by Rudyar Cortés

It should be definitively a validation error.

它绝对应该是一个验证错误。

Put this code in your register view and try again:

将此代码放在您的注册视图中并重试:

<!--  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

回答by samlev

You should add postRegisterto your AuthController:

您应该添加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());
}

Also note that the validatorfunction isn't really required. In 5.1, a Controllerobject has the validatemethod, which will return failures automatically.

另请注意,该validator功能并不是真正需要的。在 5.1 中,Controller对象具有validate方法,该方法会自动返回失败。

Oh, and you should probably add postLogin, too. I'll leave that as an exercise for you.

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

回答by Milkmannetje

With a basic installation, a validation rule is running when registering. With no further error messages, it's a little confusing.

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

It's the password validation rule in AuthController.php, that wants a password length of minimum 6 characters:

这是 中的密码验证规则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',
    ]);
}

So just make sure you enter 6 or more characters :)

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