dongwei7048 2013-09-16 13:26 采纳率: 100%
浏览 27
已采纳

CakePHP控制器动作设置cookie并传递给帮助者

I checked typical CakePHP request but couldn't find the solution.

At first myThemeCookie doesn't exist. By setting this cookie, user can select his/her favorite theme and he/she will see that theme in the future.

I have myController and myAction. I have a form in myAction.ctp. User can select theme name.

At first request myAction checks for $_POST. It can't find any value. Action only shows form.

In second request, when user submits form, I send user to myAction again. Action finds $_POST data. Getting theme css address from $_POST. By using myComponent, myThemeCookie is written.

Then I have myLayout.ctp file. This layout file uses myHelper and by using myHelper->getCookieTheme(), it gets theme name from the cookie. But helper gives empty name. So user sees old theme's fonts. (Note that I check cookie data by using $_COOKIE variable in helper)

When I refresh myAction page and make a third request I see that myHelper->getCookieTheme() gives right css file name. And user sees new theme's fonts.

So, after I set a cookie in myAction, I can't see it in my helper method and layout file. But it seems like view/helper files are calculated after controller actions. Do I miss something ?

  • 写回答

1条回答 默认 最新

  • dongshi4589 2013-09-16 13:37
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行