对于我的 laravel 5 应用程序,我希望同时拥有管理员用户和客户端。过去,我对两者都使用了用户表,并有一个标志,表明它是哪种类型的用户 - 带有一个用于更多客户端信息的连接表。
我发现这种方法相当困惑......并且认为最好同时拥有用户表和模型以及客户表和模型。
这导致了我的问题,诸如身份验证密码重置、身份验证尝试之类的事情是针对用户表的。是否有可能使其也适用于客户端表。
这可能吗? (我已标记 laravel 4,因为解决方案也可能会影响此问题)。
最佳答案
我在 Laravel 中处理多个用户时也遇到过类似的问题。
我发现 multiauth
Laravel 包非常有帮助,请查看 https://github.com/ollieread/multiauth
文档非常简单。该插件最终扩展了原生 Laravel Auth 以允许多个用户。
让我粘贴网站上的片段:
Usage
Everything is done the exact same way as the original library, the one exception being that all >method calls are prefixed with the key (account or user in the above examples) as a method itself.
Auth::account()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::user()->attempt(array(
'email' => $attributes['email'],
'password' => $attributes['password'],
));
Auth::account()->check();
Auth::user()->check();
I found that have to call the user() method on a user type called user() looked messy, so I have added in a nice get method to wrap around it.
Auth::user()->get();
In the instance where you have a user type that can impersonate another user type, example being an admin impersonating a user to recreate or check something, I added in an impersonate() method which simply wraps loginUsingId() on the request user type.
Auth::admin()->impersonate('user', 1, true);
The first argument is the user type, the second is the id of said user, and the third is whether or not to remember the user, which will default to false, so can be left out more often than not.
And so on and so forth.
希望这有帮助!
关于php - Laravel 具有两种不同的用户类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357831/