MayVortex has asked for the wisdom of the Perl Monks concerning the following question:

Good Day!
package SUi; use Mojo::Base 'Mojolicious'; sub startup { my $self = shift; $self->helper('db' => sub { some stuff }); $self->routesr->any('/store')->to('ui#store') }
package SUi::Ui; use SUi::Ext; use Mojo::Base 'Mojolicious::Controller'; sub store{ my ($self) = @_; my $db = $self->db; //Works! my $req = $self->req->param(); //Works! my $ext = SUi::Ext->new(); $ext->test() }
package SUi::Ext; use Mojo::Base 'Mojolicious::Controller'; sub text{ my ($self) = @_; my $db = $self->db; //Does't work! my $db = $self->app->db; //Does't wotk my $req = $self->req->param(); //Doen't work! my $req = $self->app->req->param(); //Doen't work! }
So... How to access helpers and other Mojolicious stuff from outside of controller? Thank you for help!

Replies are listed 'Best First'.
Re: Mojolicious Helpers outside of contollers
by Anonymous Monk on Aug 06, 2014 at 00:46 UTC

    First the "jokes"


    Now back to the question :)

    ... my $ext = SUi::Ext->new();

    Why are you creating another controller instance inside a controller? Why aren't you passing any arguments to the constructor?

    https://metacpan.org/pod/Mojolicious::Controller#ATTRIBUTES

    Lets see what the source tells us

    $ perldoc -l Mojolicious ...\site\lib\Mojolicious.pm $ ack "con\S+\bnew" -in ...\site\lib\mojo* ... ...\site\lib\Mojolicious.pm 73: = $self->controller_class->new(app => $self, stash => $stash, t +x => $tx); 531: my $c = $app->build_controller(Mojo::Transaction::HTTP->new); 532: my $c = $app->build_controller(Mojolicious::Controller->new); 560: $app->dispatch(Mojolicious::Controller->new); 569: $app->handler(Mojolicious::Controller->new);

    Hmm $self->controller_class->new(app => $self, stash => $stash, tx => $tx);

    Maybe you simply want to subclass SUi::Ext, to make SUi::Ui a subclass of SUi::Ext, as in use Mojo::Base 'SUi::Ext';