Skip to content

Instantly share code, notes, and snippets.

@leejo
Last active November 5, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leejo/0b4b728b0319bfa01665 to your computer and use it in GitHub Desktop.
Save leejo/0b4b728b0319bfa01665 to your computer and use it in GitHub Desktop.
routes using a root
#!/usr/bin/env perl
# in reality this would be in a separate file
package ExampleApp;
# automatically enables "strict", "warnings", "utf8" and perl 5.10 features
use Mojo::Base qw( Mojolicious );
sub startup {
my ( $self ) = @_;
my $r = $self->routes;
my $root = $r->get( '/foo/bar/baz' );
$root->under( 'cb' => sub {
my $self = shift;
# do some checking here
} );
my $boz = $root->get( '/boz' ); # /foo/bar/baz/boz
$boz->to( 'ExampleController#example_form' );
my $baz = $root->get( '/baz' ); # /foo/bar/baz/baz
$baz->to( 'ExampleController#example_form' );
}
# in reality this would be in a separate file
package ExampleApp::ExampleController;
use Mojo::Base 'Mojolicious::Controller';
sub example_form {
my ( $self ) = @_;
$self->render( text => 'boo' );
}
# in reality this would be in a separate file
package main;
use strict;
use warnings;
use Mojolicious::Commands;
Mojolicious::Commands->start_app( 'ExampleApp' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment