我有一个简单的ACL检查,如果未授予访问权限,则使用重定向;如果发送了post请求,则返回401。
if (!$e->getViewModel()->acl->hasResource($route) || !$e->getViewModel()->acl->isAllowed($role, $route)) {
//Naughty trying to get somewhere they shouldn't (Clear there identity force them to login again)
$response = $e->getResponse();
//location to page or what ever
if($request->isPost())
{
$response->setStatusCode(401);
return $response;
}
else
{
return $e->getTarget()->getEventManager()->getSharedManager()->attach('Zend'Mvc'Controller'AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->redirect()->toRoute('auth');
}, -11);
}
}
现在,我在一些SQL查询中添加了一个"WHERE id=user->id"
但是因为用户没有登录PHP,所以会抛出通知
注意:未定义的属性:stdClass::$ID
并且不再重定向。
这只是在dev服务器上发生的,因为prod服务器没有显示错误/注意,但这仍然让我感到非常不舒服。这是正常的吗?还是有办法"强制"重定向?
我试着把它附加到不同的事件上,但没有改变的行为
问候
详述我们对问题的评论。显示器错误仅设置
确定是否应将错误作为输出,或者它们是否应该对用户隐藏。
它不是决定应该将哪些错误/警告记录到日志文件的设置。如果display_errors被打开,并且输出没有被缓冲,它将被立即发送到客户端(可能通过Web服务器)。由于标头总是首先发送到客户端,因此应用程序在输出警告消息之前会发送一个200 OK状态代码。因此,您尝试发送的任何状态代码都会丢失。这就是为什么你的401重定向在你的开发机器上不起作用。
好吧,问题只是我没有返回重定向。。。如果您在ZF2中返回它,它将中断当前进程,否则它将把它附加到末尾。
if (!$e->getViewModel()->acl->hasResource($route) || !$e->getViewModel()->acl->isAllowed($role, $route)) {
//Naughty trying to get somewhere they shouldn't (Clear there identity force them to login again)
$response = $e->getResponse();
//location to page or what ever
if($request->isPost())
{
$response->setStatusCode(401);
return $response;
}
else
{
return $e->getTarget()->getEventManager()->getSharedManager()->attach('Zend'Mvc'Controller'AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
return $controller->redirect()->toRoute('auth');
}, -11);
}
}