Can I generate a
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x
from a simple path, e.g. "/products/widget" ?
(assume /products/widget exists in the system and is a canonical path)
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityCan I generate a
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x
from a simple path, e.g. "/products/widget" ?
(assume /products/widget exists in the system and is a canonical path)
Use Url:fromRoute () and pass in the route's name and arguments. You can also use fromUri() for external paths. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromRoute/8.2.x
I usually find the route name by searching the routing.yml files for the path.
Alternately, you can do this. How to get the Route object from an URI?