我尝试将用户重定向到未通过身份验证的登录页面。我正在使用Slim3中的一个中间件来使用Sentinel进行检查。工作,但我需要覆盖主体,以不显示内容。例如,我可以使用CURL访问像/users这样的路由,这样我就可以获得所有的页面。因此,如果用户未通过身份验证,我需要删除/覆盖正文。
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);
if ($route !== '/login' && ! $user = Sentinel::check() )
{
$response = $response
->withStatus(301)
->withHeader("location", '/login')
;
}
return $next($request, $response);
}发布于 2018-11-17 22:04:43
如果您只想重定向用户,则不应调用$next回调:
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$route = parse_url($request->getUri(), PHP_URL_PATH);
if ($route !== '/login' && ! $user = Sentinel::check() )
{
return $response
->withHeader('Location', '/login')
->withStatus(302);
}
return $next($request, $response);
}https://stackoverflow.com/questions/53347273
复制相似问题