我目前正在试用新的Laravel5,并通过了身份验证(注册/登录).

To get the authenticated user in my controller I currently inject Guard into the controller action:

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()]);
    }
...

这是推荐的方式吗?

Second Question:我应该如何着手实现某些类型的角色/权限?大概client.edit,client.add,...5号幼虫在这里提供某种便利吗? 我如何为路由/控制器操作设置必要的角色/权限?

我想我可能需要为此编写自己的中间件.对如何解决这个问题有什么建议吗?

推荐答案

在Laravel 5上花费更多时间后,我可以回答自己的问题:

Is injecting Guard the recommended way? No: If you need to access Auth in your view, you can do so already like this:

@if( Auth::check() )
    Current user: {{ Auth::user()->name }}
@endif

This uses the Auth facade. A list of all available facades is in config/app.php under aliases:

What if I need Auth in my controller? Injecting an instance of Guard like shown in the question works, but you don't need to. You can use the Auth facade like we did in the template:

    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.

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

为了完整起见,我想再提两个项目.它们提供了在数据库中保存角色和权限所需的一切,并与Laraguard完美配合或独立工作:

Laravel相关问答推荐

在Laravel Sail Docker环境中的PHPMyAdmin中出现`Out of Memory‘错误

Laravel:通过数据透视表数据限制多对多Eager 加载

如何使用 laravel 更新单个值

我的共享主机上的每个帖子请求都出现 503 服务不可用

未捕获的 TypeError:Vue.component 不是函数

PHP 中的 Http 响应代码枚举

你如何在自定义 Laravel Nova 工具中使用确认对话?

Eloquent 的 Model::query() 是什么意思?

Laravel 分页方法不适用于 map 集合?

laravel 5.4 指定的key太长,为什么数字191

使用 Laravel 创建新项目会引发异常

如何从 Laravel 中的资源中获取图像?

无法声明类 Controller,因为该名称已在使用中

Laravel hasManyThrough

控制器中的laravel foreach循环

如何让 Laravel 将 View 的Content-Type标头返回为application/javascript?

在 Laravel 中判断会话超时

同一模型上的 Laravel 父/子关系

如何判断是否连接到 Laravel 4 中的数据库?

搜索结果分页 laravel 5.3