我想将未经过身份验证的用户(访客)转发至登录表单。zf2 acl转发至登录表
使用重定向时,我需要将访客用户从他们正在查找的页面重定向到登录页面;然后再重定向它们。
实施例:
(客体的访问次数)MYSITE /控制器/动作VAR1 = XXX & VAR2 = YYY
(AclService重定向)MYSITE /登录
(AuthService重定向)MYSITE /控制器/ action?var1 = xxx & var2 = yyy
而这一切只能使用会话变量(我猜)。
我的想法是将用户转发到mySite /登录。当身份验证成功时,我需要做的唯一事情就是重定向到当前的URL。 此行为的优点是,如果用户单击浏览器的后退按钮,页面保持不变(mySite/controller/action?var1 = xxx & var2 = yyy)。
这里我的代码:
在module.php
public function onBootstrap(MvcEvent $e){
$app = $e->getApplication();
$sm = $app->getServiceManager();
$acl = $sm->get('AclService');
$e -> getApplication()-> getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController',MvcEvent::EVENT_DISPATCH,array($acl, 'checkAcl'),-10);
}
在我AclService
,checkAcl功能
[...]
if (!$this->acl ->isAllowed($role, $controller, $action)){
//when my AuthService->hasIdentity() returns false, the the role is guest
if($role=='guest'){
$controllerClass = get_class($e->getTarget());
//this prevents nested forwards
if($controllerClass!='Auth\Controller\Auth2Controller'){
$e->setResult($e->getTarget()->forward()->dispatch('Auth\Controller\Auth2',array('action'=>'index')));
}
}
else{...}
}
然后在我的AuthService,我用这个函数(称为mysite的/ login)重定向认证用户
//if the login page has ben forwarded
if($e->getRouteMatch()->getParam('controller')!='Auth\Controller\Auth2'
{
$url=$this->Config['webhost'].$e->getRequest()->getRequestUri();
return $e->getTarget()->redirect()->toUrl($url);
}
//if the request comes directly from a login/register attempt
else{return $e->getTarget()->redirect()->toRoute('user_home');}
您对此有何看法?这说得通?
你知道更好的方法吗?
我建议不要重定向,但立即显示登录表单。 – akond
嗨,你是什么意思?我的方法是检查acl的调度事件,如果用户未通过身份验证以向登录表单Controller/action发送/发送请求。我只有在用户成功执行时才使用重定向功能。我如何在派送上显示登录表单?谢谢 –