background

The project consists of several sub-sites that have different functions but share underlying data and logic. It was decided to implement the entire system in a Single Laravel application during development and operational efficiency.

Based on Laravel 5.2, this article mainly introduces how to transform user authentication for multiple sites, in order to maximize the use of Laravel’s own authentication system. However, the default authentication is based on the Email and Password fields. Later, it is possible to add custom fields, such as the transformation scheme of “phone”, which is not involved in this paper.

The specific plan

For clarity, the project is organized into modules for different subsystems. On the basis of Laravel’s original directory structure, the subsystems are given respectively

├── all the exercises, all the exercises, all the exercises, all the exercises, all the exercises site2Copy the code

This section uses Admin as an example. If you need to add other subsystems, make similar changes.

Structure

Run the following commands to generate the default route, controller, and view.

php artisan make:authCopy the code

Copy the default controller and view structure to the sub-module, create the relevant model, migrate the table, modify the routing, and authentication configuration.

In this example, create Admin and Admin directories under APP /Http/Controllers and Resources/Views respectively. All Controllers and views related to the subsystem are placed in these directories.

The final file tree involved is as follows.

├─ │ ├─ ├─ Auth │ ├─ │ ├─ Auth │ ├─ │ ├─ Auth │ ├─ │ ├─ Auth │ ├─ AuthController. PHP │ │ │ │ └ ─ ─ PasswordController. PHP │ │ │ └ ─ ─ HomeController. PHP │ │ └ ─ ─ routes. The PHP (change) │ └ ─ ─ Admin. PHP (new) ├ ─ ─ the config │ └ ─ ─ auth. PHP (change) ├ ─ ─ the database │ └ ─ ─ migrations │ ├ ─ ─ 2014 _10_12_000000_create_admins_table. PHP (new) │ └ ─ ─ 2014 _10_12_100000_create_admins_password_resets_table. PHP (new) └ ─ ─ ├─ ├─ class └─ ├─ class ├─ class ├─ class ├─ class ├─ class └─ Email. Blade. PHP │ └ ─ ─ reset. The blade. The PHP └ ─ ─ the register. The blade. The PHPCopy the code

Config

To create a provider for Admin, use Admin’s Model or table. Create guard for Admin and use the new provider. This guard will then be specified for use in Admin’s authentication Controller.

config/auth.php

'guards' => [ 'admins' => [ 'driver' => 'session', 'provider' => 'admins', ], ], 'Providers' => ['admins' => ['driver' => 'eloquent', 'model' => App\Admin::class,],Copy the code

Router

The purpose here, of course, is for requests to access the Admin subsystem to be forwarded to that subsystem’s Controller. In this example, the system is distinguished by domain names, but other methods could be used.

app/Http/routes.php

Route::group(['domain' => 'admin.example.com', 'namespace' => 'Admin'], function () { Simply specify the public namespace Route::auth(); Route::group(['middleware' => ['auth']], function () {Route::get('/', 'HomeController@index'); }); });Copy the code

View

By default, the system uses blades under Resource/View/Auth. We need to use different styles for different users, so we need to change these default paths. Note the following two forms of code:

  1. View (‘auth. initially, this form is only used in Laravel framework files.

    vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php:37:        return view('auth.login');
    vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php:33:        return view('auth.register');
    vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php:49:            return view('auth.passwords.email');
    vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php:52:        return view('auth.password');
    vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php:194:            return view('auth.passwords.reset')->with(compact('token', 'email'));
    vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php:197:        return view('auth.reset')->with(compact('token', 'email'));Copy the code

    These are all used by App\Http\Controllers\ AuthController in the form of traits. $this->loginView (); $this->loginView (); $this->loginView (); So we just need to inherit the default AuthController and specify these properties. (Refer to the Controller section for details)

    vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

    public function showLoginForm()
    {
       $view = property_exists($this, 'loginView')
                   ? $this->loginView : 'auth.authenticate';
    
       if (view()->exists($view)) {
           return view($view);
       }
    
       return view('auth.login');
    }Copy the code
  2. @include(‘auth. initially, this form is not used, but if you use acacha/adminlte-laravel, it will be used in resources/views/auth template files. Anyway, we’ll change it to @include(‘admin.auth. Can.

Migration & Model

After all, it is a new subsystem, refer to the built-in users/password_resets table and model can be created.

Controller

After copying the structure from the original app/Http/Controllers to Admin, modify the namespace and then modify it for Admin.

The following uses AuthController as an example. PasswordController is similar:

app/Http/Controllers/Admin/Auth/AuthController.php

use App\Http\Controllers\Auth\AuthController as BaseAuthController; Class AuthController extends BaseAuthController extends BaseAuthController to make things simpler. Private */ protected $guard = 'admins'; // Specify guard protected $loginView = 'admin.auth.login' in config/auth.php; $registerView = 'admin.auth.register'; {return validator ::make($data, $data, $data); [/ / according to the information needed for the Admin change validation configuration 'name' = > 'required | Max: 255', 'email' = > 'required | email | Max: 255 | unique: admins', / / use the admins' password '= >' required | confirmed | min: 6 ', 'terms' = >' required ']); } protected function create(array $data) {return Admin::create([// use custom Model(Admin) 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }}Copy the code

reference

Throughout the transformation process, the main reference to the following sources of information, thank the authors at the same time also released reference.