Symfony 1.4和pjax(ajax pushstate)?

时间:2022-10-25 21:03:41

symfony: http://www.symfony-project.org pjax: https://github.com/defunkt/jquery-pjax

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).

我正在尝试在symfony中使用pjax来加速我们的网站(我们将能够在大多数时间保持页眉和页脚静态,并且还避免重新加载大量的css / js和其他文件)。

I have no problem with ajax or symfony, but I want to know if there is a better way:

我对ajax或symfony没有问题,但我想知道是否有更好的方法:

  1. Is it a good idea to use postExecute to return the html code back right away without sf going to the template at all
  2. 使用postExecute立即返回html代码是一个好主意,而不需要转到模板
  3. If so, can I somehow write this only once for all modules? I imagine that I can do:

    如果是这样,我可以以某种方式只为所有模块写一次吗?我想我能做到:

    mySfActions extends sfActions

    mySfActions扩展了sfActions

    moduleActions extends mySfActions

    moduleActions扩展了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?

我想知道是否有更好的方法? 3.有没有办法在控制器/动作中获取当前布局名称(在模块的view.yml中定义)?

3 个解决方案

#1


1  

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:

问题1:不要像这样使用后执行。如果你需要在动作中从ajax调用中返回html,那么你的动作应该像这样返回:

return $this->renderText("<p>Your html result.</p>");

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.

问题2:这是正确的。您已经编写了一次编写函数的最佳方法,并将其用于所有模块操作。

#2


0  

There is nothing to do.

没有什么可以做的。

When calling an action via XmlHttpRequest, symfony automaticaly skip the Layout render, and only return the module render.

当通过XmlHttpRequest调用一个动作时,symfony会自动跳过Layout渲染,只返回模块渲染。

You need to put all your "static" assets and html in your layout and that's all.

你需要将所有“静态”资产和html放在你的布局中,这就是全部。

#3


0  

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:

首先,我扩展了sfActions类,所以我不必在每个模块上添加preExecute:

<?php 

class mySfActions extends sfActions{
    public function preExecute(){
        $request = $this->getRequest(); 
        if ($request->getParameter('_pjax')) {
            $this->setLayout(false);
        }       
    }
}

Then of course each of my module action class must extend this new class.

当然,我的每个模块动作类都必须扩展这个新类。

Inside my individual template I have something like this:

在我的个人模板中,我有这样的事情:

<?php if($sf_request->getParameter('_pjax')):?>
         <script type="text/javascript" src="/js/question_list.js"></script>
<?php endif;?>

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)

这对我来说似乎运行得很好,我很享受支持pushstate时令人难以置信的加载速度,并且当它不支持时仍然可以回退(例如在愚蠢的IE上)

#1


1  

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:

问题1:不要像这样使用后执行。如果你需要在动作中从ajax调用中返回html,那么你的动作应该像这样返回:

return $this->renderText("<p>Your html result.</p>");

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.

问题2:这是正确的。您已经编写了一次编写函数的最佳方法,并将其用于所有模块操作。

#2


0  

There is nothing to do.

没有什么可以做的。

When calling an action via XmlHttpRequest, symfony automaticaly skip the Layout render, and only return the module render.

当通过XmlHttpRequest调用一个动作时,symfony会自动跳过Layout渲染,只返回模块渲染。

You need to put all your "static" assets and html in your layout and that's all.

你需要将所有“静态”资产和html放在你的布局中,这就是全部。

#3


0  

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:

首先,我扩展了sfActions类,所以我不必在每个模块上添加preExecute:

<?php 

class mySfActions extends sfActions{
    public function preExecute(){
        $request = $this->getRequest(); 
        if ($request->getParameter('_pjax')) {
            $this->setLayout(false);
        }       
    }
}

Then of course each of my module action class must extend this new class.

当然,我的每个模块动作类都必须扩展这个新类。

Inside my individual template I have something like this:

在我的个人模板中,我有这样的事情:

<?php if($sf_request->getParameter('_pjax')):?>
         <script type="text/javascript" src="/js/question_list.js"></script>
<?php endif;?>

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)

这对我来说似乎运行得很好,我很享受支持pushstate时令人难以置信的加载速度,并且当它不支持时仍然可以回退(例如在愚蠢的IE上)