MatchingRouteNotFoundException is deprecated and is not thrown so why are we catching it

Created on 25 August 2025, 4 days ago

Problem/Motivation

In \Drupal\redirect\Form\RedirectForm::prepareEntity we do

      if (!empty($redirect_url)) {
        try {
          $redirect->setRedirect($redirect_url, $redirect_query, $redirect_options);
        }
        catch (MatchingRouteNotFoundException) {
          $this->messenger()->addMessage($this->t('Invalid redirect URL %url provided.', ['%url' => $redirect_url]), 'warning');
        }
      }

But MatchingRouteNotFoundException is not thrown but the called code.

Steps to reproduce

We should test set a redirect to an invalid URL and we should also improve the code comments in

  public function setRedirect($url, array $query = [], array $options = []) {
    $uri = $url . ($query ? '?' . UrlHelper::buildQuery($query) : '');
    $external = UrlHelper::isValid($url, TRUE);
    $uri = ($external ? $uri : 'internal:/' . ltrim($uri, '/'));
    $this->redirect_redirect->set(0, ['uri' => $uri, 'options' => $options]);
  }

Because $external = UrlHelper::isValid($url, TRUE); is very odd. What is happening here is if the URL is not an valid absolute URL we assume it is an interal URL and slap 'internal:/' on the front of it... that feels like code that needs a comment :)

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom alexpott 🇪🇺🌍

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024