我一直在读到在 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 容器的正确方法是什么?我猜应该有办法吗?