Catalyst:将查询参数传递给 "forward" 调用
Catalyst: Passing query param to a "forward" call
我有一个 Catalyst 控制器,它以通常的方式响应参数 (/endpoint/foo/bar
),但也采用安全令牌的查询参数。在启动对此控制器的 forward
调用之前,如何设置参数的值?它不允许我分配给 $c->req->param('token')
。
(无法重写控制器以接受令牌作为参数而不是参数。)
$c->req->param('token')
不是左值,因此您不能 分配 给它。相反,您需要 to pass in the value.
$c->req->param('token', 1);
此行为已记录在案:
Like CGI, and unlike earlier versions of Catalyst, passing multiple
arguments to this method, like this:
$c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
will set the parameter foo to the multiple values bar, gorch and quxx.
Previously this would have added bar as another value to foo (creating
it if it didn't exist before), and quxx as another value for gorch.
考虑这个演示:
package MyApp::Controller::Root;
use base 'Catalyst::Controller';
__PACKAGE__->config(namespace => '');
sub default : Path {
my ($self, $c) = @_;
$c->req->param('foo', 1);
$c->forward('bar');
}
sub bar : Path('foo') {
my ($self, $c) = @_;
if ($c->req->param('foo')) {
$c->res->body("Secret param set!\n");
}
else {
$c->res->body("Hello World\n");
}
return;
}
我有一个 Catalyst 控制器,它以通常的方式响应参数 (/endpoint/foo/bar
),但也采用安全令牌的查询参数。在启动对此控制器的 forward
调用之前,如何设置参数的值?它不允许我分配给 $c->req->param('token')
。
(无法重写控制器以接受令牌作为参数而不是参数。)
$c->req->param('token')
不是左值,因此您不能 分配 给它。相反,您需要 to pass in the value.
$c->req->param('token', 1);
此行为已记录在案:
Like CGI, and unlike earlier versions of Catalyst, passing multiple arguments to this method, like this:
$c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
will set the parameter foo to the multiple values bar, gorch and quxx. Previously this would have added bar as another value to foo (creating it if it didn't exist before), and quxx as another value for gorch.
考虑这个演示:
package MyApp::Controller::Root;
use base 'Catalyst::Controller';
__PACKAGE__->config(namespace => '');
sub default : Path {
my ($self, $c) = @_;
$c->req->param('foo', 1);
$c->forward('bar');
}
sub bar : Path('foo') {
my ($self, $c) = @_;
if ($c->req->param('foo')) {
$c->res->body("Secret param set!\n");
}
else {
$c->res->body("Hello World\n");
}
return;
}