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];

Here is my routing YML file.

  path: '/statistics/stats-degrees'
    _title: 'Engineering and Physical Science Degrees Earned by Members of Underrepresented Groups'
    _form: '\Drupal\stats_degrees\Form\StatsDegreesForm'
      _permission: 'access content'
  path: '/statistics/stats-degrees-chart/{demos}/{data_set}/{engineering_degrees1}/{engineering_degrees2}/{engineering_degrees3}/{engineering_degrees4}/{physical_science_degrees}'
    _controller: '\Drupal\stats_degrees\Controller\StatsDegreesController::stats_degrees_chart'
    _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?

  • What is the error? Look in your logs.
    – Jaypan
    Commented Jul 27, 2021 at 7:14
  • 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?
    – joachim
    Commented Jul 27, 2021 at 11:01

2 Answers 2


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?


You can use a private tempstore. See for example http://karimboudjema.com/en/drupal/20190315/saving-temporary-values-form-private-tempstore-drupal-8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.