Skip to content

Instantly share code, notes, and snippets.

@kraih
Created March 21, 2012 16:16
Show Gist options
  • Save kraih/2149176 to your computer and use it in GitHub Desktop.
Save kraih/2149176 to your computer and use it in GitHub Desktop.
package MyApp::Controller::Bar;
use Mojolicious::Lite;
get '/test' => sub {
my $self = shift;
$self->render('test');
};
1;
__DATA__
@@ test.html.ep
Just some test template.
package MyApp::Controller::Foo;
use Mojolicious::Lite;
get '/' => {text => 'Welcome to Mojolyst!'};
1;
package Mojolicious::Plugin::Mojolyst;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Loader;
sub register {
my ($self, $app, $conf) = @_;
# Discover controllers
my $loader = Mojo::Loader->new;
for my $class (@{$loader->search($conf->{controllers})}) {
# Steal children
$loader->load($class);
$app->routes->add_child($_) for @{$class->new->routes->children};
# Make DATA sections accessible
push @{$app->static->classes}, $class;
push @{$app->renderer->classes}, $class;
}
}
1;
#!/usr/bin/env perl
use lib 'lib';
use Mojolicious::Lite;
plugin Mojolyst => {controllers => 'MyApp::Controller'};
app->start;
@s1037989
Copy link

s1037989 commented Apr 4, 2017

@ovntatar: The reason why not all routes are being generated as expected is explained in add_child.
Add a child to this route, it will be automatically removed from its current parent if necessary.

Instead do:

    my @children = @{$class->new->routes->children};
    $app->routes->add_child($_) for @children;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment