問題描述
我正在構建一個至少具有兩級身份驗證的系統,并且兩者在數據庫中都有單獨的用戶模型和表.在 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
.但我希望 CustomerAuth
和 AdminAuth
根據 config/customerauth.php
和 configadminauth.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模板網!