在 Laravel 中找不到“用户”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15801314/
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
Class 'User' not found in Laravel
提问by emen
I keep getting
我不断得到
Unhandled Exception
Message:
Class 'User' not found
Location:
C:\wamp\www\laravel\laravel\auth\drivers\eloquent.php on line 70
When I'm logging in a user. I don't think there's any problem in eloquent.php. Please take a look at my login controller:
当我登录用户时。我认为 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);
}
}
}
This is the viewof login:
这是登录的视图:
{{ 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() }}
And routes.php:
和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');
});
I used Eloquent as my authentication driver. I've tried changing it to Fluent, but after I click login button, it displays a login error made by this line return Redirect::to('login')->with('login_errors', true);
in the else statement.
我使用 Eloquent 作为我的身份验证驱动程序。我已经尝试将其更改为 Fluent,但是在我单击登录按钮后,它会return Redirect::to('login')->with('login_errors', true);
在 else 语句中显示此行导致的登录错误。
What's wrong with 'User' class when using Eloquent?
使用 Eloquent 时“User”类有什么问题?
回答by Hexodus
Mike's right, this is because you've got no User model but there's more ...
Mike 是对的,这是因为您没有 User 模型,但还有更多……
The line where laravel's searching for the user model and not finding it is the following:
Laravel 搜索用户模型但没有找到它的行如下:
if ( Auth::attempt($user_details) )
This happens because Laravels authentification system uses per default the eloquent driver. To satisfy Laravel you need a database table named 'users' with at least the columns 'username' and 'password' of type text and maybe also the columns 'created_at' and 'updated_at' when using timestamps but you can switch it off.
发生这种情况是因为 Laravel 身份验证系统默认使用 eloquent 驱动程序。为了满足 Laravel,您需要一个名为“users”的数据库表,其中至少包含文本类型的“username”和“password”列,并且在使用时间戳时可能还有“created_at”和“updated_at”列,但您可以将其关闭。
\application\models\user.php
<?PHP
class User extends Eloquent
{
public static $timestamps = false; //I don't like 'em ;)
}
回答by adamellsworth
This is actually a comment to @Hexodus response, but I don't have the required points to comment.
这实际上是对@Hexodus 回复的评论,但我没有评论所需的要点。
You can actually have your User authentication named whatever you want, for instance
例如,您实际上可以将您的用户身份验证命名为您想要的任何名称
- Model:
Turtle
- Table:
turtles
- 模型:
Turtle
- 桌子:
turtles
But you have to go into app\config\auth.php
and change the 'model' => '...'
and 'table' => '...'
values in order for Laravel's authentication to work.
但是您必须进入app\config\auth.php
并更改'model' => '...'
和'table' => '...'
值,以便 Laravel 的身份验证工作。
Also, according to the docs, you don't even need 'username'
or 'password'
explicitly defined as such in your database
此外,根据 docs,您甚至不需要'username'
或'password'
在数据库中明确定义
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
return Redirect::intended('dashboard');
}
Take note that 'email' is not a required option, it is merely used for example. You should use whatever column name corresponds to a "username" in your database. The Redirect::intended function will redirect the user to the URL they were trying to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.
请注意,'email' 不是必需选项,它仅用作示例。您应该使用与数据库中的“用户名”相对应的任何列名。Redirect::intended 函数会将用户重定向到他们在被身份验证过滤器捕获之前尝试访问的 URL。如果预期目的地不可用,则可以为此方法提供后备 URI。
Effectively, 'email'
in this instance is considered a 'username'
实际上,'email'
在这种情况下被认为是'username'
Edit, and because I was having trouble with this at first, you do notneed to hash passwords when you use Auth::attempt(...)
.
编辑,因为我是在第一次有这个麻烦,你不要当您使用需要哈希密码Auth::attempt(...)
。