Disable caching on a route I have a route with a parameter, e.g. /r/{app_id}. The controller extends ContainerInjectionInterface.  The method either returns a render array or a new TrustedRedirectResponse.

I believe I have a caching problem because subsequent visits to the page will not update even with changing {app_id}.  If I visit from another browser I get the correct response one time.

My controller is, basically, this:

if (!$a) {
  return ['#markup' =&gt; 'no'];
}

return new TrustedRedirectResponse($url);


I have tried to disable cache in several ways, to no avail:


addCacheableDependency
putting #cache =&gt; ['max-age' =&gt; 0] on the render array
\Drupal::service('page_cache_kill_switch')-&gt;trigger();
using $response = new Response() and $response-&gt;setCache(['max_age' =&gt; 0]) and then, from within the conditional, using $response-&gt;setContent('blah')


Not having luck with any of the above.
You have to do all together, set cache max-age = 0 for all responses the controller returns and apply the kill switch

public function mycontroller() {

  \Drupal::service('page_cache_kill_switch')-&gt;trigger();

  if (!$a) {
    return [
      '#markup' =&gt; 'no',
      '#cache' =&gt; ['max-age' =&gt; 0],
    ];
  }

  return (new TrustedRedirectResponse($url))
    -&gt;addCacheableDependency((new CacheableMetadata())-&gt;setCacheMaxAge(0));
}


and then clear the cache one more time.