Armi me
Armi me

Reputation: 11

symfony2 redirect every request from listener doesn't works

i try to redirect every request to a specific form. If my user don't accept the form, he can't go anywhere. I saw a lot of tuto but when i try to redirect he just loading my page many times or throw me "The page is not redirected properly".

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));    
        }


    }
}

Any idea ? Thanks


Ok, the page is loaded correctly, but no css. Maybe something is wrong in the layout ? The bug appear only after redirect.

<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 %}

To me this is good. It works for all the page except this one..

I have this error several times in firebug console :

SyntaxError: syntax error <!DOCTYPE html>

But only on this page after redirect..


Thanks, i tried this, because i redirect when my user is logged and if i'm not on confidential page. But still loading the page many times but now he load the page confidential without 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));   
    }

Upvotes: 0

Views: 1981

Answers (2)

Thomas
Thomas

Reputation: 54

Maybe you can try to add these lines at the beginning of your onKernelRequest method:

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

It will not take into account your js and css files.

Upvotes: 0

Bartłomiej Wach
Bartłomiej Wach

Reputation: 1986

try changing

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

into

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

Upvotes: 1

Related Questions