New answers tagged redirection
3
If you want to run code before the Drupal kernel is started use a middleware.
Use the drush code generator drush gen middleware and put your code in handle() of the generated middleware class:
MymoduleMiddleware.php
<?php
namespace Drupal\mymodule;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\Request;
...
2
In general, never send responses from custom code. See How do I set a cookie using $response->headers->setCookie() in a form submission handler?
In this case you are sending a redirect response to the client while Drupal builds the real response containing the cookie to the session where the message gets stored. The real response never reaches the ...
3
The code depends on host name and IP address. So you could add the cache contexts url.site and ip:
$response = new TrustedRedirectResponse('https://site.ch/');
$response->getCacheableMetadata()->addCacheContexts(['ip', 'url.site']);
$event->setResponse($response);
In this case, though, caching doesn't make much sense because it doesn't save many ...
Top 50 recent answers are included
Related Tags
redirection × 8137 × 254
8 × 156
users × 126
forms × 86
uri × 73
nodes × 50
rules × 49
views × 42
.htaccess × 41
path-aliases × 39
6 × 36
routes × 23
i18n-l10n × 21
webforms × 19
hooks × 18
caching × 14
taxonomy-terms × 13
multi-sites × 12
navigation × 11
files × 10
ssl × 10
commerce × 9
domain-access × 9
event-subscribers × 9