symfony2重定向来自侦听器的每个请求都不起作用

时间:2014-10-16 09:02:43

标签: symfony redirect request listener

我尝试将每个请求重定向到特定表单。如果我的用户不接受表格,他就不能去任何地方。我看到了很多tuto,但是当我尝试重定向时,他只是多次加载我的页面或者抛出“页面没有正确重定向”。

config.yml:

project.listener.before_request:
    class: Project\ClientBundle\Listener\BeforeControllerListener
    arguments: ["@router", "@security.context"]
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

BeforeControllerListener.php     

namespace Project\ClientBundle\Listener;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Project\ClientBundle\Model\InitializableControllerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;

class BeforeControllerListener
{  
    protected $security_context;
    private $router;

     public function __construct($router, SecurityContextInterface $security_context)
     {
         $this->router = $router;
         $this->security_context = $security_context;
     }

    public function onKernelRequest( getResponseEvent $event ){
        $user = $this->security_context->getToken()->getUser();

        $route = 'confidential';

        if ( HttpKernel::MASTER_REQUEST != $event->getRequestType() || !is_object($user)  || $route == $event->getRequest()->get('_route') ) {
            // don't do anything if it's not the master request
            return;
        }else{
            $redirectUrl = $this->router->generate( $route );

            $event->setResponse(new RedirectResponse($redirectUrl));    
        }


    }
}

有什么想法吗?感谢


好的,页面加载正确,但没有css。也许布局有问题? 该错误仅在重定向后出现。

<head>
<meta charset="UTF-8" />
{% block head_style %}
    <link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Oxygen+Mono' rel='stylesheet' type='text/css'>
    {% stylesheets
        'bundles/bduclient/css/mopabootstrapbundle.css'
        'bundles/bduclient/css/redactor.css'
        'bundles/bduclient/css/font-awesome.min.css'
        'bundles/bduclient/css/select2-bootstrap.css'
        'bundles/bduclient/css/jquery-ui-1.9.2.custom.min.css'
        'bundles/bduclient/css/select2.css'
        'bundles/bduclient/css/simple-sidebar.css'
        'bundles/bduclient/css/bootstrap-datetimepicker.min.css'
        'bundles/bduclient/css/datepicker3.css'
        'bundles/bduclient/css/style.css'
    %}
    {# 
        filter='less, cssrewrite, yui_css' #}

    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen" />

    {% endstylesheets %}

{% endblock head_style %}

<title>{% block title %}Base de Donnée Universelle{% endblock title %}</title>
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />

{% block head_bottom %}
{% endblock head_bottom %}

对我来说这很好。它适用于除此之外的所有页面..

我在firebug控制台中多次出现此错误:

SyntaxError: syntax error <!DOCTYPE html>

但仅限于重定向后的此页面..


谢谢,我试过这个,因为我在用户登录时重定向,如果我不在机密页面上。但仍然多次加载页面,但现在他加载页面保密而没有css / js。

if( HttpKernel::MASTER_REQUEST != $event->getRequestType() ){
        // skip redirect if its not the main request
        return;
    }else if( is_object( $user )  && $route != $event->getRequest()->get('_route')){
        // redirect if its the main request and an anonymous user asks for 'confidential' route
        $redirectUrl = $this->router->generate( $route );

        $event->setResponse(new RedirectResponse($redirectUrl));   
    }

2 个答案:

答案 0 :(得分:1)

尝试更改

( HttpKernel::MASTER_REQUEST != $event->getRequestType() || ... || ... || ...)

if( HttpKernel::MASTER_REQUEST != $event->getRequestType() )
    // skip redirect if its not the main request
else if(!is_object($user)  && $route == $event->getRequest()->get('_route'))
    // redirect if its the main request and an anonymous user asks for 'confidential' route

答案 1 :(得分:0)

也许您可以尝试在onKernelRequest方法的开头添加这些行:

    if( $event->getRequest()->getRequestFormat() == 'css' || $event->getRequest()->getRequestFormat() == 'js' ) )
    {
        return;
    }

它不会考虑你的js和css文件。