【问题标题】:Class 'User' not found in Laravel在 Laravel 中找不到类“用户”
【发布时间】:2013-03-25 22:38:29
【问题描述】:

我不断得到

Unhandled Exception
Message:

Class 'User' not found

Location:

C:\wamp\www\laravel\laravel\auth\drivers\eloquent.php on line 70

当我登录用户时。我认为eloquent.php 没有任何问题。请看看我的登录控制器

class Login_Controller extends Base_Controller {

    public $restful = true;

    public function get_index(){
        return View::make('login');
    }

    public function post_index(){

        $username = Input::get('username');
        $password = Input::get('password'); 
        $user_details = array('username' => $username, 'password' => $password);

        if ( Auth::attempt($user_details) )
        {
            return Redirect::to('home.index');
        }
        else
        {
            return Redirect::to('login')
                ->with('login_errors', true);
        }


    }
}

这是登录的视图

{{ Form::open('login') }}
    <!-- username field -->
    <p>{{ Form::label('username', 'Username') }}</p>
    <p>{{ Form::text('username') }}</p>
    <!-- password field -->
    <p>{{ Form::label('password', 'Password') }}</p>
    <p>{{ Form::password('password') }}</p>
    <!-- submit button -->
    <p>{{ Form::submit('Login', array('class' => 'btn btn-primary')) }}</p>
{{ Form::close() }}

还有routes.php

<?php

Route::controller(Controller::detect()); // This line will map all our requests to all the controllers. If the controller or actions don’t exist, the system will return a 404 response.
Route::get('about', 'home@about');


Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::to('login');
});

我使用 Eloquent 作为我的身份验证驱动程序。我尝试将其更改为 Fluent,但是在单击登录按钮后,它会在 else 语句中显示由该行 return Redirect::to('login')-&gt;with('login_errors', true); 造成的登录错误。

使用 Eloquent 时,'User' 类有什么问题?

【问题讨论】:

  • 你的例子。我还没有看到:class User { ... } 所以它没有定义
  • 确实,User 应该是一个模型。请确认您的models 文件夹中有user.php 文件。

标签: php laravel eloquent laravel-3


【解决方案1】:

Mike 是对的,这是因为你没有 User 模型,但还有更多......

laravel 搜索用户模型但没有找到的行如下:

if ( Auth::attempt($user_details) )

这是因为 Laravel 认证系统默认使用 eloquent 驱动程序。为了满足 Laravel,您需要一个名为“users”的数据库表,其中至少包含文本类型的“用户名”和“密码”列,并且在使用时间戳时可能还包含“created_at”和“updated_at”列,但您可以将其关闭。

\application\models\user.php

<?PHP 
class User extends Eloquent
{    
    public static $timestamps  = false; //I don't like 'em ;)
}

【讨论】:

    【解决方案2】:

    这实际上是对@Hexodus 回复的评论,但我没有评论所需的要点。

    您实际上可以将用户身份验证命名为任何您想要的名称,例如

    • 型号:Turtle
    • 表:turtles

    但是您必须进入 app\config\auth.php 并更改 'model' =&gt; '...''table' =&gt; '...' 值才能使 Laravel 的身份验证正常工作。

    另外,according to the docs,您甚至不需要在数据库中明确定义 'username''password'

    if (Auth::attempt(array('email' => $email, 'password' => $password)))
    {
      return Redirect::intended('dashboard');
    }
    

    请注意,“电子邮件”不是必需选项,它仅用作示例。您应该使用与数据库中的“用户名”相对应的任何列名。 Redirect::intended 函数会将用户重定向到他们在被身份验证过滤器捕获之前尝试访问的 URL。如果预期的目的地不可用,则可以为该方法提供一个备用 URI。

    实际上,在这种情况下,'email' 被视为'username'


    编辑,因为我一开始遇到了麻烦,所以当你使用Auth::attempt(...) 时,你不需要对密码进行哈希处理。

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2017-06-04
      • 2016-07-09
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多