在特定的路由范围cakephp中调用中间件3

时间:2017-12-04 09:41:56

标签: cakephp routing cakephp-3.0 middleware

我创建了一个自定义中间件,并尝试在cakephp 3.5.x中的Cases下的特定路由范围中使用,

要求:

routes.php文件

use Api\Middleware\CasesMiddleware;

Router::scope('/', function (RouteBuilder $routes) {

    $routes->registerMiddleware('CasesRequired', new CasesMiddleware());

    Router::scope('/Cases', function (RouteBuilder $routes) {
        $routes->applyMiddleware('CasesRequired');
    }
}

但这不起作用。但是当我将$routes->applyMiddleware('CasesRequired')放在路由范围cases之外时,其工作方式如下,

Router::scope('/', function (RouteBuilder $routes) {

    $routes->registerMiddleware('CasesRequired', new CasesMiddleware());
    $routes->applyMiddleware('CasesRequired');

    Router::scope('/Cases', function (RouteBuilder $routes) {

    }
}

请说明出现了什么问题.......只能在路由范围Cases下调用中间件。

1 个答案:

答案 0 :(得分:0)

use Api\Middleware\CasesMiddleware;

Router::scope('/', function (RouteBuilder $routes) {

    $routes->registerMiddleware('CasesRequired', new CasesMiddleware());

    $routes->scope('/Cases', function ($routes) {
        $routes->applyMiddleware('CasesRequired');
    }
}