【问题标题】:Zend Framework2 using Zfcuser & Bjyauthorize routingZend Framework2 使用 Zfcuser & Bjyauthorize 路由
【发布时间】:2013-05-21 15:42:39
【问题描述】:
我是一名初学者 Zend 框架程序员。我确实使用 ZfcUser 进行身份验证,使用 Bjyauthorize 进行授权。我要用户类型:普通用户和管理员。所以我想做的是在认证后将用户路由到页面 A 和管理员到页面 B。
在 Zfcuser 配置文件中没有这种可能性,我们只有这一行
'logout_redirect_route' => 'zfcuser/login',
如何为我的不同用户指定不同的路线?
【问题讨论】:
标签:
zend-framework
zfcuser
bjyauthorize
【解决方案1】:
对我来说,您的问题与 ZfcUser 或 BjyAuthorize 无关:只需让用户和管理员进入您的控制器,然后您就可以根据用户角色来调度他们。
return $this->forward()->dispatch('MyModule\Controller\Index', array('action'=>'PageB'));
【解决方案2】:
假设你在 bjyauthorize 中有一个“管理员”角色,你想重定向到另一个路由。
在你的 loginAction 中替换代码:
if ($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {
return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
}
使用此代码:
if ($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {
$roles = $this->serviceLocator->get('BjyAuthorize\Provider\Identity\ProviderInterface')->getIdentityRoles();
if (in_array('admin',$roles))
{
return $this->redirect()->toRoute('admin_route');
} else {
return $this->redirect()->toRoute('user_route');
}
}