我可以在Laravel 5.2中创建一个从User继承的新类吗? [英] Can I create a new class that inherits from User in Laravel 5.2?

查看:84
本文介绍了我可以在Laravel 5.2中创建一个从User继承的新类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对laravel非常陌生(使用5.2是迄今为止的最新版本),因此我面临以下难题:我知道Laravel附带了一个User类,但我想开发我可以拥有另外两种类型的用户的系统,分别称为ResearcherAdmin.

I am pretty new to laravel (using 5.2 which is the latest version to date), therefore I have the following dilemma: I know that Laravel comes with a User class right out of the box, but I want to develop a system where I can have another two types of users called Researcher and Admin.

我主要需要创建完全不同的用户类(Researcher和Admin),可能从User继承,因为它们之间的业务逻辑几乎有100%的差异,并且我不想在数据库中创建列来进行分类用户类型.此外,股票UserAdminResearcher类之间没有太多重叠的字段.

My main need to create completely different classes of users (Researcher and Admin), possibly inheriting from User because the business logic is almost 100% different amongst them and I would not want to create a column in the database to classify the type of user. Furthermore, there aren't many fields that overlap between the stock User, Admin and Researcher classes.

我的主要问题是:如果我从其他两个类的User继承,一切是否仍将正常工作(启用身份验证控制器,启用中间件等)?我知道OOP的原理,并且凭直觉,我认为如果执行以下操作应该没问题:

My main question would be: Will everything still work the same (Auth Controller, middleware enabled, etc...) if I inherit from User for my other 2 classes? I know the principles of OOP and by intuition I assume I should be fine if I do the following:

//'Stock' User class:
//
class User extends Authenticatable{
    //Any overlapping logic between Researcher and Admin.
}


class Researcher extends User{
    //My class definition here.
}

class Admin extends User{
    //My class definition here.
}

然后使用两个类,就像我通常使用类User的实例一样.通过这种方式,我的意思是使用所有方法,并立即使用User功能.

And then use both classes as I would normally use an instance of the class User. By that, I mean use all the methods and right out of the box User functionality.

问题扩展: 在写我的问题时,我意识到默认情况下,类User看起来像这样:

QUESTION EXTENSION: While writing my question, I realize that the class User looks like so by default:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

我不能只是做以下事情,而不是从User继承来创建我的其他2个类(Researcher& Admin):

Could not I just do the following instead of inheriting from User to create my other 2 classes (Researcher & Admin):

//Researcher Class:
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Researcher extends Authenticatable
{
    //Class definition...
}

...

//Admin Class:
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    //Class definition
}

在这种方法中,我试图模仿" User类.问题是我不知道内核中是否有任何逻辑经过硬编码并被隐式引用到User类.

In this approach, I am trying to 'mimic' the User class. The thing is that I don't know if there is any logic in the Kernel that is hardcoded and is being referenced to the User class implicitly.

任何想法和帮助将不胜感激.抱歉,这对您来说很愚蠢.我刚刚开始使用laravel框架.

Any thought and help will be much appreciated. Sorry if this is silly to you. I am just getting started with the laravel framework.

干杯!

推荐答案

是的,您可以扩展类Illuminate\Foundation\Auth\User:

use Illuminate\Foundation\Auth\User as Authenticatable;

class Researcher extends Authenticatable {
    //My class definition here.
}

如果您查看User类:

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
}

您会看到它扩展了Model类并实现了用于身份验证处理的所有其他有用接口

you see that it extends the Model class and implements all the other useful interfaces for authentication handling

您可以安全地扩展User类,因为Laravel足以与Illuminate\Contracts\Auth\Authenticatable接口一起使用来处理身份验证,而不是直接与User

you can safely extend the User class, because Laravel is good enough to work with the Illuminate\Contracts\Auth\Authenticatable interface to handle the authentication and not directly with the User class

实际上,如果您签入:

Illuminate/Auth/SessionGuard.php

这是auth处理的主要类,您会看到所有auth操作都是针对Illuminate\Contracts\Auth\Authenticatable接口执行的,即:

That is the main class for auth handling, you'll see that all the auth actions are made against the Illuminate\Contracts\Auth\Authenticatable interface, i.e:

/**
 * Log a user into the application.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  bool  $remember
 * @return void
 */
public function login(AuthenticatableContract $user, $remember = false)
{
    //other code
} 

我认为您的真正问题将是:如何在认证过程中实例化类ResearcherAdmin而不是User类?

I Think that your real problem would be: how to instantiate the classes Researcher and Admin instead of the User class during the authentication process?

如果您使用Eloquent作为身份验证驱动程序,则默认情况下,Laravel将使用Illuminate\Auth\EloquentUserProvider创建Model的实例.因此,如果要创建一个实例而不是User的实例,则应覆盖此提供程序(或创建自己的实例),然后在此处可以选择要实例化的类

If you use Eloquent as authentication driver, by default Laravel is going to use the Illuminate\Auth\EloquentUserProvider to create an instance of the Model. So, if you want to create an instance one of your classes instead of User, you should override this provider (or create one of your own) and here you could choose which class to instantiate

这篇关于我可以在Laravel 5.2中创建一个从User继承的新类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