Say I need to check some URI before I serve some result. I can do something like this:
sub type-routes {
route {
get -> Str $type where $type ∈ @food-types {
my %ingredients-table = $rrr.calories-table;
my @result = %ingredients-table.keys.grep: {
%ingredients-table{$_}{$type} };
content 'application/json', @result;
}
get -> Str $type where $type ∉ @food-types {
not-found;
}
}
}
Basically, different signature for existing and non-existing products in this case. However, that same URI is going to be used all across routes. It would be interesting to be able to check it before any route gets matched, so that when it arrives to the route block, we know that it's OK. That way it could also be reused across different routes.
I have checked before
and before-match
, and apparently you can do it, but you need to analyze the request object to get to that, there's no simple way of doing it.
Alternatively, would there be any way of defining a "fallback-route" so that if an URI is not found, not-found is returned?