Search code examples
mojolicious

`mojo routes` just shows "/*whatever * whatever"


I have copied the exact example from Minimal Routes into myapp.pl

# Application
package MyApp;
use Mojo::Base 'Mojolicious';

sub startup {
  my $self = shift;

  # Router
  my $r = $self->routes;

  # Route
  $r->get('/welcome')->to(controller => 'foo', action => 'welcome');
}

1;

When I run mojo routes, I get

/*whatever  *  whatever

When I run mojo routes myapp.pl, I get

/*whatever  *  whatever

How is mojo routes supposed to function, when I run mojo --help | grep -i routes it says,

routes    Show available routes

If I run myapp.pl routes, it shows nothing. How do I get a listing of the routes, as provided in the example?


Solution

  • That code copied is for a module. Mojo creates a startup script. This script is the argument required for the routes command. Generate a project with

    mojo generate app MyApp
    

    then checkout script/my_app, it contains

    #!/usr/bin/env perl                                                                                       
    
    use strict;                                                                                               
    use warnings;                                                                                             
    
    use FindBin;                                                                                              
    BEGIN { unshift @INC, "$FindBin::Bin/../lib" }                                                            
    use Mojolicious::Commands;                                                                                
    
    # Start command line interface for application                                                            
    Mojolicious::Commands->start_app('MyApp');
    

    You'll see the code for the file you copied in lib/MyApp.pm. You'll want to run

    Now you can run

    script/my_app routes
    /  GET  
    

    It will check the files in lib to generate the route listing.


    Thanks to Grinnz and CandyAngel on Freenode (IRC) in #mojo for providing the support to answer this.