Drupal - How do I get parameters from form redirect?
Ok i got it by this way :
So i have 2 parameters from my form to pass to my controller :
public function submitForm(array &$form, FormStateInterface $form_state)
{
$metier=$form_state->getValue('metier');
$position=$form_state->getValue('position');
$url = \Drupal\Core\Url::fromRoute('recherche.resultat')
->setRouteParameters(array('metier'=>$metier,'position'=>$position));
$form_state->setRedirectUrl($url);
}
The key is in the YAML routing, you need to build your path like that for get parameters ( name of parameters need to be same as your route, see previous function (submitForm)) :
recherche.resultat:
path: '/resultat/{metier}/{position}'
defaults:
_title: 'resultat'
_controller: '\Drupal\recherche\Controller\ResultatRechercheController::resultat'
metier: ''
position: ''
requirements:
_permission: 'access content'
And in my controller for get my parameters :
Class: ResultatRechercheController
public function resultat($metier, $position)
{
kint($metier);
kint($position);
return array('#markup'=>t('la premiere spécialite est @specialite',array('@specialite'=>'test')));
}
And the URL is clean :
localhost:8888//monsite.com/resultat/{metier}/{position}
You can get the get attributes directly from the request object \Drupal::request()->attributes->get('param');
or \Drupal::request()->query
Source: https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3Arequest/8