1

我有一个基本的 laravel 4 应用程序,允许某人注册然后登录。我正在努力做到这一点,以便当用户成功完成注册时,他们会自动登录。我收到错误异常“传递给 Illuminate\Auth\Guard::login() 的参数 1 必须是 Illuminate\Auth\UserInterface 的实例,用户给定的实例”。我知道这意味着传递给登录方法的第一个参数不正确,但我不明白为什么当laravel 文档说要使用时它不正确

$user = User::find(1);

Auth::login($user);

这是我的控制器

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

    }
?>
4

2 回答 2

5

我能想到几个场景:

  1. 您没有使用User全新 Laravel 安装随附的模型(听起来不太可能,但该模型实现UserInterface了,如果您已对其进行编辑或创建新模型,则您的模型可能不会)。
  2. User::create()未成功调用(未成功创建用户)
  3. $user = User::where()->...没有产生结果

尝试:

$user = User::create(array(
            'username'=>Input::get('username'),
            'password'=>Hash::make(Input::get('password'))
        ));

Auth::login($user);

如果您仍然收到错误,则很可能$user不是User对象,因为用户未成功创建。

于 2013-09-11T23:54:27.047 回答
4

make sure your User.php begins like..

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
于 2014-02-06T20:33:37.403 回答