在 cakephp 中使用重定向时传递数据以查看

发布于 2024-11-02 11:12:50 字数 386 浏览 4 评论 0原文

我在一个视图上有两个表单编辑配置文件和更改密码。我已将changepassword表单操作设置为changePassword并且我正在使用

$this->autoRender = false; 
$this->render("index");
$this->redirect("index#chngPassTab");

,但由于重定向,我无法设置一些要查看的变量。我不想使用会话。 我只想根据控制器操作中的条件设置一些变量,在视图中获取这些变量值,并且在回发后重新加载页面时,应选择密码选项卡,因为我使用了 $this->redirect("index#chngPassTab ");

请指导我解决此问题的最佳方法。谢谢。

I have two forms on a single view edit profile and change password. I have set the changepassword form action to changePassword and I am using

$this->autoRender = false; 
$this->render("index");
$this->redirect("index#chngPassTab");

but I am not able to set some varible to view due to redirection. I don't want to use session.
All I want to set some variables based on conditions in controller action, get these variable values in view and when page reloaded after postback the password tab should be selected for that I used $this->redirect("index#chngPassTab");

Please guide me best way to fix this issue. Thank you.

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

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

发布评论

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

评论(4

南薇 2024-11-09 11:12:50

设置 jquery 来根据解析到视图的数据处理更改不是更容易吗?

因此,例如,在您发表帖子后,您的 $whatever_variable

在您的视图中

<script>
 <?php if(!empty($whatever_variable): ?>
     $('.tabs').tabs('select', '#chngPassTab');; //or whatever plugin yu are using
<?php endif?>
</script>

设置编辑:我的错,只需重新阅读您的问题并确认您的帖子操作与您的视图不同。在这种情况下,发布到索引控制器,然后从索引控制器调用changepassowrd操作,该操作将返回您希望它返回的任何内容。或者,使用会话。

would;t be easier to set up a jquery to handle the changes based on data you parse to a view?

so, for example after you do a post your $whatever_variable is set

then in your view

<script>
 <?php if(!empty($whatever_variable): ?>
     $('.tabs').tabs('select', '#chngPassTab');; //or whatever plugin yu are using
<?php endif?>
</script>

EDIT: my bad, just reread your question and reliased that your post action is different then your view. In that case post to index controller and then from index controller call changepassowrd action, which returns whatever you want it to return. or, use sessions.

冷清清 2024-11-09 11:12:50

在控制器的函数中尝试一下:

$this->set('name_of_variable', {YOUR DATA HERE});

然后在视图中,您可以访问变量:

<?php echo $name_of_variable; ?>

Try this in the function of the controller:

$this->set('name_of_variable', {YOUR DATA HERE});

Then in the view, you can access the variable:

<?php echo $name_of_variable; ?>
嗼ふ静 2024-11-09 11:12:50

我认为当您重定向到索引控制器时,您需要在索引控制器中进行一些处理。处理表单时保存的任何项目都可以在数据库中使用,如果您正在基于 url 处理某些内容,则可以在渲染之前执行此操作。

另外:

$this->autoRender(false);

将阻止您的控制器渲染,但随后调用

$this->render("index");

将尝试渲染您的索引视图,但调用

$this->redirect("index#chngPassTab");

将覆盖它并重定向它,在这种情况下您只需要进行重定向,渲染调用将变得无用。

I think you need to do some processing in the index controller when you redirect to it. Any items that were saved when you processed the form would be available in the database and if you are processing something based on the url you could do that before render.

Plus:

$this->autoRender(false);

would stop your controller from rendering, but then calling

$this->render("index");

would try to render your index view, but calling

$this->redirect("index#chngPassTab");

would override it and redirect it, you only need to do the redirect in that case, the render call is being made useless.

花开半夏魅人心 2024-11-09 11:12:50

Http 在设计上是无状态的。如果重定向,最后一个请求的数据就消失了。这就是发明会话的原因。

如果您希望数据显示,请停止使用重定向。

此外,调用渲染和重定向也是没有意义的,因为渲染永远不会被渲染。

Http is stateless by design. If you redirect the data for the last request is gone. This is why sessions were invented.

If you want the data to show stop using redirect.

Also there is no point to calling render and the redirect as the render will never be rendered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文