Home » Php » How to access class object inside Slim middleware?

How to access class object inside Slim middleware?

Posted by: admin October 26, 2017 Leave a comment

Questions:

I have created a middleware in PHP slim framework and have created an object of a custom class above it. Now, I can access that object in Slim routes using use ($myClassObject) syntax, but same doesn’t work for this middleware. I tried like this:

$app->add(function (Request $request, Response $response, $next) use ($myClassObject) {
    return $response;
});

It didn’t work. I can’t access any methods of that class. Is there any other way to do this for middleware?

Answers: