如何在 Slim v3 中访问中间件类中的$container?
我一直在阅读Slim v2中,$app被绑定到中间件类。我发现在v3中情况并非如此?下面是我的中间件类,但我只是没有定义:
<?php
namespace CrSrc\Middleware;
class Auth
{
/**
* Example middleware invokable class
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
{
// before
var_dump($this->getContainer()); // method undefined
var_dump($this->auth); exit; // method undefined
if (! $this->get('auth')->isAuthenticated()) {
// Not authenticated and must be authenticated to access this resource
return $response->withStatus(401);
}
// pass onto the next callable
$response = $next($request, $response);
// after
return $response;
}
}
访问中间件中 DI 容器的正确方法是什么?我猜应该有办法吗?