Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

First steps with Mojolicious

by frazap (Monk)
on May 01, 2019 at 12:47 UTC ( [id://1233228]=perlquestion: print w/replies, xml ) Need Help??

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

I have two small web apps that I managed to create with Mojolicious::Lite separately. Now I'm trying to unite these pages in one bigger Mojolicious application.

The page's names are libmap, invtot and together they would create the Lib application, therefore the Lib.pm file.

My files and folders are
run.pl lib.conf templates common_js.html.ep invtot.html.ep libmap.html.ep layouts default.html.ep public index.html some .js files lib Lib.pm Invtot.pm Invtot Controller Invtot.pm
The run.pl script is
use strict; use warnings; use lib 'lib'; use Mojolicious::Commands; Mojolicious::Commands->start_app('Lib')
The Lib.pm module is
package Lib; use Mojo::Base 'Mojolicious'; # This method will run once at server start sub startup { my $self = shift; # Load configuration from hash returned by config file my $config = $self->plugin('Config'); # Configure the application $self->secrets($config->{secrets}); # Router my $r = $self->routes; # Normal route to controller $r->get('/')->to(controller => 'index'); $r->get('/libmap'); $r->get('/invtot'); $r->post('/invtot')->to(controller => 'Invtot', action=> 'post'); } 1;
The Invtot.pm module connects to the database, it is
package Invtot; use Mojo::Base 'Mojolicious'; sub startup { my $app = shift; my $config = $app->config; $app->log->debug("connecting..."); $app->plugin( 'Database', { dsn => 'dbi:mysql:host=mysql.unifr.ch:dbname=dokpe_i0 +1', username => $config->{username}, password => $config->{password}, options => { 'pg_enable_utf8' => 1, AutoCommit => 1, PrintError => 0, RaiseError => 1 }, helper => 'db', } ); } 1;
And the file that I placed under Invtot/Controller (also named Invtot.pm ....) is
package Invtot::Controller::Invtot; use Mojo::Base 'Mojolicious::Controller'; sub post { my $c = shift; my $p = $c->req->body_params->to_hash; #gets the form values my $for_abo = $p->{'ccabo'} // '' eq 'ABO' ? 1 : 0; my $reqVal = $p->{'ZLField'}; .... #build the sql string $sql{$key} = format_sql( $reqVal, $p->{ZLMode}, $for_abo ); #get the values from the db my $stm = $c->db->prepare_cached( $sql{$key} ) $stm->execute($toFind) or die $stm->errstr; $c->stash( stm => $stm, for_abo => $for_abo ); } #build the page with the db values $c->render('invtot'); } ... 1;

The libmap page and the form from the invtot page are displayed correctly. However, the post is not run :

I got the error

Class "Lib::Invtot" is not a controller Template "invtot/post.html.ep" not found
How should I correct this ?

frazap

Replies are listed 'Best First'.
Re: First steps with Mojolicious
by Your Mother (Archbishop) on May 01, 2019 at 19:47 UTC

    I don't have an answer to your actual question. I just want to point out that if you have two working apps and you have a URI map that won't collide, there isn't necessarily a compelling reason to join them. They can be run as is together. Re: Serving multiple Plack apps shows Catalyst, Mojolicious, PHP, and CGI code all run under one app server. Doing two mojo apps would work just as easily.

      Thanks for the suggestion

      At the end, placing the connection to the database in the startup of MyLib.pm works. That means I can drop the file MyLib/Invtot.pm whitch was supposed to make this

Re: First steps with Mojolicious
by frazap (Monk) on May 01, 2019 at 14:38 UTC
    I changed a few things and did some progress (I think)...

    First, rename the whole thing from Lib to MyLib and reread the doc

    My +folders are now
    run.pl my_lib.conf +templates common_js.html.ep invtot.html.ep libmap.html.ep +layouts default.html.ep +public index.html some .js files +lib MyLib.pm +MyLib Invtot.pm +Controller Invtot.pm
    I have the post action starting until it crashes on the line trying to fetch the database handler which is get from startup in package MyLib::Invtot. The whole file is
    package MyLib::Invtot; use Mojo::Base 'Mojolicious'; sub startup { my $app = shift; my $config = $app->config; $app->log->debug("connecting..."); $app->plugin( 'Database', { dsn => 'dbi:mysql:host=mysql.unifr.ch:dbname=dokpe_i0 +1', username => $config->{username}, password => $config->{password}, options => { 'pg_enable_utf8' => 1, AutoCommit => 1, PrintError => 0, RaiseError => 1 }, helper => 'db', } ); } 1;
    and the call to helper => db should make it accessible to the controller in MyLib/Controller/Invtot. That's not the case and I'm not even sure it's called at all. How can I make sure the connection is made the first time the invtot page is load and how can I then retrieve the database connection in the controller ?

    Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1233228]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found