How to get the request parameters in Symfony 2?
The naming is not all that intuitive:
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
// $_GET parameters
$request->query->get('name');
// $_POST parameters
$request->request->get('name');
Update Nov 2021: $request->get('name') has been deprecated in 5.4 and will be private as of 6.0. It's usage has been discouraged for quite some time.
Getting all request parameters in Symfony 2
You can do $this->getRequest()->query->all();
to get all GET params and $this->getRequest()->request->all();
to get all POST params.
So in your case:
$params = $this->getRequest()->request->all();
$params['value1'];
$params['value2'];
For more info about the Request class, see http://api.symfony.com/2.8/Symfony/Component/HttpFoundation/Request.html
Symfony Request get all url Parameters
Did you try:
$parametersToValidate = $request->query->all();
Get parameters from URL in Controller Symfony2
Do like this, if you can't find QS values:
var_dump($request->query->all());
it shows all QS values;
In your case, you can access your QS data by:
$formData = $request->query->get('search_form');
or
$formData = $request->query->get($form->getName());
it returns array of values, after that you can access startPoints
like this:
$formData['startPoints']
Symfony2 retrieve the parameter values from URL
use Symfony\Component\HttpFoundation\Request;
.
.
public function MyAction()
{
$request = $this->getRequest();
$amount =$request->attributes->get('amount'); //replace this line in above code
echo 'Amount ='.$amount;
exit;
}
Symfony2: How to get request parameter
Not a good thing but you can still do. Normally you would pass the info to Twig from the controller like @Maerlyn pointed out.
Get route parameters in Twig.
{{ app.request.attributes.get('_route_params') }}
AND
Gets whole bundle name in Twig.
{{ app.request.attributes.get("_controller") }}
Get route name in Twig.
{{ app.request.attributes.get('_route') }}
How to get all post parameters in Symfony2?
$this->get('request')->request->all()
Symfony2 Form overwrites 'GET Request Parameters'
@Thiago Rodrigues answer pushed me towards the right direction. Here is how I solved it.
I added the sort
and direction
fields as hidden fields to my form.
class AttributeFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($request->get('sort')) {
$builder->add('sort', 'hidden', array(
'data' => $request->get('sort'),
));
$builder->add('direction', 'hidden', array(
'data' => $request->get('direction'),
));
}
}
}
To make sure that the Values of the Form are not overwritten by old legacy values of the form. I made changes to the request, as follows (note: attributeFilter is the name of my Form):
/**
* This function will analyze the given request.
*
* It will correct and set the settings of the given sort Values
*
* If the request contains sort and attributeFilter, always take the sort and overwrite the attributeFilter
* If only the attributeFilter is given, set the sort=Attributefilter
*
* @param Request $request
*/
protected function manipulateParameterBag(Request $request)
{
$parameterBag = $request->query;
$sort = $parameterBag->get('sort');
$direction = $parameterBag->get('direction');
$attributeFilter = $parameterBag->get('attributeFilter');
if ($sort && $direction) {
if ($attributeFilter) {
$attributeFilter['sort'] = $sort;
$attributeFilter['direction'] = $direction;
$parameterBag->set('attributeFilter', $attributeFilter);
}
} elseif ($attributeFilter) {
$parameterBag->set('sort', $attributeFilter['sort']);
$parameterBag->set('direction', $attributeFilter['direction']);
}
$request->query = $parameterBag;
return $request;
}
Hope this helps the next person that tries to accomplish the same.
Related Topics
PHP How to Retrieve Array Values
Display Custom Order Meta Data Value in Email Notifications Woocommerce
Accessing Laravel .Env Variables in Blade
Why, Fatal Error: Class 'Phpunit_Framework_Testcase' Not Found in ...
Create an Order Programmatically with Line Items in Woocommerce 3+
How to Customize Fos Userbundle Urls
How to Display Currency in Indian Numbering Format in PHP
Reflectionexception: Class Classname Does Not Exist - Laravel
Php, Curl Post to Login to Wordpress
Scale Image Using PHP and Maintaining Aspect Ratio
Bad Request. Connecting to Sites via Curl on Host and System
How to Escape Single-Quote (Apostrophe) in String Using PHP
How to Print SQL Statement in Codeigniter Model
How to Create a Twig Custom Tag That Executes a Callback