Silex icon indicating copy to clipboard operation
Silex copied to clipboard

Middleware After/Before with mount()

Open aserhii87 opened this issue 10 years ago • 2 comments

I want to attach middleware to group routes, i write this, for example:

$app->mount("/profile", function ($profile) {
    $profile->get("/", "App\\Controllers\\IndexController::profile");
})
->before(new AuthMiddleware())
->after(new AfterMiddleware());

This generate an error:

Fatal error: Call to a member function after() on null in 
.../app/routes.php on line 23

where did I go wrong?

aserhii87 avatar Aug 02 '16 15:08 aserhii87

you need to attach the middleware's to the $profile controller collection I'm pretty sure instead of the $app

ragboyjr avatar Aug 02 '16 16:08 ragboyjr

$app->mount("/profile", function ($profile) {
    $profile->get("/", "App\\Controllers\\IndexController::profile");
    $profile->before(new AuthMiddleWare())->after(new AfterMiddleware());
});

ragboyjr avatar Aug 02 '16 16:08 ragboyjr