Problem/Motivation

Drupal\Core\Routing\RouteProvider lacks a routeExists method. There is a close method, getRouteByName, but it throws an exception.

Right now, the standard way of checking of a route exists, based on this answer on drupal.stackexchange.com, is this:

$exists = count($route_provider->getRoutesByNames(['abc.xyz'])) === 1;

https://drupal.stackexchange.com/a/222652/45409

It would be nice if RouteProvider had a public method such as this, or if that's not the best place, some other built in service.

Proposed resolution

It would be useful to have a method added to RouteProvider that doesn't throw an exception, called ::routeExists perhaps.

Remaining tasks

- review

Draft Record Change:
https://www.drupal.org/node/3206861

Issue fork drupal-3029545

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

oknate created an issue. See original summary.

oknate’s picture

Issue summary: View changes
oknate’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
2.16 KB

Status: Needs review » Needs work

The last submitted patch, 3: route-provider-route-exists-3029545-3.patch, failed testing. View results

oknate’s picture

oknate’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

mlncn’s picture

Status: Needs review » Reviewed & tested by the community

Patch works great. Nice API addition.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll, +Needs change record

Needs a re-roll and a change record.

oknate’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs reroll, -Needs change record

Added reroll and draft record change:
https://www.drupal.org/node/3206861

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mlncn’s picture

i can't push to the issue fork but here's another reroll as a patch

mlncn’s picture

Status: Needs review » Reviewed & tested by the community

Because this re-roll affected only a comment (preserving "Tests…" instead of the older "Test"), setting back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs review

Thanks for the reroll, looked at the patch again and a couple more questions:

1. Do we need an interface with this method for RouteProvider to implement?

2. Should we add a return type hint here since it's new code?

Praveen Saini’s picture

Status: Needs review » Needs work

Hi Catch, #18
I have reviewed the patch @#16 and it works great but it still needs some improvements.

For your queries
1. Do we need an interface with this method for RouteProvider to implement?
Yes, This method should be defined inside RouteProviderInterface. Its good to have it inside RouteProviderInterface as we can use it to extended the functionality across system.

2. Should we add a return type hint here since it's new code?
Type hint can help as it's a way of self documentation of method and also tightly restrict developers to write good quality code.

Praveen Saini’s picture

Status: Needs work » Needs review
FileSize
3.41 KB

Here is the patch according to #18

Praveen Saini’s picture

Praveen Saini’s picture

Praveen Saini’s picture

Praveen Saini’s picture

Praveen Saini’s picture

FileSize
3.99 KB

Status: Needs review » Needs work

The last submitted patch, 25: 3029545-25.patch, failed testing. View results

Praveen Saini’s picture

FileSize
4.63 KB
Praveen Saini’s picture

Status: Needs work » Needs review

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

public function routeExists($name)
This needs to be typehinted both function and parameter

public function routeExists($name)
Parameter needs typehinting

All instances of that outside the tests.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Liam Morland’s picture

Status: Needs work » Needs review
FileSize
4.68 KB

This adds type declarations and uses casting instead of count() to check whether the array is empty.

If the merge request is to to used, I think the person who created the merge request needs to update its target branch to point to 11.x.

Status: Needs review » Needs work

The last submitted patch, 34: drupal-create_routeExists-3029545-34.patch, failed testing. View results

Liam Morland’s picture

Status: Needs work » Needs review
FileSize
4.55 KB
3.07 KB
890 bytes

Fix test.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

CR looks good, updated branch numbers.

Think this is a good addition.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 36: drupal-create_routeExists-3029545-36.patch, failed testing. View results

nlisgo’s picture

Assigned: Unassigned » nlisgo
nlisgo’s picture

Assigned: nlisgo » Unassigned

Hitting retest to see if we can display a green run.

The random test failure has already been logged: #3376563: Random test fail in Drupal\Tests\Component\Utility\RandomTest::testRandomMachineNamesUniqueness

nlisgo’s picture

Status: Needs work » Reviewed & tested by the community
nlisgo’s picture

Slight improvement, imo. Not a deal breaker for me.

public function routeExists(string $name): bool {
  return !empty($this->getRoutesByNames([$name]));
}

Unless we anticipate a lot of calls to routeExists with an empty string then let's just assume the supplied string is a potential route and then explicitly declare true if not empty rather than casting.

quietone’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
2.17 KB
4.55 KB

I'm triaging RTBC issues.

I read the Issue summary, comments and the change record. I found that all questions have been answered. The CR is complete and has a before and after section which makes it easy for the reader. It is a bit brief for my tastes but it does not require a change.

I looked at the patch and ran the tests.

  1. +++ b/core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php
    @@ -655,7 +655,7 @@ public function testRouteCaching() {
    +   * Tests RouteProvider::getRouteByName(), RouteProvider::getRoutesByNames(), and RouteProvider::routeExists().
    

    The summary line is to be 80 characters max. This could be changed by using @covers for the methods being tested.

  2. +++ b/core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php
    @@ -667,6 +667,11 @@ public function testRouteByName() {
    +    $this->assertFalse($provider->routeExists(''));
    

    And since a change is being made, this could use a simple comment to help the next person working on this.

I decided to make a patch for the above and at the same time I have added the suggestion in #42.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

All green so back to RTBC

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 43: 3029545-43.patch, failed testing. View results