Bohdan Vorona
Bohdan Vorona

Reputation: 725

Take GET-parameter from any place of application (Symfony2)

I'm new in Symfony, but I see that it's very good framework. Now I'm working with Yii and some moments in Symfony can't understand... Sorry)

How can I take GET-parameter from any controller (for example ?ref=), processing it parameter (save REF ID in session) and return current user for current page but without this GET-parameter?

Upvotes: 1

Views: 131

Answers (2)

Crozin
Crozin

Reputation: 44386

  1. Create an event listener that listens on kernel.request event.
  2. Check if given URL contains required parameter, and handle it according to your needs.
  3. Return a redirect response to the very same URL but without ref parameter:

    public function onKernelRequest(GetResponseEvent $event) {
        if (!$event->isMasterRequest()) {
            return;
        }
    
        if (!$event->getRequest()->query->has('ref')) {
            return;
        }
    
        $ref = $event->getRequest()->query->get('ref');
    
        // do whatever you need
    
        $url = ...; // prepare redirect URL
        $event->setResponse(new RedirectResponse($url));
    }
    

Upvotes: 2

Oleg Yemelyanov
Oleg Yemelyanov

Reputation: 34

You need to use next construction:

http://example?ref=123


public function indexAction(Request $request)
{
    $ref = $request->query->get('ref');
}

Upvotes: 0

Related Questions