How do I use Url::setUnrouted() in a custom module?

If I call this method I get the following error:

Call to protected method Drupal\\Core\\Url::setUnrouted()


Url::setUnrouted() is really an internal method used by the URL class. It is used by the static constructor Url::fromUri() when the URI passed as argument isn't a route.

In general, you should create URL from one of the static constructor functions, and then use the public methods to change anything that you need. And, paths in your own code should be set up as routes, whenever possible.

If you really need to create an unrouted URL to a static resource that can't be handled through other method (eg, JS and CSS should be handled using #attached), then you would do something like the following:

$url = Url::fromUri('base:some/path/foo.txt');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.