2015-12-22 55 views
0

我是Laravel的新手,我正在使用Web应用程序的教程。教程使用Laravel 4,而我使用Laravel 5.我解决了问题,寄存器,但是当我放下来测试登录以下显示了错误:如何在laravel 5中验证凭据以登录功能?

ErrorException在EloquentUserProvider.php线111:传递照亮\验证参数1 \ EloquentUserProvider :: validateCredentials()必须是在C:\ Users \ Pujan \ Desktop \ projectlaravel \ vendor \ laravel \ framework \ src \ Illuminate \中调用的App \ user的实例Illuminate \ Contracts \ Auth \ Authenticatable,行390上的Auth \ Guard.php并定义。

我不能在这里找出问题。实际上意味着凭证。我知道Laravel 5具有内置登录功能,但无法使用它,所以我尝试了不同的方式,但这个问题超出了我的焦点。 我UserController的是:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 
use View; 

use App\user; 
use Input; 
use App\Http\Requests\YourFormRequest; 
use Auth; 
//use Illuminate\Support\Facades\Auth; 

class UserController extends Controller 
{ 

    public $restful = true; 

    public function index() 
    { 

     return View::make('users.new')->with('title', 'Make it snappy Q&A - Register'); 
    } 


    public function create(YourFormRequest $request) 
    { 
     User::create($request->all()); 

     return redirect('/')->with('message','Thanks for registering!'); 
    } 

    public function getlogin() 
    { 
     // return \Auth::user(); 
     return View::make('users.login')->with('title','Make it snappy Q&A - Login '); 
    } 

    public function createlogin() 
    { 
     $user = array(
      'username'=>Input::get('username'), 
      'password'=>Input::get('password') 
      ); 
     if (Auth::attempt($user)) 
     { 
      return redirect('/')->with('message','You are logged in:'); 
     }else{ 
      return redirect('login') 
      ->with('message','Your username or password are incorrect pls chk it out') 
      ->with_input(); 
     } 
    } 

我有我的路线:

Route::get('/','[email protected]'); 
Route::get('register','[email protected]'); 
Route::get('login','[email protected]'); 
Route::post('register','[email protected]'); 
Route::post('login','[email protected]'); 

我的登录布局工作正常,但是当我尝试登录上面的错误出现。我认为这个错误属于内置的Laravel 5功能,但我无法匹配内置和我创建的登录功能。

+0

您使用的是Laravel 5.0还是5.1?在5.0中,身份验证(登录系统)已经设置,在5.1中您可以按照[官方文档](http://laravel.com/docs/5.1/authentication)设置身份验证系统 – Vikas

+0

您的控制器的第12行应该是'使用App \ User;',不使用App \ user;' – Vikas

回答

1

您看到的错误与您的登录代码无关。您的User实体应该实现Illuminate\Contracts\Auth\Authenticatable接口。接口是一个契约,它列出了类必须具有的方法/函数。类定义应该是这样的:

class User extends Model implements Authenticatable 
{ 

在你的代码的快速浏览,还有其他事情可以做,把它清理干净:

public function createlogin() 
{ 
    if (Auth::attempt(Input::only('username', 'password'))) { 
     return redirect('/')->with('message','You are logged in:'); 
    } 

    return redirect('login') 
     ->with('message','Your username or password are incorrect pls chk it out') 
     ->with_input(); 
} 

而且最后值得注意的是,我d停止使用您正在关注的教程。 Laravel的最新文档包含认证的快速入门指南,有两个版本,beginnnerintermediate

+0

谢谢@Logan Bailey的信息。我正在使用内置的身份验证,但它仍然没有重定向到登录消息的主页,而是显示旧输入的登录页面。如何解决此问题 – anandshrestha57

+0

@ anandshrestha57最有可能表示验证错误。 –

0

@logan提出的是真实的,你应该遵循这一点。

我只想去清理一下你的代码。

无论您何时处理验证,都应该将其视为单独的东西,并且您需要创建FormRequest对象。

请参阅this document了解如何创建表单请求。

然后更新authorizerules方法有以下几点:

/** 
* Authorize the request. 
* 
* @return bool 
*/ 
public function authorize() 
{ 
    return true; 
} 

/** 
* Get the validation rules that apply to the request. 
* 
* @return array 
*/ 
public function rules() 
{ 
    return [ 
     'username' => 'required|exists:users|alpha_dash|min:4', 
     'password' => 'required' 
    ]; 
} 

现在,在您createLogin方法

/** 
* Log in the user. 
* 
* @param \App\Http\Requests\YourFileRequest $request 
* @return \Illuminate\Http\RedirectResponse 
*/ 
public function createlogin(YourFileRequest $request) 
{ 
    $credentials = $request->only('username', 'password'); 

    if (Auth::attempt($credentials)) { 
     return redirect('/')->with('message','You are logged in:'); 
    } 

    return redirect('login') 
     ->with('message', 'Invalid credentials') 
     ->with_input(); 
} 

现在你看,当你6个月后访问你的代码是如何简单,可读现在起 ?它必须是这样的。 Validation单独文件中的逻辑和控制器中的Login逻辑。据我所知,当你通过phpUnit,phpSpec或任何其他正在使用的测试工具进行测试时,这会有很大帮助。

我和洛根也替换了您的if语句块。这是因为,当你从ifelse区块返回某些东西时,应尽可能避免else区块。

只是一个侧面说明:

  1. 您应尽量遵循3个原则,同时编程/编码:SOLIDKISS,并DRY
  2. 只要你完成了那部分代码,请记录下你的代码。这将再次帮助未来的程序员,也为将来的参考知道那段代码正在做什么。

希望这可以帮助你。干杯。

+0

谢谢@ user3514160宝贵的建议。当我坐下来再次测试时,它不会被重定向到您登录的消息的主页: – anandshrestha57

+0

相反,它会保留在同一页面中,而旧的输入没有错误。在哪里我再次错误@ user3514160 – anandshrestha57

+0

请更新与你正在尝试的问题..什么是你得到的错误......还张贴你正在做的测试的代码。 –

0

不知道你是否已经解决了这个问题,但我把我的经验放在这里,也许它可以帮助别人寻找解决这个问题的方法,这个问题恰好与我在一起。

你的App/User模型类应该扩展Illuminate\Foundation\Auth\User而不是Eloquent模型。

因此改变你的用户模型如下:

use Illuminate\Foundation\Auth\User as Authenticatable; 
class User extends Authenticatable 

它确实解决这个同样的错误,我代替

class User extends Model 

。希望这可以帮助。