php Laravel 5 新身份验证:获取当前用户以及如何实现角色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27571573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 new auth: Get current user and how to implement roles?
提问by cgross
I am currently experimenting with the new Laravel 5 and got the authentication to work (register/login).
我目前正在尝试使用新的 Laravel 5 并进行身份验证(注册/登录)。
To get the authenticated user in my controller I currently inject Guardinto the controller action:
为了在我的控制器中获取经过身份验证的用户,我目前注入Guard控制器操作:
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
class ClientController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index(Guard $auth)
{
return view('client.index', ['user' => $auth->user()]);
}
...
First Question:Is this the recommended way?
第一个问题:这是推荐的方式吗?
Second Question:How would I go about implementing some kind of roles/permissions? Something like client.edit, client.add, ... Does Larval 5 offer some kind of convenience here?
How would I set the necessary role/permission for a route/controller action?
第二个问题:我将如何实施某种角色/权限?类似client.edit, client.add, ... Larval 5 是否提供某种便利?我将如何为路由/控制器操作设置必要的角色/权限?
I am thinking that I might need to write my own middleware for that. Any suggestions on how to approach the problem?
我想我可能需要为此编写自己的中间件。关于如何解决问题的任何建议?
回答by cgross
After spending some more time on Laravel 5 I can an answer my own question:
在 Laravel 5 上花费更多时间后,我可以回答我自己的问题:
Is injecting Guardthe recommended way?No: If you need to access Authin your view, you can do so already like this:
注射Guard是推荐的方式吗?否:如果您需要Auth在视图中访问,您可以这样做:
@if( Auth::check() )
Current user: {{ Auth::user()->name }}
@endif
This uses the Authfacade. A list of all available facades is in config/app.phpunder aliases:
这使用了Auth门面。所有可用外墙的列表在config/app.php下面aliases:
What if I need Authin my controller?Injecting an instance of Guardlike shown in the question works, but you don't need to. You can use the Authfacade like we did in the template:
如果我需要Auth我的控制器怎么办?注入Guard问题中显示的like实例有效,但您不需要。您可以Auth像我们在模板中所做的那样使用外观:
public function index()
{
if(\Auth::check() && \Auth::user()->name === 'Don') {
// Do something
}
return view('client.index');
}
Be aware that the \is needed before the facade name since L5 is using namespaces.
请注意,\由于 L5 使用命名空间,因此在外观名称之前需要 。
I want to have permissions/roles using the new auth mechanism in L5:I implemented a lightweight permission module using the new middleware, it is called Laraguard. Check it out on Github and let me know what you think: https://github.com/cgrossde/Laraguard
我想在 L5 中使用新的身份验证机制获得权限/角色:我使用新的中间件实现了一个轻量级权限模块,它称为Laraguard。在 Github 上查看并告诉我您的想法:https: //github.com/cgrossde/Laraguard
UPDATE:For the sake of completeness I want to mention two more projects. They provide everything you need to save roles and permissions in the DB and work perfectly together with Laraguard or on their own:
更新:为了完整起见,我想再提两个项目。它们提供了在数据库中保存角色和权限所需的一切,并且可以与 Laraguard 完美配合或单独使用:
回答by Gabriel Sigouin
If you want make your own custom authentification, you need to keep the User model from Laravel 5 with all the dependency. After you will be able to login your user in your controller. Dont forget to put (use Auth;) after the namespace of your controller.
如果您想自定义身份验证,则需要保留 Laravel 5 中的 User 模型以及所有依赖项。之后您将能够在您的控制器中登录您的用户。不要忘记在控制器的命名空间之后放置(使用 Auth;)。

