Search code examples
perlmoosemojolicious

Do Mojolicious and Moose play well together?


I'm working on a Mojo app and I'd like to be able to consume some Moose roles to make my life easier.

On CPAN I see MojoX::Moose::Controller, which has very simple internals. I don't see much else on using Moose with Mojo. Any potential issues I should be aware of or is it just smooth sailing?


Solution

  • In my experience, they work fine together. I've successfully built Mojolicious apps with Moose (and Moo should work fine as well.)

    Moose can extend the base Mojolicious controller class and then you can do whatever usual Moose things you want. Here's an example:

    package MyApp {
        use Moose;
        extends 'Mojolicious';
        with 'MyApp::Role::Whatever';
    
        sub startup { 
            my $self = shift;
            my $r = $self->routes;
            $r->get('/')->to('foo#default');
        }
    }
    
    package MyApp::Foo {
        use Moose;
        extends 'Mojolicious::Controller';
    
        sub default {
            my $self = shift;
            $self->render( text => "Helloooooo!!" );
        }
    }