通过重定向传递参数

发布于 2024-12-28 16:48:10 字数 376 浏览 3 评论 0原文

是否可以通过重定向传递参数?我尝试了很多选择,但似乎没有任何效果。我最新的方法是:

return $this->redirect(array('Users::helloworld', 'args' => array('id' => 'myId')));

然后我创建了一条路线:

Router::connect('/Users/helloworld?id={:id}', array('controller' => 'Users', 'action' => 'helloworld'));

但我得到的只是 users/helloworld/myId

Is it possible to pass parameters via redirect? I tried a lot of options, but nothing seems to work. My latest approach is:

return $this->redirect(array('Users::helloworld', 'args' => array('id' => 'myId')));

then I created a route:

Router::connect('/Users/helloworld?id={:id}', array('controller' => 'Users', 'action' => 'helloworld'));

but all I get is users/helloworld/myId

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2025-01-04 16:48:10

args 是路由的一部分,将使用非常通用的路由(不是您创建且不需要的路由)转换为 URL

要获取查询字符串,只需使用 ? 键:

return $this->redirect(array(
    'Users::helloworld',
    '?' => array('id' => $myId)
));
// will use the route:
//    /{:controller}/{:action}/{:args}
// and generate
//    /users/helloworld?id=$myId

测试:https://github.com/UnionOfRAD/lithium/blob/master/tests/cases/net/http/RouteTest.php#L374-405

args is part of the routes and will be converted into an URL using the very generic route (not the one you created and don't require)

To get a query string, simply use the ? key:

return $this->redirect(array(
    'Users::helloworld',
    '?' => array('id' => $myId)
));
// will use the route:
//    /{:controller}/{:action}/{:args}
// and generate
//    /users/helloworld?id=$myId

The test for that: https://github.com/UnionOfRAD/lithium/blob/master/tests/cases/net/http/RouteTest.php#L374-405

梦旅人picnic 2025-01-04 16:48:10

您可以执行以下操作,而不是定义单独的路由来传递参数。
假设您想传递 2 个参数: $arg1 & $arg2 到您的 Users 控制器的 helloworld 操作:

return $this->redirect(array(
'Users::helloworld',
'args' => array(
        $arg1,
        $arg2
    )
));

Instead of defining a separate route to pass arguments, you could just do the following.
Lets say you want to pass 2 arguments: $arg1 & $arg2 to the helloworld action of your Users controller:

return $this->redirect(array(
'Users::helloworld',
'args' => array(
        $arg1,
        $arg2
    )
));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文