Laravel 5 Implement multiple Auth drivers(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
.但我希望 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:
然后我在包中实现了代码,例如 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
然后我实现了:Example/Auth/CustomerAuthManager.php
然后我实现了 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):
然后我实现了Example/Contracts/Auth/SiteGuard.php
最后我实现了 Facade;Example/Support/Facades/Auth/CustomerAuth.php
Finally I implemented the Facade; Example/Support/Facades/Auth/CustomerAuth.php
<小时>
快速更新,当尝试使用带有 phpunit 你可能会得到以下错误:
A quick update, when trying to use these custom auth drivers with phpunit you may get the following error:
您还需要实现这一点,最简单的解决方案是覆盖 be
方法并创建一个与其类似的 trait
:
You also need to implement this, the easiest solution is override the be
method and also creating a trait
similar to it:
这篇关于Laravel 5 实现多个Auth驱动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!