User route 404 does not match the root of the site

I have several routes defined for my Mojolicious application and only 404 routes:

$r->any('*')->to(cb => sub {
    my $self = shift;
    $self->render(text => '404 Not Found');
    $self->rendered(404);
});

Route 404 is working fine:

$ ./bin/myapp.pl -m production get /no_such_url
404 Not Found

But I also want route 404 to match the root of the website, and I always get the default Mojolicious 404, even in production mode:

$ ./bin/myapp.pl -m production get /
<!DOCTYPE html>
<html>
  <head><title>Page not found</title></head>

What do I need to do to service my simple 404 callback on /?

+4
source share
1 answer

You are correct that any '*'you will not catch the main index /. This is the one exception. There are two simple solutions:

. , :

use Mojolicious::Lite;

sub my404 {
    my $self = shift;
    $self->rendered(404);
    $self->render(text => '404 any *');

}

any '*' => \&my404;
any '/' => \&my404;

app->start;

404 , Rendering exception and not found pages:

use Mojolicious::Lite;

app->start;

__DATA__

@@ not_found.development.html.ep
404 default template
+4

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


All Articles