Score:1

What is best practice for passing routing parameters?

ae flag

In submitForm() I want to redirect to a controller and pass the form values. The form values are all arrays, btw.

Here is submitForm.

public function submitForm(array &$form, FormStateInterface $form_state) {
    // get values
    $demo_values = $form_state->getValue('demo');
    $data_set_value = $form_state->getValue('data_set');
    $engineering_degrees1 = $form_state->getValue('engineering_degrees1');
    $engineering_degrees2 = $form_state->getValue('engineering_degrees2');
    $engineering_degrees3 = $form_state->getValue('engineering_degrees3');
    $engineering_degrees4 = $form_state->getValue('engineering_degrees4');
    $physical_science_degrees = $form_state->getValue('physical_science_degrees');

   
    # set form data in url redirect
    $params = ['demos' => $demo_values,
      'data_set' => $data_set_value,
      'engineering_degrees1' => $engineering_degrees1,
      'engineering_degrees2' => $engineering_degrees2,
      'engineering_degrees3' => $engineering_degrees3,
      'engineering_degrees4' => $engineering_degrees4,
      'physical_science_degrees' => $physical_science_degrees];
    $form_state->setRedirect('stats_degrees.render_chart',$params);
  }
}

Here is my routing YML file.

stats_degrees.render_form:
  path: '/statistics/stats-degrees'
  defaults:
    _title: 'Engineering and Physical Science Degrees Earned by Members of Underrepresented Groups'
    _form: '\Drupal\stats_degrees\Form\StatsDegreesForm'
  requirements:
      _permission: 'access content'
stats_degrees.render_chart:
  path: '/statistics/stats-degrees-chart/{demos}/{data_set}/{engineering_degrees1}/{engineering_degrees2}/{engineering_degrees3}/{engineering_degrees4}/{physical_science_degrees}'
  defaults:
    _controller: '\Drupal\stats_degrees\Controller\StatsDegreesController::stats_degrees_chart'
  requirements:
    _permission: 'access content'

Here is my Controller method call.

public function stats_degrees_chart($demos,$data_set,$engineering_degrees1,$engineering_degrees2,$engineering_degrees3,$engineering_degrees4,$physical_science_degrees) {

Currently, it's throwing a 500 error after I submit the form. Is there a better way to pass the parameters to the controller?

Jaypan avatar
de flag
What is the error? Look in your logs.
by flag
What you're doing should work, I think. What happens if you go to /statistics/stats-degrees-chart/ with the parameter values in the URL? Can you debug to see what URL the form redirect is trying to go to?
Score:2
cn flag

What is best practice to pass the parameters to the controller is opinion based and depends on the use case, so I try to cover all possible approaches.

In general, there are three other ways to pass parameters to controllers, query string, POST data and SESSION. You could serialize the arrays for the query string, but easier would be POST or SESSION.

See How to get $_POST and $_GET parameters in a controller.

If you use session values then unset them at the end of the controller because no longer needed session values are bad for caching. See How can I destroy or unset session?.

Specifically for forms there is more. If the purpose of the form is to store the values in the database (permanently in an entity or temporarily in a tempstore), do this first and then reference the database item via a routing parameter.

If you don't want to store the data it could make sense to post the data directly to the controller because you can't attach POST data to a submit redirect.

You don't necessarily need a controller, you can rebuild the form displaying the chart below the form or replacing it. See How do I show the result on same page after submitting the form?

Score:1
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.