Fat Free Framework (F3): custom 404 page (and others errors)

前端 未结 1 445
栀梦
栀梦 2021-02-09 11:54

How I can handle my 404 custom page (and possibly other errors)?

I just tried in routing part to add

GET /@codes /WebController->error
相关标签:
1条回答
  • 2021-02-09 12:34

    You don't have to define a route for this. F3 will automatically generate a 404 status code for any non-defined route.

    If you want to define a custom error page, you need to set the ONERROR variable.

    Here's a quick example:

    $f3->route('GET /','App->home');
    $f3->set('ONERROR',function($f3){
      echo \Template::instance()->render('error.html');
    });
    $f3->run();
    

    with error.html defined as:

    <!DOCTYPE html>
    <head>
    <title>{{@ERROR.text}}</title>
    </head>
    <body>
      <h1>{{@ERROR.text}}</h1>
      <p>Error code: {{@ERROR.code}}</p>
    </body>
    </html>
    

    Now if you call any non-defined route like /foo, the template error.html will be rendered with a 404 status code.

    NB: this works for other error codes. Other error codes are triggered by F3 or your application with the command $f3->error($status), $status being any valid HTTP status code (404, 500, 403, etc...)

    0 讨论(0)
提交回复
热议问题