Have you tried coderefs?

Here's a complete example (using full app syntax):

#!/usr/bin/env perl
use 5.18.2;
package RouteHandlers;
use Mojo::Base 'Mojolicious';

my $handle_all_the_routes = sub {
    my $self = shift;
    $self->render(text => "hi! you came from: " . $self->req->url);
};

sub startup {
    my $self = shift;
    my $r = $self->routes;
    $r->get('/foo/bar')->to(cb => $handle_all_the_routes);
    $r->get('/bar/baz')->to(cb => $handle_all_the_routes);
}

require Mojolicious::Commands;
Mojolicious::Commands->start_app('RouteHandlers');

You can also declare the sub normally and take a reference to it in the 
route, if you like that better:

sub handle_all_the_routes {
    my $self = shift; 
    $self->render(text => "hi! you came from: " . $self->req->url);
}

(elsewhere)
$r->get('/one/two')->to(cb => \&handle_all_the_routes)

hope that's helpful.

On Wednesday, May 7, 2014 5:36:56 PM UTC-5, Neil Watson wrote:
>
> On Wed, May 07, 2014 at 04:23:09PM -0600, Glen wrote: 
> >Create a controller for the action. If you want to create a one-off 
> subroutine, route to a callback. 
> > 
> >$r->get('/trend/kept')->to(cb => sub { ... }); 
>
> I was doing something similar before: 
>
> $r->get( '/trend/kept' sub { ... } => 'trend' ); 
>
> It worked, but now I hope to use the same sub for multiple URL's with 
> different 
> parameters. And that leads me to the error. 
>
> -- 
> Neil Watson 
> Linux/UNIX Consultant 
> http://watson-wilson.ca 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to