8
#!/usr/local/bin/perl
use warnings;
use 5.014;
use Mojolicious::Lite;
use DBI;

# ...

get '/choose' => sub {
    my $self = shift;
    my $lastname = $self->param( 'lastname' );
    my $sth = $dbh->prepare( "SELECT id, firstname, birthday FROM $table WHERE lastname == ?" );
    $sth->execute( $lastname );
    my @rows;
    while ( my $row = $sth->fetchrow_hashref ) {
        push @rows, { id => $row->{id}, firstname => $row->{firstname}, lastname => $lastname, birthday => $row->{birthday} };
    }
    if ( not @rows ) {
        $self->redirect_to( 'new_entry' );
    } elsif ( @rows == 1 ) {
        my $id = $rows[0]{id};
        $self->redirect_to( "/show_address?id=$id" );   # show_address needs parameter "id"
    } else {
        $self->stash( rows => \@rows );
        $self->render( 'choose' );
    }
};

# ...

When I use redirect_to, is there another way to pass a parameter than writing it directly in the url ("/show_address?id=$id")?

sid_com
  • 24,137
  • 26
  • 96
  • 187
  • 2
    Did you try looking at the Documentation (http://search.cpan.org/~sri/Mojolicious-1.76/lib/Mojolicious/Controller.pm#redirect_to)? – Dimitar Petrov Aug 12 '11 at 16:13
  • http://search.cpan.org/~sri/Mojolicious-1.76/lib/Mojolicious/Lite.pm#Sessions – Ibrahim Aug 12 '11 at 16:21
  • And those versions go away so fast... https://metacpan.org/module/Mojolicious::Lite#Sessions – pwes Aug 22 '11 at 17:43
  • Possible duplicate of [Passing arguments to redirect\_to in mojolicious and using them in the target controller](http://stackoverflow.com/questions/9622247/passing-arguments-to-redirect-to-in-mojolicious-and-using-them-in-the-target-con) – Eugen Konkov Dec 01 '16 at 10:35

1 Answers1

8

redirect_to can build url for you and replace placeholders in your routes with your params.

If you need to build url with params you can do following:

my $url = $self->url_for("/show_address");
$self->redirect_to($url->query(id => $id));

Also note that you can pass parameters with query by setting flash variables: http://mojolicio.us/perldoc/Mojolicious/Controller#flash

That variables will be cleaned up by Mojolicious with next request.

yko
  • 2,710
  • 13
  • 15
  • 3
    Which of these two methods would you prefer? – sid_com Aug 28 '11 at 10:19
  • 3
    It absolutely depends on what you need and how you build your app. If you are passing parameters to the same application - flash is a good choice. However, passing query params is more failsafe: flash() method uses cookies to pass parameters around and also cookies are limiting you in params size more than query string. Personally I use both, but prefer query params in redirect. For example in auth process flash is really good: https://github.com/zipkid/mojolicious-login/blob/master/lib/Login/Auth.pm – yko Aug 28 '11 at 16:50
  • # + pass multiple url params as hash ref : my $url = $self->url_for('route_name') ; $c->redirect_to($url->query({'param_name1' => "$param_value1" , 'param_name2' => $param_value2})); – Yordan Georgiev Sep 27 '15 at 19:49