Sho*_*ine 1 php zend-framework2 zfcuser
我在控制器中有一个功能,需要检查用户是否已登录(我正在使用zfcuser模块),如果没有,则显示登录屏幕.
我的理解是我应该运行这个:
return $this->forward()->dispatch('zfcuser', array('action' => 'authenticate'));
Run Code Online (Sandbox Code Playgroud)
不幸的是,这改变了网址.我想显示登录屏幕,并允许用户登录而不更改网址.通过扩展,这意味着我还希望将用户重定向回同一页面,而不是转到/user页面.
我怎样才能实现这两件事?
小智 5
在过去的几天里,我一直在努力解决这个问题.ZfcUser配置包括一个use_redirect_parameter_if_present设置,但文档没有提供任何关于如何使用它的示例.我不知道我的方法是否稳固,但这就是我做的工作.请注意,此方法会保留URL,因为它使用了forward.如果不使用前进,我不确定另一种方法.
在zfcuser配置文件中,继续并将该use_redirect_parameter_if_present设置设置为true.这会导致ZfcUser的登录操作redirect在请求中查找参数.它使用它在成功验证后将用户返回到指定位置.
然后,在我想确保用户登录的控制器中,我有:
if (!$this->zfcUserAuthentication()->hasIdentity()) {
// Build the redirect URL using the route to which we want
// the user returned.
$redirect = $this->url()->fromRoute('your route', array(
'optional-route-param' => 1234
));
// Set the redirect URL in the request so that ZfcUser can
// pick it up. This is the key.
$this->getRequest()->getQuery()->set('redirect', $redirect);
// Use ZfcUser's login action rather than its authentication
// action.
return $this->forward()->dispatch('zfcuser', array(
'action' => 'login'
));
}
Run Code Online (Sandbox Code Playgroud)
我希望有所帮助.如果您在解决此问题时遇到问题,则可能需要发布一些代码.
| 归档时间: |
|
| 查看次数: |
2949 次 |
| 最近记录: |