定义应用中间件后,设置某个路由则失效

lethe

问题描述

config/middleaware.php中设置了api应用中间件

return [
    'api'=>[
        \app\api\middleware\LogMiddleware::class
    ]
];

然后再设置api下某个路由时则没有执行这个中间件

/config/route.php

<?php

/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

use core\utils\Json;
use Webman\Route;

//跨域检测
Route::options('[{path:.+}]', function () {
    return response('');
});

// 加载admin应用下的路由配置
require_once app_path('admin/route.php');

// 加载api应用下的路由配置
require_once app_path('api/route.php');

//设置默认的路由兜底
Route::fallback(function () {
    return Json::fail('404 not found', [], 404);
});

//关闭默认路由
Route::disableDefaultRoute();

/app/api/route.php

<?php
use app\api\controller\IndexController;
use Webman\Route;

Route::group("/api", function () {
    Route::post('/index/index', [IndexController::class, 'index']);
});

这个api/index/index路由地址没有执行中间件

362 1 0
1个回答

喵了个咪

有可能有其它中间件拦截了请求,所以没走api中间件

  • 暂无评论
🔝