2015-09-02 61 views
1

我对来自CI的Laravel和PHP 5.4很陌生。请原谅这个关于非常基本认证的愚蠢问题。Laravel 5.1基本身份验证(注册)不起作用

我是从the docs开始执行登录/注册(身份验证)。

我的迁移到位:

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\[email protected]'); 
Route::post('admin/login', 'Auth\[email protected]'); 
Route::get('admin/register', 'Auth\[email protected]'); 
Route::post('admin/register', 'Auth\[email protected]'); 

我的观点是否正确显示:

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

与形式:

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

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

我错过了什么?

编辑

我不认为我需要添加一个postRegister(),因为它是由AuthenticatesAndRegistersUsers使用的RegistersUsers性状由AuthController使用已定义。

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

+0

您检查应用程序日志?你可以在'storage/logs/laravel.log'中检查应用程序日志,确保你已经设置了error_reporting(E_ALL),并且检查服务器日志 –

+0

考虑到你发布的方法,你应该添加你的'postRegister()'方法。 。或者你在使用默认值? 此外,请检查您的浏览器开发人员工具的网络选项卡,是否应告诉您从帖子请求中得到的回复,这应该暗示您为什么被重定向。 – Jeemusu

+0

@GaneshGhalame我清除了laravel.log并重新提交了表单。日志中没有任何内容。 – Obay

回答

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 
0

您应该添加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。我会把它作为你的练习。

+2

但'postRegister()'已经在使用特征'AuthenticatesAndRegistersUsers'的特征'RegistersUsers'中定义,我在'AuthController.php'中使用''' – Obay

0

通过基本安装,注册时将运行验证规则。没有更多的错误信息,这有点令人困惑。

它在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个或更多字符:)