Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

perl - Passing arguments to redirect_to in mojolicious and using them in the target controller

I am passing arguments to redirect_to like

$c->redirect_to('named', foo => 'bar');

or

$c->redirect_to('named, query => {foo=> 'bar'});

but I am not sure how to use it or retrieve the value of foo in the target controller.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$self->redirect_to('named', foo => 'bar'), used without a preceding slash, refers to named routes, and parameters are placed into route placeholders.

Each route you define in your application gets assigned a route name by default, or you can assign them manually. (You can also get a list of assigned routes using ./myapp routes)

In a lite app:

action # route name

get '/named' => sub { ... }; # named
get '/named/:foo' => sub { ... }; # namedfoo
get '/named/:foo' => sub { ... } => 'something-else'; # something-else

The following redirects to the get '/named/:foo' action:

$self->redirect_to('namedfoo', foo => 'bar') 

Which is effectively the same as:

$self->redirect_to('/named/bar');

You can access the placeholder value within the action using ->param:

get '/named/:foo' => sub {
  my $self = shift;
  $self->render_text($self->param('foo'));
};

Which renders the following HTML:

bar


You might also want to check out: http://mojocasts.com/e2#Generic%20Placeholders


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...