为什么我的 CakePHP flash 消息没有出现?
背景:我是 CakePHP 的新手。我有一个小型测试网站(目前主要由静态视图和联系表单组成)可以在学习时使用。在本地主机(Ubuntu 上的 Apache)上一切正常,因此我将其部署到共享托管帐户(由 Lunarpages 提供)。我将 /cake 文件夹移出了正常的目录结构(这样我就可以将它用于多个应用程序),并相应地重新配置了我的 webroot 的 index.php 路径。
问题:
setFlash
消息不会显示。即使制作一个简单的视图,除了$this->Session->setFlash('message');
之外什么也不做。我没有收到任何错误消息,只是不显示闪光灯重定向不起作用。例如,在联系表单完成后,我想要
<块引用>$this->redirect( array( 'action' => 'success' ), null, true);
但服务器抛出错误:警告(2):无法修改标头信息 - 标头已发送(输出从 /routetoapp/config/routes.php:40 开始)[CORE/cake/libs/controller/controller.php,第 742 行]
其他一切似乎都有效就像在 localhost 上一样 - URL 重写、组件加载、模型验证。我不知道我的问题是相关的还是单独的问题
到目前为止的故障排除:
- 我已经尝试了“cake”和“php”
Configure::write('Session.save' , 'val');
但两者都没有什么区别。 - 我的 app/tmp 文件夹是可写的。
- 我的布局模板具有用于显示闪存消息的正确代码。 (完全相同的 M、V、C 和布局对象在本地主机上显示预期的闪存)
我认为我错过了一些简单的东西,但我是这个框架的新手,所以我不知道还能在哪里查看。
Background: I'm new to CakePHP. I have a small test site (mostly composted of static views and a contact form at the moment) to play with while I learn. Everything worked fine on localhost (Apache on Ubuntu) so I deployed it to a shared hosting account (provided by Lunarpages). I moved the /cake folder out of the normal directory structure (so I could use it for multiple apps) and I reconfigured my webroot's index.php paths accordingly.
Problems:
setFlash
messages do not get displayed. Even making a simple view that does nothing other than$this->Session->setFlash('message');
. I don't get any error message, the flash just doesn't get displayedRedirects don't work. For instance, after the contact form is completed I want to
$this->redirect( array( 'action' => 'success' ), null, true);
but the server throws an error:Warning (2): Cannot modify header information - headers already sent by (output started at /routetoapp/config/routes.php:40) [CORE/cake/libs/controller/controller.php, line 742]
Everything else seems to work just as it did on localhost - URL rewriting, component loading, model validation. I don't know if my problems are related or separate issues
Troubleshooting so far:
- I've tried both 'cake' and 'php' for
Configure::write('Session.save', 'val');
but neither made a difference. - My app/tmp folder is writeable.
- My layout template has the correct code for displaying flash messages. (The exact same M, V, C, and Layout objects display the expected flash on localhost)
I assume I'm missing something simple, but I'm new to this framework so I'm not sure where else to look.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅马特·哈金斯 (Matt Huggins) 回答您的闪光灯问题。这是正确的
至于您的重定向问题,
您的
routes.php
文件中可能有多余的空格或其他内容。确保起始标记之前没有空格,并删除结束
?>
See Matt Huggins answer for your flash problem. That's correct
As for your redirect issue,
you might have an extra space or something in your
routes.php
file. Make sure there are no spaces before the starting<?php
tag and remove the closing?>
$this->Session->setFlash(...)
用于从控制器内设置 flash 消息。当您在视图中时,您应该像这样渲染 Flash 消息:如果需要,您还可以使用如下内容使 Flash 消息更加详细:
$this->Session->setFlash(...)
is used to SET the flash message from within the controller. When you're in the view, you should be rendering the flash message like this:You can also make your flash message more elaborate if you need with something like this:
没错,但是在
$session->flash();
之前添加了echo
所以,它应该是这样的:
这对我有用!
That was right but there also have
echo
add before$session->flash();
so, it should be like:
this worked for me!