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.
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')->getUrlIfValid('node/1')
$url_object->routhName
I get an error: Cannot access protected property Drupal\Core\Url::$routeName
.
You can also do like this
$route_name = Url::fromUserInput($internal_path)->getRouteName();
More details : http://www.drupal8.ovh/en/tutoriels/275/drupal-8-url-tips-get-name-path-args