4

In Drupal 8, $_GET does not return the query parameter I am expecting to get. With the following code, I get 1 all the times, even when I expect to get 2.

namespace Drupal\newday\Controller;

use Drupal\Core\Controller\ControllerBase;

class NewdayController extends ControllerBase {

  public function new() {
    $day = [
      "#markup" => \Drupal::request()->query->get('id'),
    ];

    return $day;
  }

}

How do I get the correct query parameter?

0

1 Answer 1

10

The request is being cached, you need to tell the system to vary by the query arg:

$day = [
    '#markup' => \Drupal::request()->query->get('id'),
    '#cache' => [
        'contexts' => ['url.query_args:id'],
    ],
];

You need to rebuild cache after altering the render array to see the effects.

If you're not familiar with it already, Cacheability of render arrays is worth a read.

0

Your Answer

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