duanqiao1947 2014-10-13 16:57
浏览 17
已采纳

Symfony2是否给出了请求(主要和次要)的错误印象,或者我弄错了

When a user hits submit button in a webform (for instance, to insert data into database) event listener treats it as Master POST request whcih is fine. After successfull submission (data inserted into db), the logic return $this->redirect($this->generateUrl('home')); we have written in controller redirects user to success or fail page and this is also treated as Master GET request. Is it not supposed to be Sub GET request instead because user doesn't directly cause it?

Either I get whole stuff wrong or the answers (what is the difference between MASTER / SUB REQUEST in Symfony2?) here are short/wrong.

And full Doc is here.

The reason I'm asking is I want to avoid allocating system resources when redirect happens.

SERVICE:

services:
    kernel.listener.kernel_request:
        class: Site\MainBundle\EventListener\Request\KernelRequest
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

EVENT LISTENER CLASS:

<?php

namespace Site\MainBundle\EventListener\Request;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class KernelRequest
{
    public function onKernelRequest(GetResponseEvent $event)
    {
        if ($event->isMasterRequest() === true) {
            // Do something with Master request
        } else {
            // Do something with sub request
        }
    }
} 

CIONTROLLER:

publich function saveAction()
{
    // Insert user's POST stuff into database
    // Then either redirect to success or fail page like;
    return $this->redirect($this->generateUrl('success-or-fail-page'));
}
  • 写回答

1条回答 默认 最新

  • duanli0453 2014-10-13 17:43
    关注

    Redirect returns a redirect response with 302 status by default and that's it. Finito. Kernel shuts down.

    Then User's browser get's redirected to your success-or-fail-page and you're receiving another regular master request, as you would if User just came to that page directly.

    If you in fact want to issue a subrequest you should then do:

    publich function saveAction()
    {
        return $this->forward('YourBundle:YourController:action', ['params' => 'here']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的