简体   繁体   English

如何使用f3 beforeroute()检查登录用户,重定向到登录名然后返回原始路由?

[英]How to use f3 beforeroute() to check for logged in user, redirect to login and then back to original route?

I'm trying to use fat free framework in my application. 我正在尝试在应用程序中使用无脂肪框架。 beforeroute() seems like a logical choice for checking for an active session, if all routes on this path require authentication. 如果此路径上的所有路由都需要认证,则beforeroute()似乎是检查活动会话的逻辑选择。 I am having trouble trying to find which route the user was attempting to go to before I hijack it and force them to login. 在劫持并强迫他们登录之前,我很难找到用户尝试去的路线。 I feel like there should be an f3 variable, or something simple that I can call within beforeroute() that'll give me information about where they were trying to go, and that I should be able to use f3 to accomplish this, instead of other ways I've done this in php apps (session variable, or sending something as a param in the domain, etc). 我觉得应该有一个f3变量,或者我可以在beforeroute()中调用的一些简单方法,它将为我提供有关他们试图去的地方的信息,并且我应该能够使用f3来完成此任务,而不是我在php应用程序中完成此操作的其他方式(会话变量,或在域中作为参数发送内容等)。

Am I missing something about beforeroute()? 我是否缺少有关beforeroute()的内容? Should it not be used for this situation? 它不应该用于这种情况吗? And if not, what is the best practice? 如果没有,最佳实践是什么?

I've tried grabbing the url in a session variable $f3->set('SESSION.previousUrl', $f3->REALM), but since I am always redirecting them to login, the result is always /login. 我尝试在会话变量$ f3-> set('SESSION.previousUrl',$ f3-> REALM)中获取url,但是由于我总是将它们重定向到登录名,因此结果始终是/ login。

While I'm at it, can somebody explain the difference between reroute and redirect within f3? 当我在使用它时,有人可以解释f3中重新路由和重定向之间的区别吗? Thanks in advance. 提前致谢。

Just pass the origin URL to the login route. 只需将原始URL传递到登录路由即可。 Eg: 例如:

function beforeRoute($f3,$params) {
  if (/*user not authenticated*/)
    $f3->reroute('/login?origin='.$f3->PATH);
    // or if you need to preserve query strings:
    $f3->reroute('/login?origin='.urlencode($f3->PATH.($f3->QUERY?'?'.$f3->QUERY:'')));
}

Now in your login route, if the user authenticates correctly, reroute it back to the origin URL: 现在,在您的登录路线中,如果用户正确验证身份,请将其重新路由回原始URL:

function post($f3,$params) {
  if (/*user gets authenticated*/) {
    $f3->reroute(isset($_GET['origin'])?$_GET['origin']:'/');
  }
}

As for the redirect method, it's a mix between route and reroute . 至于重定向方法,它是routereroute之间的混合。

For example, $f3->redirect('GET /oldpage','/newpage',TRUE) is a shorthand for: 例如, $f3->redirect('GET /oldpage','/newpage',TRUE)是以下代码的简写:

$f3->route('GET /oldpage',function($f3){
  $f3->reroute('/newpage',TRUE);
});

It's mostly interesting to be used in configuration files, such as: 在配置文件中使用它最有趣,例如:

[redirects]
GET /oldpage = /newpage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM