2012-11-07 34 views

回答

8

您可以添加一個模板重定向用戶自定義錯誤頁您的自定義頁面名爲exception.html.epnot_found.html.ep位於liteapp的末尾。

例如:

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> 

對於參考,見Mojolicious rendering guide

渲染器將總是試圖找到例外。$模式,$格式。*或 NOT_FOUND。$模式,$格式。*回落到內置的默認 模板之前。

4

我想在我的404頁運行一些代碼,從這裏這麼借款 https://groups.google.com/forum/#!topic/mojolicious/0wzBRnetiHo

我所做的一切捕獲的路線,並把它放在我的所有其他途徑之後,使不匹配的URL路由通過這個:

any '/(*)' => sub { 
    my $self = shift; 
    $self->res->code(404); 
    $self->res->message('Not Found'); 

    # 404  
    $self->stash({ 
     # ... my stuff in the stash ... 
    }); 

    $self->render('mytemplate', status => 404); 
}; 
相關問題