0

我检查了典型的 CakePHP 请求,但找不到解决方案。

一开始myThemeCookie是不存在的。通过设置此 cookie,用户可以选择他/她最喜欢的主题,并且他/她将来会看到该主题。

我有myControllermyAction。我有一个表格myAction.ctp。用户可以选择主题名称。

首先请求myAction检查$_POST. 它找不到任何价值。行动只显示形式。

在第二个请求中,当用户提交表单时,我myAction再次将用户发送到。动作查找$_POST数据。从中获取主题 css 地址$_POST。通过使用myComponent,myThemeCookie被写入。

然后我有myLayout.ctp文件。此布局文件使用myHelper并通过使用myHelper->getCookieTheme(),它从 cookie 中获取主题名称。但是助手给出了空名。所以用户会看到旧主题的字体。(请注意,我通过$_COOKIE在帮助程序中使用变量来检查 cookie 数据)

当我刷新 myAction 页面并发出第三个请求时,我看到它 myHelper->getCookieTheme()给出了正确的 css 文件名。用户会看到新主题的字体。

因此,在 myAction 中设置 cookie 后,我无法在辅助方法和布局文件中看到它。但似乎视图/帮助文件是在控制器操作之后计算的。我错过了什么吗?

4

1 回答 1

0

You don't have to do that in the view at all. Also don't access $_POST directly but use the request object $this->request in the controller.

have a method like checkThemeFromCookie() in your AppController and call it in beforeRender() and read the theme from the cookie, if the value is not empty simply do $this->theme = $themeFromCookie. If the cookie is empty just leave the property or set whatever default theme you want.

References

于 2013-09-16T13:37:33.640 回答