Laravel 4 arg1 在使用 Auth::login 时必须是 UserInterface 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18752840/
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
Laravel 4 arg1 must be an instance of UserInterface when using Auth::login
提问by Mark
I have a basic laravel 4 app that allows someone to register and then login. I am trying to make it so that when a user completes their registration successfully they are logged in automatically. I get an error exception 'Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Auth\UserInterface, instance of User given'. I understand that this means that the first argument being passed to the login method is not correct but I don't understand why it is not correct when the laravel documentationsays to use
我有一个基本的 laravel 4 应用程序,它允许某人注册然后登录。我试图做到这一点,当用户成功完成注册时,他们会自动登录。我收到错误异常“传递给 Illuminate\Auth\Guard::login() 的参数 1 必须是 Illuminate\Auth\UserInterface 的实例,给定的用户实例”。我知道这意味着传递给 login 方法的第一个参数不正确,但我不明白为什么当laravel 文档说要使用时它不正确
$user = User::find(1);
Auth::login($user);
Here is my controller
这是我的控制器
<?php
Class UsersController extends BaseController {
public $restful = 'true';
protected $layout = 'layouts.default';
public function post_create()
{
$validation = User::validate(Input::all());
if ($validation->passes()) {
User::create(array(
'username'=>Input::get('username'),
'password'=>Hash::make(Input::get('password'))
));
$user = User::where('username', '=', Input::get('username'))->first();
Auth::login($user);
return Redirect::Route('home')->with('message', 'Thanks for registering! You are now logged in!');
}
else {
return Redirect::Route('register')->withErrors($validation)->withInput();
}
}
}
?>
回答by fideloper
There's a few scenarios I can think of:
我能想到的有几个场景:
- You're not using the
User
model which comes with a fresh Laravel install (sounds unlikely, but that one implementsUserInterface
, it's possible yours does not if you've edited it or created a new one). User::create()
isn't successfully being called (isn't created a user successfully)$user = User::where()->...
isn't resulting in a result
- 您没有使用
User
新安装的 Laravel 附带的模型(听起来不太可能,但那个实现了UserInterface
,如果您编辑过它或创建了一个新的,则您的模型可能不会使用)。 User::create()
未成功调用(未成功创建用户)$user = User::where()->...
没有导致结果
Try:
尝试:
$user = User::create(array(
'username'=>Input::get('username'),
'password'=>Hash::make(Input::get('password'))
));
Auth::login($user);
If you still get errors, it's likely that $user
isn't a User
object because the user wasn't created successfully.
如果您仍然遇到错误,那很可能$user
不是User
对象,因为用户未成功创建。
回答by ibex
make sure your User.php begins like..
确保你的 User.php 开始像..
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {