Laravel - 如何在不编辑内核或任何其他文件的情况下禁用 web.php 中某些路由的 Web 中间件组?

问题描述 投票:0回答:2

我正在开发一个使用源守护者编码的 laravel 8 项目 我只能编辑的文件是routes/web.php并且无法编辑api.php

那么我如何使用 web.php 添加路由到 api 组并为其禁用 Web 中间件组? 我无法编辑除 web.php 之外的任何文件

laravel laravel-8
2个回答
2
投票

您可以使用

withoutMiddleware
方法从路由中排除中间件:

Route::withoutMiddleware('web')->group(function () {
    // your routes without the 'web' middleware assigned
});

要模仿

api.php
文件加载到的组,您必须添加“api”中间件和前缀:

Route::withoutMiddleware('web')->middleware('api')->prefix('api')->group(function () {
    ...
});

Laravel 8.x 文档 - 中间件 - 注册中间件 -

withoutMiddleware


0
投票

在 Laravel 8 之前的版本中,

withoutMiddleware
外观上没有
Route

但是您可以复制与

相同的功能
Route::withoutMiddleware('web')->group(function () {
    // your routes without the 'web' middleware assigned
});

通过这样做:

Route::group(['excluded_middleware' => 'web'], function () {
    // your routes without the 'web' middleware assigned
});

如果需要排除多个中间件,请将数组传递给

excluded_middleware

© www.soinside.com 2019 - 2024. All rights reserved.