简体   繁体   中英

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?

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

You need to use next construction:

http://example?ref=123


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM