Search code examples
perlmojoliciousmojolicious-lite

Mojolicious lite how to redirect for not found and server error pages to user defined error page


How to redirect user defined error page for not found and server error pages to user define page Mojolicious lite


Solution

  • You can add a template for your custom page named exception.html.ep or not_found.html.ep at the end of your liteapp.

    For example:

    use Mojolicious::Lite;
    get '/' => sub {
        my $self = shift;
        $self->render(text => "Hello.");
    };
    app->start;
    
    __DATA__
    @@ not_found.html.ep
    <!DOCTYPE html>
    <html>
      <head><title>Page not found</title></head>
      <body>Page not found <%= $status %></body>
    </html>
    

    For a reference, see the Mojolicious rendering guide.

    The renderer will always try to find exception.$mode.$format.* or not_found.$mode.$format.* before falling back to the built-in default templates.