-
Notifications
You must be signed in to change notification settings - Fork 49
Description
I feel a sub-section within routing should be devoted to displaying the correct way to provide a catch-all... Fat-Free doesn't provide a direct method for catch-all, so it may confuse new users. It's also one of the first things I do within a project to provide custom logic instead of a default 405.
Now personally I have two ways of achieving this, but I figured I'd ask everyone else to see if you guys have your own ways.
Method 1:
// known routes should be added before catch-all...
$f3->route('GET|HEAD|POST /*/*', function($f3) {
//
// add custom logic here....
//
// call F3 error handler for cases not matching custom logic
$f3->error(405);
});In my tests, "//" proved to work but I didn't use a full test suite with edge cases, so I'm unsure if it provides true catch-all support.
Using "//" also infers that a method has already caught all single parameter routes. E.g. /@action. With this said the wildcard may need to be at a proportional depth compared with existing routes.
Method 2:
I'm going to be lazy here, but it involves adding a custom error handler to catch 405 errors... I'll just provide an example from my error handler:
\Error::instance()->addHandler(405, function() use($f3) {
// custom logic
if (!$f3->get('AJAX') && in_array($f3->get('VERB'), array('GET', 'HEAD', 'POST'))) {
$f3->reroute('/');
}
// call default F3 error handler (will pass on 405 error)
call_user_func_array(array($f3, 'error'), $f3->get('ERROR'));
});That's about it. Let me know your thoughts, or if you have an alternative method (we can pick which one is best suited) :)