Silex将中间件设置为ControllerCollection

Gab*_*iel 2 middleware silex

我想做这样的事情:

$app->mount('dashboard', new Travel\Controllers\Dashboard())->before(function() use ($app) {
              //check if is logued...
            })
Run Code Online (Sandbox Code Playgroud)

可能吗?

谢谢!

mpm*_*mpm 6

你可以做

$controllers = $app["controllers_factory"];
$controllers->before(function(Request $request){});
Run Code Online (Sandbox Code Playgroud)

在你的ControllerProviderInterface::connect方法

如果你需要$app定义php文件中定义的函数,你可以创建一个受保护的函数

$app['callback'] = $app->protect(function(){});
Run Code Online (Sandbox Code Playgroud)

然后

$controllers->before($app["callback"]);
Run Code Online (Sandbox Code Playgroud)