Search code examples
perlmojolicious

mojolicious routes not working


I am going over the documentation http://mojolicio.us/perldoc/Mojolicious/Lite

I'm pasting the examples in the tutorial and I get this error message almost always:

Page not found... yet!

the last example i tried was this:

use Mojolicious::Lite;

  get '/with_layout';

  app->start;
  __DATA__

  @@ with_layout.html.ep
  % title 'Green';
  % layout 'green';
  Hello World!

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

this is the error I get

[Thu Nov 14 03:43:15 2013] [debug] GET "/with_layout".
[Thu Nov 14 03:43:15 2013] [debug] Template "with_layout.html.ep" not found.
[Thu Nov 14 03:43:15 2013] [debug] Template "not_found.development.html.ep" not found.
[Thu Nov 14 03:43:15 2013] [debug] Template "not_found.html.ep" not found.
[Thu Nov 14 03:43:15 2013] [debug] Rendering cached inline template.
[Thu Nov 14 03:43:15 2013] [debug] Rendering cached inline template.
[Thu Nov 14 03:43:15 2013] [debug] 404 Not Found (0.011649s, 85.844/s).

Solution

  • you missing the subroutine to define the route?!

    from the docs you can get this:

    use Mojolicious::Lite;
    
    # Route leading to an action that renders a template
    get '/with_layout' => sub {
        my $self = shift;
        $self->render('foo');
    };
    
    app->start;
    __DATA__
    
    @@ with_layout.html.ep
    <!DOCTYPE html>
    <html>
      <head><title><%= title %></title></head>
      <body><%= content %></body>
    </html>
    

    better is this:

    get '/with_layout' => sub {
        my $self = shift;
        $self->render(template => 'with_layout', format => 'html', handler => 'ep');
        return;
    };
    

    now the errors:

    Template "not_found.development.html.ep" not found.
    Template "not_found.html.ep" not found.
    

    you can create these files in you templates directory

    /lite_app.pl
    /templates/not_found.development.html.ep
    /templates/not_found.html.ep