Can I create a new class that inherits from the user in Laravel 5.2?

I am new to laravel (using 5.2, which is the latest version to date), so I have the following dilemma: I know that Laravel comes with the class Userright out of the box, but I want to develop a system in which I can have two more type of users called Researcherand Admin.

My main need is to create completely different user classes (Researcher and Admin), possibly inherited from User, because the business logic is almost 100% different from them, and I would not want to create a column in the database to classify the user type. In addition, there are not many fields that overlap between stock classes User, Adminand Researcher.

My main question is: will everything still work the same way (Auth Controller, middleware, etc.) if I inherit from Userfor my other 2 classes? I know the principles of OOP and intuition. I believe that 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.

: , User :

<?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 JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

User, 2 (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 .

. , . laravel.

!

+4
2
0

, 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

User, Laravel Illuminate\Contracts\Auth\Authenticatable , User

, :

Illuminate/Auth/SessionGuard.php

auth, , auth Illuminate\Contracts\Auth\Authenticatable, ..

/**
 * 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
} 

, : Researcher Admin User ?

Eloquent , Laravel Illuminate\Auth\EloquentUserProvider . , User, ( ), ,

0

Source: https://habr.com/ru/post/1623251/


All Articles