在 laravel 服务提供者 boot() 方法中使用“auth:web”中间件定义路由

问题描述

我正在使用一个名为 https://github.com/garygreen/pretty-routes 的包 在它的服务提供者 boot() 方法中有一行 (here the code)

它正在定义一个带有来自其配置文件link to the code)的中间件的获取路由我刚刚在其配置文件添加了“auth:web”,但似乎“auth:web”中间件在代码中被调用当 auth('web')->user() 尚未 null

时,在 Laravel 引导其会话等之前到达该行

我不明白的是,我用 laravel/telescope 做同样的事情 (here the code) 但它有效。 为什么??? 也在改变:

['c','d','e']

到:

def search_at_idx(search_word,char_mat,idx,start,end):
if len(char_mat[idx]) == 2:
    if ''.join(char_mat[idx]) == search_word:
        return 0,2
    else:
        return 'not found','not found'
start,end = search_at_idx(search_word,char_mat[idx][1:],start+1,end)
return start,end 

in service provider 似乎解决了这个问题,并使这段代码的行为类似于望远镜包使用“auth:web”作为中间件的方式。 发生了什么?

解决方法

您需要将 web 中间件应用于您需要会话的任何路由,这是默认身份验证系统使用的。当您应用 auth 中间件而不使用它时,它不可能解析用户,因为没有要对其进行身份验证的会话。

您需要应用 web 中间件,然后再应用您想要的任何其他中间件:

'middlewares' => [
    'web','auth:web',],

如果您查看您提供的望远镜示例,您会看到它们还添加了 web 中间件。所以你并没有像望远镜配置那样“做同样的事情”。