久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 5 實現多個Auth驅動

Laravel 5 Implement multiple Auth drivers(Laravel 5 實現多個Auth驅動)
本文介紹了Laravel 5 實現多個Auth驅動的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在構建一個至少具有兩級身份驗證的系統,并且兩者在數據庫中都有單獨的用戶模型和表.在 google 上快速搜索,目前唯一的解決方案是使用 MultiAuth 包,該包在 Auth 上硬塞多個驅動程序.

I am building a system with at least two levels of Authentication and both have separate User models and tables in the database. A quick search on google and the only solution thus far is with a MultiAuth package that shoehorns multiple drivers on Auth.

我正在嘗試刪除相當簡單的 Auth.但我希望 CustomerAuthAdminAuth 根據 config/customerauth.phpconfigadminauth.php<使用單獨的配置文件/代碼>

I am attempting to remove Auth which is fairly straight-forward. But I would like CustomerAuth and AdminAuth using a separate config file as per config/customerauth.php and configadminauth.php

推薦答案

解決方案

我假設您有一個可以處理的包.在此示例中,我的供應商命名空間將簡單地為:Example - 可以按照說明找到所有代碼片段.

Solution

I'm assuming you have a package available to work on. My vendor namespace in this example will simply be: Example - all code snippets can be found following the instructions.

我將 config/auth.php 復制到 config/customerauth.php 并相應地修改了設置.

I copied config/auth.php to config/customerauth.php and amended the settings accordingly.

我編輯了 config/app.php 并將 IlluminateAuthAuthServiceProvider 替換為 ExampleAuthCustomerAuthServiceProvider.

I edited the config/app.php and replaced the IlluminateAuthAuthServiceProvider with ExampleAuthCustomerAuthServiceProvider.

我編輯了 config/app.php 并將 Auth 別名替換為:

I edited the config/app.php and replaced the Auth alias with:

'CustomerAuth' => 'ExampleSupportFacadesCustomerAuth',

然后我在包中實現了代碼,例如 vendor/example/src/.我從 ServiceProvider 開始:Example/Auth/CustomerAuthServiceProvider.php

I then implemented the code within the package for example vendor/example/src/. I started with the ServiceProvider: Example/Auth/CustomerAuthServiceProvider.php

<?php namespace ExampleAuth;

use IlluminateAuthAuthServiceProvider;
use ExampleAuthCustomerAuthManager;
use ExampleAuthSiteGuard;

class CustomerAuthServiceProvider extends AuthServiceProvider
{
    public function register()
    {
        $this->app->alias('customerauth',        'ExampleAuthCustomerAuthManager');
        $this->app->alias('customerauth.driver', 'ExampleAuthSiteGuard');
        $this->app->alias('customerauth.driver', 'ExampleContractsAuthSiteGuard');

        parent::register();
    }

    protected function registerAuthenticator()
    {
        $this->app->singleton('customerauth', function ($app) {
            $app['customerauth.loaded'] = true;

            return new CustomerAuthManager($app);
        });

        $this->app->singleton('customerauth.driver', function ($app) {
            return $app['customerauth']->driver();
        });
    }

    protected function registerUserResolver()
    {
        $this->app->bind('IlluminateContractsAuthAuthenticatable', function ($app) {
            return $app['customerauth']->user();
        });
    }

    protected function registerRequestRebindHandler()
    {
        $this->app->rebinding('request', function ($app, $request) {
            $request->setUserResolver(function() use ($app) {
                return $app['customerauth']->user();
            });
        });
    }
}

然后我實現了:Example/Auth/CustomerAuthManager.php

<?php namespace ExampleAuth;

use IlluminateAuthAuthManager;
use IlluminateAuthEloquentUserProvider;
use ExampleAuthSiteGuard as Guard;

class CustomerAuthManager extends AuthManager
{
    protected function callCustomCreator($driver)
    {
        $custom = parent::callCustomCreator($driver);

        if ($custom instanceof Guard) return $custom;

        return new Guard($custom, $this->app['session.store']);
    }

