在 mojolicious 中将参数传递给 redirect_to 并在目标控制器中使用它们 [英] Passing arguments to redirect_to in mojolicious and using them in the target controller
问题描述
我正在将参数传递给 redirect_to like
$c->redirect_to('named', foo => 'bar');
或
$c->redirect_to('named, query => {foo=>'bar'});
但我不确定如何使用它或在目标控制器中检索 foo 的值.
$self->redirect_to('named', foo => 'bar')
,使用前不带斜线,指的是命名路由,参数放在路由占位符中.
默认情况下,您在应用程序中定义的每个路由都会分配一个路由名称,或者您可以手动分配它们.(您也可以使用 ./myapp routes
获取分配的路由列表)
在精简版应用中:
<前>动作#路由名称get '/named' => sub { ... };# 命名get '/named/:foo' => sub { ... };# 命名的fooget '/named/:foo' => sub { ... } => 'something-else';# 别的东西以下重定向到 get '/named/:foo'
操作:
$self->redirect_to('namedfoo', foo => 'bar')
实际上等同于:
$self->redirect_to('/named/bar');
您可以使用 ->param 访问操作中的占位符值:
get '/named/:foo' =>子{我的 $self = shift;$self->render_text($self->param('foo'));};
呈现以下 HTML:
bar
您可能还想查看:http://mojocasts.com/e2#Generic%20Placeholders
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.
$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
这篇关于在 mojolicious 中将参数传递给 redirect_to 并在目标控制器中使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!