doutou19761022 2015-11-16 07:03
浏览 21
已采纳

如何在Fat-Free Framework中显示自定义401页面?

I'm new to F3 and I've just implemented a form-based login system (using the Auth plugin). It works, but my not-logged-in check looks like this:

if (!$f3->get('SESSION.user')) $f3->reroute('/login');

The example I was referring to, did this, which seems more correct:

if (!$f3->get('SESSION.user')) $f3->error(401);

However, this 401 error shows a simple error page. I want it to send back the login form (which is at /login) along with an error saying "You must be logged in" and I want this response to be a 401 instead of a 403 or 200.

Am I right in expecting this behaviour or is a 401 only valid for HTTP Basic Auth and not custom form-based auth?

  • 写回答

1条回答 默认 最新

  • doujian7132 2015-11-16 08:28
    关注

    You can customize the error response with the ONERROR hook.

    So in your case, you could do something like this:

    $f3->ONERROR=function($f3) {
      if ($f3->get('ERROR.code')==401) {
        // custom behaviour on 401
        echo \Template::instance()->render('error-401.htm');
      } else
        // default behaviour otherwise
        return FALSE;
    };
    
    <!-- error-401.htm -->
    <h1>This page requires authorization</h1>
    <a href="/login?path={{ @PATH }}">Click here to sign in</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化