Fat Free Framework (F3): 404 user page (and other errors)

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

I just tried to add routing

GET /@codes /WebController->error 

Where my WebController class handles the error, but for 404 I decided (partially). In fact, it works for

 http://mydomain.ext/itdoesntexists 

but if i remember

 http://mydomain.ext/sub/maybe_another_sub/and_so_on/doesnt_exist 

My route (of course) does not work.

Btw, with this route, in each case, it does not click on the 404 heading (just a maniac view of things, I think that Google is looking for resources and does not get a β€œclean” 404).

thanks

+6
source share
1 answer

You do not need to determine the route for this. F3 automatically generates a 404 status code for any undefined route.

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

Here is an 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 undefined route, for example / foo, the error.html template will be displayed 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 - any valid HTTP status code (404, 500, 403, etc.)

+20
source

Source: https://habr.com/ru/post/957323/


All Articles