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;
@ovntatar
Copy link

Hi I'm not sure if it is a bug but if I start you example with multiple routes like:

package MyApp::Controller::Bar;
use Mojolicious::Lite;

get '/test' => sub {
my $self = shift;
$self->render('test');
};

get '/test2' => sub {
my $self = shift;
$self->render('test2');
};

get '/test3' => sub {
my $self = shift;
$self->render('test3');
};

get '/test4' => sub {
my $self = shift;
$self->render('test4');
};

1;
DATA

@@ test.html.ep
Just some test template.

@@ test2.html.ep
Just some test template.

@@ test3.html.ep
Just some test template.

@@ test4.html.ep
Just some test template.

The main controller import only two routes test and test3, ( test2 and test4 lost)

perl myapp.pl routes -v

/test GET test ^/test(?:.([^/]+)$)?
/test3 GET test3 ^/test3(?:.([^/]+)$)?

any Ideas why ?

@s1037989
Copy link

s1037989 commented Apr 4, 2017

Update Mojolyst.pm for the new Mojo::Loader implemented in 5.81:

package Mojolicious::Plugin::Mojolyst;
use Mojo::Base 'Mojolicious::Plugin';

use Mojo::Loader qw/find_modules load_class/;

sub register {
  my ($self, $app, $conf) = @_;

  # Discover controllers
  for my $class ( find_modules $conf->{controllers} ) {

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

    # Make DATA sections accessible
    push @{$app->static->classes},   $class;
    push @{$app->renderer->classes}, $class;
  }
}

1;

@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