laravel middleware except 不生效问题

最近使用 laravel 6 遇到了一个中间件的问题,在控制器的构造函数中写了这样一句代码:

 $this->middleware('auth:api')->except(['index', 'show']);

可是不起作用,路由表中对应方法依旧完美的包含了这个中间件

laravel middleware except 不生效问题

于是,我新建了一个新的 laravel 项目开始了实验,

routes/api.php

Route::middleware('auth:api')->group(function (){
    Route::apiResource('user', 'UserController');
});

app/Http/Controller/UserController

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth')->except('index');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
     /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

当我在middleware 中传入auth:api时:

 $this->middleware('auth:api')->except('index');

index方法上的auth:api中间件并未去掉:

laravel middleware except 不生效问题

而在middleware 中传入auth时,不仅index方法上的auth:api中间件并未去掉,同时其他几个方法中反倒新增了auth中间件

laravel middleware except 不生效问题

也曾在百度和 google 上查了,大多不过是给控制器构造函数中那行代码变换写法

$this->middleware('auth:api', ['except' => 'index']);

或者是提问者拼写错误。我也尝试追了源码,可是到了 pipeline 那里就有点看不太明白了,所以来这里请教请教,还望看到的朋友能指点一二,谢谢!

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

路由文件怎么写的?

2年前 评论
seeker-x2y (楼主) 2年前
seeker-x2y (楼主) 2年前
Su (作者) 2年前
seeker-x2y (楼主) 2年前
Su (作者) 2年前
讨论数量: 3

你是中间件组,都会带上验证,特定方法不会走那个验证

2年前 评论
raybon (作者) 2年前
seeker-x2y (楼主) 2年前

路由文件怎么写的?

2年前 评论
seeker-x2y (楼主) 2年前
seeker-x2y (楼主) 2年前
Su (作者) 2年前
seeker-x2y (楼主) 2年前
Su (作者) 2年前

file

file

2年前 评论
Su (作者) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!