我有一个 Catalyst Controller ,它以通常的方式响应参数 (/endpoint/foo/bar
),但也采用安全 token 的查询参数。在发起对此 Controller 的 forward
调用之前,如何设置参数的值?它不允许我分配给 $c->req->param('token')
。
(不可能重写 Controller 来接受 token 作为参数而不是参数。)
最佳答案
$c->req->param('token')
不是 LValue,因此您不能分配给它。相反,您需要 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;
}
关于perl - Catalyst:将查询参数传递给 "forward"调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58935999/