本文介绍了Auth :: user()在模块__construct()上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 laravel-modules 创建了一个名为Article的新模块.一些后端路由需要身份验证,我添加了auth中间件和附加的权限view_backend.我正在使用 https://github.com/spatie/laravel-permission 程序包执行以下任务:权限.

I created a new Module named Article using laravel-modules. Some backend routes needed authentication and i added auth middleware and an additional permission view_backend. I am using https://github.com/spatie/laravel-permission package for role-permissions.

问题是当我尝试访问路由admin/article/posts时,它提示我登录符合预期.但是登录后,它在Auth::user()__construct()方法上显示为空;

the issue is when i try to access the route admin/article/posts it prompts me the login as expected. But after login it show null on __construct() method for Auth::user();

我添加了#204 中提到的web中间件没有解决问题.你能指导我解决这个问题吗?我的项目是在Laravel 5.6上,并使用最新版本的Laravel-Modules

I added web middleware as mentioned on #204 but it did not solve the issue. Can you please guide me to resolve this? My project is on Laravel 5.6 and using the latest version of Laravel-Modules

Route::group(['namespace' => 'Modules\Article\Http\Controllers\Backend', 'as' => 'backend.article.', 'middleware' => ['web', 'auth', 'can:view_backend'], 'prefix' => 'admin/article'], function () {

    Route::resource("posts", "PostsController");
});

我的项目托管在Github上, https://github.com/nasirkhan/laravel-starter/tree/module

My project is hosted at Github, https://github.com/nasirkhan/laravel-starter/tree/module

推荐答案

根据文档:

作为替代,您可以直接定义基于Closure的中间件 在控制器的构造函数中.使用此功能之前,请确保 您的应用程序正在运行Laravel 5.3.4或更高版本:

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

public function __construct()
{
    $this->middleware(function ($request, $next) {
        $this->projects = Auth::user()->projects;

        return $next($request);
    });
}

或者您可以键入提示:

public function index(Request $request)
{
    $projects = $request->user()->projects;

    $value = $request->session()->get('key');
}

文档

这篇关于Auth :: user()在模块__construct()上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:51