Symfony 1.4 和 pjax (ajax Pushstate)?
symfony: http://www.symfony-project.org pjax: https://github.com/defunkt/jquery-pjax
大家好,
我是尝试在 symfony 中使用 pjax 来加速我们的网站(我们将能够在大部分时间保持页眉和页脚静态,并且还避免重新加载大量 css/js 和其他文件)。
我对 ajax 或 symfony 没有问题,但我想知道是否有更好的方法:
- 使用 postExecute 立即返回 html 代码而不需要 sf 去模板是一个好主意
如果是这样,我可以以某种方式为所有模块只写一次吗?我想我能做到:
mySfActions 扩展了 sfActions
moduleActions 扩展 mySfActions
我想知道是否有更好的方法? 3. 有没有办法在控制器/操作中获取当前布局名称(在模块的 view.yml 中定义)?
symfony: http://www.symfony-project.org
pjax: https://github.com/defunkt/jquery-pjax
Hi all,
I'm trying to use pjax in symfony in order to speed up our website (we will be able to keep header and footer static most of the time, and also avoid reloading lots of css/js and other files).
I have no problem with ajax or symfony, but I want to know if there is a better way:
- Is it a good idea to use postExecute to return the html code back right away without sf going to the template at all
If so, can I somehow write this only once for all modules? I imagine that I can do:
mySfActions extends sfActions
moduleActions extends mySfActions
I wonder if there is a better way?
3. Is there a way to get the current layout name (defined in the module's view.yml) within the controller/action?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题 1:不要像那样使用 post-execute。如果您需要从操作中的 ajax 调用返回 html,那么您的操作应该像这样返回:
这将跳过模板调用。
问题2:这是正确的。您已经编写了编写一次函数并使其可用于所有模块操作的最佳方法。
Question 1: Don't use post-execute like that. If you need to return html from an ajax call in your action then your action should return like this:
This will skip the template call.
Question 2: That is correct. You have written the best way to write a function once and have it available to all module actions.
无事可做。
当通过 XmlHttpRequest 调用操作时,symfony 会自动跳过布局渲染,只返回模块渲染。
您需要将所有“静态”资源和 html 放入布局中,仅此而已。
There is nothing to do.
When calling an action via XmlHttpRequest, symfony automaticaly skip the Layout render, and only return the module render.
You need to put all your "static" assets and html in your layout and that's all.
感谢大家对我的帮助,你们的所有回答都很有帮助,并为我指明了正确的方向。我想对两个答案都投票,但由于我只能接受一个答案,所以我接受了第一个答案。
无论如何,这就是我所做的:
首先,我扩展了 sfActions 类,这样我就不必在每个模块上添加 preExecute:
然后当然,我的每个模块操作类都必须扩展这个新类。
在我的个人模板中,我有这样的内容:
目前这似乎对我来说效果很好,当支持pushstate时我很享受令人难以置信的加载速度,并且在不支持时仍然能够回退(例如在愚蠢的IE上)
Thank you all for helping me, all your answers were helpful and pointed me to the right direction. I wanted to vote for both answers but since I can only accept one, I accepted the very first answer.
Anyhow, here is what I did:
First, I extended the sfActions class so I don't have to do add preExecute on every module:
Then of course each of my module action class must extend this new class.
Inside my individual template I have something like this:
This currently seems to work quite well for me, I'm enjoy the incredible loading speed when pushstate is supported, and still able to fallback when it is not (on the dumb IE for example)