Do you want to know how to Drupal?

Let's Drupal

How to get a route by URI in Drupal?

If you have a basic path we can get route using router.no_access_checks service. Keep in my you have to have a leading slash

$router = \Drupal::service('router.no_access_checks');
$result = $router->match('/node/2');

In case you have URL object we can get route from it like this

$router = \Drupal::service('router.no_access_checks');
$result = $router->match($url->toString());