I use hook 'before_dispatch' like 
https://metacpan.org/pod/distribution/Mojolicious/lib/Mojolicious/Guides/Routing.pod#Hooks.

If the template have link to a file that not exist on the server, morbo try 
rout to hook and responce is not 404.

$ morbo test_app.pl
Server available at http://127.0.0.1:3000
[Sun Nov  1 18:13:42 2015] [debug] GET "/"
[Sun Nov  1 18:13:42 2015] [debug] Routing to a callback
[Sun Nov  1 18:13:42 2015] [debug] Rendering template "index.html.ep" from 
DATA section
[Sun Nov  1 18:13:42 2015] [debug] Rendering template 
"layouts/default.html.ep" from DATA section
[Sun Nov  1 18:13:42 2015] [debug] 200 OK (0.003677s, 271.961/s)
[Sun Nov  1 18:13:42 2015] [debug] GET "/logo.jpg"
[Sun Nov  1 18:13:42 2015] [debug] Routing to a callback
[Sun Nov  1 18:13:42 2015] [debug] Template "uri.html.ep" not found
[Sun Nov  1 18:13:42 2015] [debug] Nothing has been rendered, expecting 
delayed response
[Sun Nov  1 18:13:57 2015] [debug] Inactivity timeout
[Sun Nov  1 18:13:57 2015] [debug] GET "/logo.jpg"
[Sun Nov  1 18:13:57 2015] [debug] Routing to a callback
[Sun Nov  1 18:13:57 2015] [debug] Template "uri.html.ep" not found
[Sun Nov  1 18:13:57 2015] [debug] Nothing has been rendered, expecting 
delayed response
[Sun Nov  1 18:14:12 2015] [debug] Inactivity timeout

Then address "http://127.0.0.1:3000/news/"; is not work

[Sun Nov  1 18:17:35 2015] [debug] GET "/news/"
[Sun Nov  1 18:17:35 2015] [debug] Routing to a callback
[Sun Nov  1 18:17:35 2015] [debug] Template "uri.html.ep" not found
[Sun Nov  1 18:17:35 2015] [debug] Nothing has been rendered, expecting 
delayed response
[Sun Nov  1 18:17:50 2015] [debug] Inactivity timeout

Morbo restart.
If first request is "http://127.0.0.1:3000/news/"; you see follow:
$ morbo test_app.pl
Server available at http://127.0.0.1:3000
[Sun Nov  1 18:22:48 2015] [debug] GET "/news/"
[Sun Nov  1 18:22:48 2015] [debug] Routing to a callback
[Sun Nov  1 18:22:48 2015] [debug] Rendering template "news.html.ep" from 
DATA section
[Sun Nov  1 18:22:48 2015] [debug] Rendering template 
"layouts/default.html.ep" from DATA section
[Sun Nov  1 18:22:48 2015] [debug] 200 OK (0.004569s, 218.866/s)
[Sun Nov  1 18:22:48 2015] [debug] GET "/logo.jpg"
[Sun Nov  1 18:22:48 2015] [debug] Routing to a callback
[Sun Nov  1 18:22:48 2015] [debug] Rendering cached template "news.html.ep" 
from DATA section
[Sun Nov  1 18:22:48 2015] [debug] Rendering cached template 
"layouts/default.html.ep" from DATA section
[Sun Nov  1 18:22:48 2015] [debug] 200 OK (0.000709s, 1410.437/s)
[Sun Nov  1 18:23:19 2015] [debug] GET "/"
[Sun Nov  1 18:23:19 2015] [debug] Routing to a callback
[Sun Nov  1 18:23:19 2015] [debug] Rendering template "index.html.ep" from 
DATA section
[Sun Nov  1 18:23:19 2015] [debug] Rendering template 
"layouts/default.html.ep" from DATA section
[Sun Nov  1 18:23:19 2015] [debug] 200 OK (0.001823s, 548.546/s)
[Sun Nov  1 18:23:19 2015] [debug] GET "/logo.jpg"
[Sun Nov  1 18:23:19 2015] [debug] Routing to a callback
[Sun Nov  1 18:23:19 2015] [debug] Rendering cached template "news.html.ep" 
from DATA section
[Sun Nov  1 18:23:19 2015] [debug] Rendering cached template 
"layouts/default.html.ep" from DATA section
[Sun Nov  1 18:23:19 2015] [debug] 200 OK (0.000720s, 1388.889/s)


Why? Is it bug or need use anything command else.

Hook 'before_routes' has the same effect.

#!/usr/bin/env perl
# test_app.pl
use Mojolicious::Lite;

app->hook( before_dispatch => sub {
    my $c = shift;
    
    my $uri = $c->req->url->path;
    
    $uri = substr( $uri, 1 );
    
    get '/*uri' => sub {
      my $c = shift;
      if ( $uri eq 'news/' ) {
        
        $c->render(template => 'news');
      }
    };
});

get '/' => sub {
  my $c = shift;
  $c->render(template => 'index');
};

app->start;
__DATA__

@@ news.html.ep
% layout 'default';
% title 'News';
<h1>News</h1>
Uri is "<%= $uri %>".<br>
<img src="/logo.jpg">

@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Hi!</h1>
<img src="/logo.jpg">

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>




-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to