当前位置: 首页 > 知识库问答 >
问题:

Laravel意外错误“类用户包含3个抽象方法…”

呼延辰龙
2023-03-14

在Laravel上编写身份验证应用程序时,我遇到了一个以前从未见过的错误。我已经为这个问题的原因进行了将近一个小时的头脑Storm,但仍然找不到解决办法

错误:

类User包含3个抽象方法,因此必须声明为抽象方法或实现其余方法(illumb\Auth\UserInterface::getRememberToken,illumb\Auth\UserInterface::setRememberToken,illumb\Auth\UserInterface::getRememberTokenName)

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;
}

}

以及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.');
}
}

共有3个答案

况经纬
2023-03-14

这是一个更新问题。Laravel强制我们使用以下代码更新User.php模型以解决此问题。

注意:此更改将使所有现有的“记住我”会话无效,因此所有用户都将被强制重新验证您的应用程序

public function getRememberToken()
{
    return $this->remember_token;   
}

public function setRememberToken($value)
{
    $this->remember_token = $value;
} 

public function getRememberTokenName()
{
    return 'remember_token';
}
暨鹭洋
2023-03-14

我不擅长实现PHP接口,但我相信您需要在User类中包含UserInterfacerementableinterface的所有方法(因为它实现了它们)。否则,该类是“抽象”的,必须这样定义。

据我所知,PHP接口是类必须遵循的一组指导方针。例如,可以为特定的数据库表提供通用接口。它将包括方法的定义,如getRow()插入()deleteRow()更新列()等。然后你可以使用这个接口为不同的数据库类型(MySQL、PostgreSQL、Redis)制作多个不同的类,但是它们都必须遵循接口的规则。这使得迁移变得更容易,因为您知道无论您使用哪个数据库驱动程序从表中检索数据,它总是实现在您的接口中定义的相同方法(换句话说,从类中抽象特定于数据库的逻辑)。

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
}

我认为#2最适合你,因为如果你的类没有实现UserInterfacerementableinterface中的所有方法,你为什么要说它实现了呢。

张银龙
2023-03-14

啊找到了。

它显然记录了Laravel更新。

您可以查看Laravel文档以解决您的问题:

首先,添加一个新的、可为空的、VARCHAR(100)、TEXT或等效的memory_标记到users表中。

接下来,如果您正在使用Eloquent身份验证驱动程序,请使用以下三种方法更新User类:

public function getRememberToken()
{
    return $this->remember_token;
}

public function setRememberToken($value)
{
    $this->remember_token = $value;
}

public function getRememberTokenName()
{
    return 'remember_token';
}

"

详情见http://laravel.com/docs/upgrade。

 类似资料:
  • 我使用的是Laravel4附带的用户类。我试图存储一个属于用户的新问题,用户需要登录才能创建。当我调用questions controller action store时,我得到以下错误 我读过一些关于PHP中抽象方法的文章,虽然我不完全理解它们,但错误本身给出了问题的两种解决方案,声明实现剩余方法的类抽象。我猜测,由于这是laravel附带的模型类,正确的解决方案不是将其声明更改为抽象,而是实现

  • 问题内容: 让抽象类定义实例变量是否是一种好习惯? 然后,子类ExternalJavaScript.class会自动获取源变量,但我认为,如果所有子类本身都定义了源而不是继承,则读取代码会更容易。 你有什么建议? /亚当 问题答案: 我本以为这样的话会更好,因为您要添加一个变量,所以为什么不限制访问并使它更整洁呢?您的吸气器/装夹器应该按照罐子上的说明去做。 再次提到这个问题,您在阅读时是否会费心

  • 问题内容: 可以有一个 实现所有方法 的抽象类-里面没有抽象方法。 例如。: 与拥有与具体类相同的类相比,拥有这样的抽象类(如果有)有什么优势? 我能想到的是,当我将其声明为抽象时,它将不会被实例化。但是,我可以通过将其具体化并将其构造函数设为私有来达到相同的效果。 TIA。 // ================== 编辑:我能想到的另一种用途: 它可能会扩展另一个抽象类或实现一个接口,而不实现

  • 这似乎是一个基本问题。但在采访前需要澄清。 我在抽象类中有一个非抽象方法。它的具体类重写了该方法。但我想调用父类的原始方法来调用,而不是重写方法。有什么办法吗? 据我所知,没有办法调用原始方法?

  • 问题内容: 这是我的代码,假设可以在按下按钮时更改一些文本:- 用下划线标记,它给我一个错误“类必须声明为抽象或实现抽象方法”。该代码大部分是从互联网上复制的,并且可以正常工作。可能仅是Android Studio错误。我如何使它工作? 问题答案: 必须实现该函数,否则您的类应该是抽象的,以便可以在某些子类中实现您的函数。但是在您的情况下,您犯了一个拼写错误。应该代替;

  • 我得到下面的错误,而执行我的测试用例。我已经将TestNg Eclipse插件升级为6.11.0最新版本,并使用所有testng jars文件进行了尝试,没有运气来解决这个问题。 我是否遗漏了任何可以添加项目的内容,或者我该怎么做? 请有人帮助解决此错误: JAVAlang.AbstractMethodError:org。testng。遥远的支持RemoteTestNG6_9_10$Delegat