Skip to content

Faq for the impatient

metaperl edited this page Oct 30, 2011 · 14 revisions

FAQ for the impatient

How do I get the physical path of the public directory?

Assuming $self is an instance of Mojolicious::Controller (which is what your action instances are), here is how:

$self->app->static->root

How do I specify the port for the Mojolicious daemon?

perl startapp.pl daemon --listen=http://*:$port

I need to create a URL that has the same IP number as the incoming request but I need to change the port to 3001 instead of the port 3000 that the incoming request came on.

 $self->req->url->clone->to_abs->port(3001);
 <@sri> perldoc Mojo::URL
 <@sri> the url you get is the absolute incoming url in its relative form with base part store in the base attribute
 <@sri> so you have to clone it, make it absolute and then adjust it to your needs
 <@sri> dump the url object after clone
 <@sri> i think that will illustrate it best

How do you get the parts of the route which matched a wildcard?

Look here

Getting an action to trigger for similar routes

I have a wildcard route like this: "/bo/(*folder)" => sub { ... } and it seems to only trigger for /bo/morestuff instead of just /bo . How can I have it trigger forboth?

Answer

First, check all answers by running: ./myapp.pl routes

Best Solution

<@sri> anyway solution is

get '/bo/(*folder)' => {folder=> 'default-value-or-undef'} => sub {...}

This solution is best because:

  1. It allows you to specify a default value for the optional path if not provided instead of writing more code for this
  2. You dont write 2 routes each times you have a path with optional part.

Naive Solution

get '/bo'           => \&bo_action;
get '/bo/(*folder)' => \&bo_action;

Other Solutions

(provided by MisterHatt and moritz) involve using regexes of Waypoints. Not for the impatient.

does app->() method chain?

It certainly does in the most common M::Lite action: app->secret('signed-cookie-password')->start;

How do I grow an app?

Starting a Mojolicious::Lite prototype from scratch and growing it into a well structured Mojolicious application is a simple process. A Lite app is a single file which will sprout in 3 ways. First an inflate command will extract the inline templates and layout and place them into templates and templates/layout directories. Next, the logic of the script file will go into the startup method of an Application class. Finally, a simple starter script will invoke your application class. An optional fourth step is to extend/replace the default Mojolicious controller. All of these steps are covered in L<WELL_STRUCTURED_APPLICATION>.

in other words - app-> in lite is the same thing as $self in sub startup {}

How do I dump all the form data?

( https://github.com/kraih/mojo/wiki/Request-data ) my $param = $self->req->body_params->to_hash;

Where do I go to read more about templating?

https://metacpan.org/module/Mojolicious::Guides::Rendering

How do I force a Mojolicious::Lite script into CGI mode?

app->start('cgi');

Cant you pass extra values to render?

https://metacpan.org/module/Mojolicious::Lite#Stash-and-templates shows you how:


  get '/bar' => sub {
    my $self = shift;
    $self->stash(one => 23);
    $self->render('baz', two => 24);
  };

  __DATA__

  @@ baz.html.ep
  The magic numbers are <%= $one %> and <%= $two %>.
Clone this wiki locally