如果用户未登录,ZF2 重定向到每个页面上的登录页面
ZF2 redirect to login page on every page if user is not logged in
有没有有效的方法来做到这一点?我研究过的选项:
- 检查布局中的会话容器
- 检查模块 onBootstrap functions() 中的会话容器
- 在每个 Controller/Action
中单独处理会话容器
理想情况下我会检查一次,有什么正确的方法吗?
类似于...
$session = new Container('username');
if($session->offsetExists('username')) {
//check im not already at my login route
//else redirect to login route
}
}
作为在 ZFcAuth 插件中的以下 url 中的结帐,我找到了一些用于检查和重定向的代码。
if (!$auth->hasIdentity() && $routeMatch->getMatchedRouteName() != 'user/login') {
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine(
'Location',
$e->getRouter()->assemble(
array(),
array('name' => 'zfcuser/login')
)
);
$response->setStatusCode(302);
return $response;
}
此代码块显示通往 validate/redirect 的道路。但是它们不是内置方式,因为 ZF2 仅提供组件。您还可以使用提供所有功能的其他插件,如 ZfcUser、ZfcAcl、ZfcRABC。
link : https://github.com/ZF-Commons/ZfcUser/issues/187#issuecomment-12088823 。
您可以在每个控制器中使用以下代码
public function onDispatch(\Zend\Mvc\MvcEvent $e)
{
if (! $this->authservice->hasIdentity()) {
return $this->redirect()->toRoute('login');
}
return parent::onDispatch($e);
}
也可以在模块的onBootstrap函数()上查看session,需要使用zf2事件匹配路由:
$auth = $sm->get('AuthService');
$em->attach(MvcEvent::EVENT_ROUTE, function ($e) use($list, $auth)
{
$match = $e->getRouteMatch();
// No route match, this is a 404
if (! $match instanceof RouteMatch) {
return;
}
// Route is whitelisted
$name = $match->getMatchedRouteName();
if (in_array($name, $list)) {
return;
}
// User is authenticated
if ($auth->hasIdentity()) {
return;
}
// Redirect to the user login page, as an example
$router = $e->getRouter();
$url = $router->assemble(array(), array(
'name' => 'login'
));
$response = $e->getResponse();
$response->getHeaders()
->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}, - 100);
其中 $list 将包含不被处理的路由列表:
$list = array('login', 'login/authenticate');
有没有有效的方法来做到这一点?我研究过的选项:
- 检查布局中的会话容器
- 检查模块 onBootstrap functions() 中的会话容器
- 在每个 Controller/Action 中单独处理会话容器
理想情况下我会检查一次,有什么正确的方法吗?
类似于...
$session = new Container('username');
if($session->offsetExists('username')) {
//check im not already at my login route
//else redirect to login route
}
}
作为在 ZFcAuth 插件中的以下 url 中的结帐,我找到了一些用于检查和重定向的代码。
if (!$auth->hasIdentity() && $routeMatch->getMatchedRouteName() != 'user/login') {
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine(
'Location',
$e->getRouter()->assemble(
array(),
array('name' => 'zfcuser/login')
)
);
$response->setStatusCode(302);
return $response;
}
此代码块显示通往 validate/redirect 的道路。但是它们不是内置方式,因为 ZF2 仅提供组件。您还可以使用提供所有功能的其他插件,如 ZfcUser、ZfcAcl、ZfcRABC。
link : https://github.com/ZF-Commons/ZfcUser/issues/187#issuecomment-12088823 。
您可以在每个控制器中使用以下代码
public function onDispatch(\Zend\Mvc\MvcEvent $e)
{
if (! $this->authservice->hasIdentity()) {
return $this->redirect()->toRoute('login');
}
return parent::onDispatch($e);
}
也可以在模块的onBootstrap函数()上查看session,需要使用zf2事件匹配路由:
$auth = $sm->get('AuthService');
$em->attach(MvcEvent::EVENT_ROUTE, function ($e) use($list, $auth)
{
$match = $e->getRouteMatch();
// No route match, this is a 404
if (! $match instanceof RouteMatch) {
return;
}
// Route is whitelisted
$name = $match->getMatchedRouteName();
if (in_array($name, $list)) {
return;
}
// User is authenticated
if ($auth->hasIdentity()) {
return;
}
// Redirect to the user login page, as an example
$router = $e->getRouter();
$url = $router->assemble(array(), array(
'name' => 'login'
));
$response = $e->getResponse();
$response->getHeaders()
->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}, - 100);
其中 $list 将包含不被处理的路由列表:
$list = array('login', 'login/authenticate');