cakephp 在重定向后将数据传递给视图
当数据设置后重定向到另一个页面时,是否可以显示我使用 $this->set()
传递的数据?
谢谢, EL
Is it possible to display data that I pass with $this->set()
when there is a redirect to another page after the data setting?
Thanks,
EL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许最简单的方法是将数据存储在会话中:
并稍后使用:
Probably the easiest way is to store the data in the session with:
and to read it later with:
您可以如上所述使用会话,但如果您不想将数据保存在会话中,您可以使用调度程序对象,这是一个示例。我觉得很有用..
You can use the session as mentioned above but if you don't want to save the data in a session you could use the dispatcher object, here is an example. I find it useful..
我知道已经很长时间了,但我也遇到了这个问题,这就是我解决它的方法。
至于cakePHP 2.3.5,Introgy提供的解决方案将不起作用,因为controller->dispatch的定义是
相反,您可以使用
正如代码中的解释所述,
因此Introgy示例将被修改为:
您的数据将在目标的data:
并且 actionOnThatController 的视图将被渲染。
编辑:
我忘记添加这个,为了要渲染的目标视图,有必要在作为 $extra 传递的数组中添加键“return”,然后你必须渲染目标操作的视图,因此完整正确的修改将是
I know it has been looong time but I also had this problem and this is how I solve it.
As for cakePHP 2.3.5 the solution provided by Introgy will not work since the definition for controller->dispatch is
instead you can use
As the explanation in code said
So Introgy example would be modified as:
Your data will be available in the target's data:
and the view for actionOnThatController will be rendered.
EDIT:
I forgot to add this, for the target view to be rendered it is necessary to add the key 'return' in the array passed as $extra, then you have to render the targeted action's view, therefor the complete correct modification would be
您使用
header('Location: ...')
进行重定向?这使得浏览器开始一个新的请求。触发重定向的脚本可能与处理新请求的脚本相同,但现在有两个实例正在运行(或者第一个甚至可能已退出),并且每个实例都无法访问另一个实例的变量。要么将数据存储在任何地方(会话、共享内存等),要么按照第一个请求中的方式重建数据,或者只是不发出第二个请求,而是在内部重定向到另一个操作/视图。You redirect with
header('Location: ...')
? This makes the browser start a new request. The script that triggered the redirection may be the same as the one handling the new request, but there are now two instances running (or the first may even have quit) and each doesn't have access to the variables of the other. Either you store the data anywhere (session, shared memory, ...) or you rebuild it the same way you did in the first request or you just don't issue a second request but redirect to another action/view internally.