我正在尝试实现一个登录系统,该系统足够智能,可以在用户决定(或被迫)进入登录页面之前将用户重定向回他们所在的页面。
我知道这似乎是一个与 this one 类似的问题, 和 this one ,但它们并未解决我的两种情况。
这里有两种情况:
用户特别决定去登录页面:
<a href="<?php echo $this->url(array( 'controller'=>'auth', 'action'=>'login'), 'default', true); ?>">Log In</a>
用户被重定向,因为他们试图访问 protected 内容:
if (!Zend_Auth::getInstance()->hasIdentity()) { $this->_helper->redirector('login', 'auth'); }
如何在不在地址栏中显示“重定向到”url 的情况下实现解决方案?
最佳答案
在 session 中保存目标 URL。我猜你有某种访问预调度插件。在那里做。然后,在登录表单处理程序中,检查 session 中的目标 URL,并在身份验证成功后重定向到它。
我项目中的示例代码:
class Your_Application_Plugin_Access extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
foreach (self::current_roles() as $role) {
if (
Zend_Registry::get('bootstrap')->siteacl->is_allowed(
$role,
new Site_Action_UriPath($request->getPathInfo())
)
) return; // Allowed
}
$this->not_allowed($request);
}
private function not_allowed(Zend_Controller_Request_Abstract $request) {
$destination_url = $request->getPathInfo();
// If the user is authenticted, but the page is denied for his role, show 403
// else,
// save $destination_url to session
// redirect to login page, with $destination_url saved:
$request
->setPathInfo('/login')
->setModuleName('default')
->setControllerName('login')
->setActionName('index')
->setDispatched(false);
}
...
}
这里,current_roles()
总是包含 'guest',它是未经身份验证的用户,Zend_Auth::hasIdentity(
) 是 false
.
关于zend-framework - Zend 框架 : How to redirect to original url after login?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1984352/