    public function createDatabaseDriver()
    {
        $provider = $this->createDatabaseProvider();

        return new Guard($provider, $this->app['session.store']);
    }

    protected function createDatabaseProvider()
    {
        $connection = $this->app['db']->connection();
        $table = $this->app['config']['customerauth.table'];

        return new DatabaseUserProvider($connection, $this->app['hash'], $table);
    }

    public function createEloquentDriver()
    {
        $provider = $this->createEloquentProvider();

        return new Guard($provider, $this->app['session.store']);
    }

    protected function createEloquentProvider()
    {
        $model = $this->app['config']['customerauth.model'];

        return new EloquentUserProvider($this->app['hash'], $model);
    }

    public function getDefaultDriver()
    {
        return $this->app['config']['customerauth.driver'];
    }

    public function setDefaultDriver($name)
    {
        $this->app['config']['customerauth.driver'] = $name;
    }
}

然后我實現了 Example/Auth/SiteGuard.php(注意實現的方法有一個額外的 site_ 定義,這對于其他 Auth 驅動程序應該是不同的):

I then implemented Example/Auth/SiteGuard.php (note the methods implemented have an additional site_ defined, this should be different for other Auth drivers):

<?php namespace ExampleAuth;

use IlluminateAuthGuard;

class SiteGuard extends Guard
{
    public function getName()
    {
        return 'login_site_'.md5(get_class($this));
    }

    public function getRecallerName()
    {
        return 'remember_site_'.md5(get_class($this));
    }
}

然后我實現了Example/Contracts/Auth/SiteGuard.php

use IlluminateContractsAuthGuard;

interface SiteGuard extends Guard {}

最后我實現了 Facade;Example/Support/Facades/Auth/CustomerAuth.php

Finally I implemented the Facade; Example/Support/Facades/Auth/CustomerAuth.php

<?php namespace ExampleSupportFacades;

class CustomerAuth extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'customerauth';
    }
}

<小時>

快速更新,當嘗試使用帶有 phpunit 你可能會得到以下錯誤:


A quick update, when trying to use these custom auth drivers with phpunit you may get the following error:

Driver [CustomerAuth] not supported.

您還需要實現這一點,最簡單的解決方案是覆蓋 be 方法并創建一個與其類似的 trait:

You also need to implement this, the easiest solution is override the be method and also creating a trait similar to it:

<?php namespace ExampleVendorTesting;

use IlluminateContractsAuthAuthenticatable as UserContract;

trait ApplicationTrait
{
    public function be(UserContract $user, $driver = null)
    {
        $this->app['customerauth']->driver($driver)->setUser($user);
    }
}

這篇關于Laravel 5 實現多個Auth驅動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 成人欧美一区二区三区白人 | 先锋资源网站 | 午夜精品影院 | 国产一区二区三区在线 | 亚洲免费成人 | 日韩在线视频观看 | 日本欧美在线观看视频 | 成人午夜视频在线观看 | 精品一区久久 | 国产精品久久久久免费 | 国产精品久久久久久久免费大片 | 精品国产1区2区3区 在线国产视频 | 国产视频在线观看一区二区三区 | 久久久高清 | 国产精品中文字幕在线观看 | 九九热在线免费视频 | 91精品国产色综合久久 | 国产999在线观看 | 正在播放国产精品 | 综合色婷婷 | 国产精品免费看 | 精品久久久久久亚洲精品 | 国产伦精品一区二区三区视频金莲 | 国产成人在线视频播放 | 一级片av | 日韩成人精品在线观看 | 亚洲综合色网 | 亚洲精品久久久久久久久久吃药 | 日韩毛片播放 | a级毛片毛片免费观看久潮喷 | av中文字幕在线观看 | 成人福利电影 | 56pao在线 | 伊人91在线 | 96国产精品久久久久aⅴ四区 | 99日韩| 久久久久久久久久久久91 | 亚洲午夜视频在线观看 | 91精品一区二区三区久久久久 | 中文字幕av亚洲精品一部二部 | 婷婷亚洲综合 |