Getting request parameters in Symfony 2
Posted by Surya kant - over 11 yearsagoGetting request parameters in Symfony 2 Symfony provides methos using that you can get request values... 1) To find the GET params you can use // It will return all GET params
$this->getRequest()->query->all(); // It will return GET params by name $this->getRequest()->query->get('foo'); 2) To find the POST params you can use // It will return all GET params
$this->getRequest()->request>all(); // It will return GET params by name $this->getRequest()->request>get('foo'); Request and response other object $request->query->get('foo'); //gets foo GET var. $request->server->get('HTTP_HOST'); //server variables. $request->headers->get('content-type'); $request->getLanguages(); Working with the session You can manage session attributes with: $session = $this->getRequest()->getSession();
or the shortcut version $this->get('session');
and to work with the data: $session->get('foo','default value');
$session->set('foo','bar'); Redirecting in a controller: $this->redirect($this->generateUrl("homepage"));
Rendering text from a controller: return new Response('…');
Forwarding: return $this->forward('Bundle:Controller:Action');
Redirect to 404 not found: throw $this->createNotFoundException(message);
Flash messages Flash messages only last one request and they are stored in a FlashBag: $this->get('session')->getFlashBag()->add('notice','message');
To iterate trough all flash messages in a template you can use: {% for flashMessage in app.session.flashbag.get('notice') %} {{ flashMessage }} {% endfor %} Say Somthing about This Blog
|