How do I get the router name from the internal path? If I know the internal path of my page (e.g. 'node/1') how do I get the router name?

I need it it to generate an internal url:

$url = Url::fromRoute($router_name);


I found Url::getRouteName, but I'm not sure if this is what I need?

Thanks.
Have a look at PathValidatorInterface::getUrlIfValid() or the version that does not check access. That is provided by the path.validator service:

\Drupal::service('path.validator')-&gt;getUrlIfValid('node/1')