php Laravel 意外错误“类用户包含 3 个抽象方法...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23094374/
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 unexpected error "class user contains 3 abstract methods..."
提问by Thanos Paravantis
While programming my Authentication app on Laravel, I came across to an error I've never seen before. I've been brainstorming for almost an hour for the cause of this problem but yet I can't find a solution.
在 Laravel 上编写我的身份验证应用程序时,我遇到了一个我以前从未见过的错误。我已经为这个问题的原因集思广益了将近一个小时,但我找不到解决方案。
Error:
错误:
Class User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Auth\UserInterface::getRememberToken, Illuminate\Auth\UserInterface::setRememberToken, Illuminate\Auth\UserInterface::getRememberTokenName)
类 User 包含 3 个抽象方法,因此必须声明为抽象方法或实现其余方法(Illuminate\Auth\UserInterface::getRememberToken、Illuminate\Auth\UserInterface::setRememberToken、Illuminate\Auth\UserInterface::getRememberTokenName)
User.php Model:
User.php 模型:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = [
"email",
"username",
"password",
"password_temp",
"code",
"active",
"created_at",
"updated_at",
"banned"
];
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
And the RegisterController.php
和 RegisterController.php
<?php
class RegisterController extends BaseController {
public function getRegister()
{
return View::make('template.home.register');
}
public function postRegister()
{
$rules = [
"email" => "required|email|max:50|unique:users",
"username" => "required|max:50|min:5|unique:users",
"password" => "required|max:50|min:6",
"password_again"=> "required|same:password",
];
$messages = ["required" => "This field is required." ];
$validator = Validator::make(Input::all(), $rules, $messages);
if($validator->fails())
{
return Redirect::route('register')->withErrors($validator)->withInput();
} else {
$email = Input::get('email');
$username = Input::get('username');
$password = Input::get('password');
$code = str_random(60);
$user = User::create([
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => $code,
'activated' => 0,
'banned' => 0
]);
if ($user)
{
Mail::send('template.email.activate', ['link' => URL::route('activate', $code), 'username' => $username], function($message) use ($user)
{
$message->to($user->email, $user->username)->subject('Account Registration');
});
return Redirect::route('register')->with('homeError', 'There was a problem creating your account.');
}
}
return Redirect::route('register')->with('homeError', 'Account could not be created.');
}
}
回答by Matija Milkovi?
Ah found it.
啊找到了。
Its apparently documented Laravel Update.
其显然记录在案的 Laravel 更新。
You can check Laravel docs to fix your issues:
您可以查看 Laravel 文档来解决您的问题:
"First, add a new, nullable remember_token of VARCHAR(100), TEXT, or equivalent to your users table.
“首先,添加一个新的、可为空的 remember_token 的 VARCHAR(100)、TEXT 或等效于您的用户表。
Next, if you are using the Eloquent authentication driver, update your User class with the following three methods:
接下来,如果您使用 Eloquent 身份验证驱动程序,请使用以下三种方法更新您的 User 类:
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
"
”
See http://laravel.com/docs/upgradefor further details.
有关更多详细信息,请参阅http://laravel.com/docs/upgrade。
回答by Sam
I'm not a pro at implementing PHP Interfaces, but I believe you need to include all methods of UserInterface
and RemindableInterface
in your User
class (since it implements them). Otherwise, the class is "abstract"and must be defined as such.
我不是在实施亲PHP接口,但我相信你需要包括的所有方法UserInterface
,并RemindableInterface
在你的User
类(因为它实现了它们)。否则,该类是“抽象的”,必须如此定义。
By my knowledge, a PHP interface is a set of guidelines that a class must follow. For instance, you can have a general interface for a specific database table. It would include definition of methods like getRow()
, insertRow()
, deleteRow()
, updateColumn()
, etc. Then you can use this interface to make multiple different classes for different database types (MySQL, PostgreSQL, Redis), but they must all follow the rules of the interface. This makes migration easier, since you know no matter which database driver you are using to retrieve data from a table it will always implement the same methods defined in your interface (in other words, abstracting the database-specific logic from the class).
据我所知,PHP 接口是类必须遵循的一组准则。例如,您可以有一个特定数据库表的通用接口。它会包括定义诸如getRow()
, insertRow()
, deleteRow()
,updateColumn()
等方法。然后您可以使用该接口为不同的数据库类型(MySQL、PostgreSQL、Redis)创建多个不同的类,但它们都必须遵循接口的规则。这使得迁移更容易,因为您知道无论您使用哪个数据库驱动程序从表中检索数据,它都将始终实现在您的接口中定义的相同方法(换句话说,从类中抽象出特定于数据库的逻辑)。
3 possible fixes, as far as I know:
据我所知,有 3 种可能的修复方法:
abstract class User extends Eloquent implements UserInterface, RemindableInterface
{
}
class User extends Eloquent
{
}
class User extends Eloquent implements UserInterface, RemindableInterface
{
// include all methods from UserInterFace and RemindableInterface
}
I think #2 is best for you, since if your class doesn't implement all methods from UserInterface
and RemindableInterface
why would you need to say it does.
我认为#2是最适合你的,因为如果你的类没有实现所有方法UserInterface
和RemindableInterface
为什么你需要说它。
回答by Bastin Robin
This is an update problem. Laravel force us to update the User.php model with the following code to fix this problem.
这是更新问题。Laravel 强制我们使用以下代码更新 User.php 模型以解决此问题。
Note: All existing "remember me" sessions will be invalidated by this change, so all users will be forced to re-authenticate with your application.
注意:所有现有的“记住我”会话都将因此更改而失效,因此所有用户都将被迫使用您的应用程序重新进行身份验证。
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return 'remember_token';
}
回答by sqren
I fixed the error by adding UserTrait
to User.php
. Get the original User.php
file here.
If you don't need the rememberMe
stuff, you just need this:
我通过添加UserTrait
到User.php
. 在此处获取原始User.php
文件。如果你不需要这些东西,你只需要这个:rememberMe
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
class User extends Eloquent implements UserInterface {
use UserTrait;
}
回答by Ironheartbj18
"First, add a new, nullable remember_token of VARCHAR(100), TEXT, or equivalent to your users table. which is put like this $table->text('remember_token', 100)->nullable();
into the table then php artisan migrate. and update to your Elq model too.
“首先,添加一个新的、可为空的 remember_token 的 VARCHAR(100)、TEXT 或等效于您的用户表。像这样$table->text('remember_token', 100)->nullable();
将其放入表中,然后 php artisan migrate。并更新到您的 Elq 模型。
回答by luiggi
Class User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods
类 User 包含 6 个抽象方法,因此必须声明为抽象方法或实现其余方法
(Illuminate\Auth\UserInterface::getAuthIdentifier,
(Illuminate\Auth\UserInterface::getAuthIdentifier,
Illuminate\Auth\UserInterface::getAuthPassword,
Illuminate\Auth\UserInterface::getAuthPassword,
Illuminate\Auth\UserInterface::getRememberToken, ...)
Illuminate\Auth\UserInterface::getRememberToken, ...)
回答by Fabien Munoz
Class User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods
类 User 包含 6 个抽象方法,因此必须声明为抽象方法或实现其余方法
(Illuminate\Auth\UserInterface::getAuthIdentifier,
Illuminate\Auth\UserInterface::getAuthPassword,
Illuminate\Auth\UserInterface::getRememberToken,
...
)
Solution:You add this code in User class (it's working for me)
解决方案:您在 User 类中添加此代码(它对我有用)
use Authenticatable, CanResetPassword;
Source: GitHub
来源:GitHub
回答by rocketz
I also get this error.then i added:
我也收到此错误。然后我补充说:
getRememberToken()
setRememberToken($value)
getRememberTokenName()
here after code is working
代码工作后在这里