Skip to content

Instantly share code, notes, and snippets.

@morsok
Last active February 28, 2022 10:22
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save morsok/5db2fb9dfb09757b9fdeb36ed5ad2540 to your computer and use it in GitHub Desktop.
Save morsok/5db2fb9dfb09757b9fdeb36ed5ad2540 to your computer and use it in GitHub Desktop.
Drupal 8 : Do something based on the referer if it's your website or another.
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
function my_function() {
$previousUrl = \Drupal::request()->server->get('HTTP_REFERER');
$fake_request = Request::create($previousUrl);
$url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri());
if ($url_object) {
$route_name = $url_object->getRouteName();
// Do something with $route_name;
}
@kalpaitch
Copy link

Yea this only validates the path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment