Generating Url for a route I want to use drupal l() function, but Url doesn't work really well with a view with optional argument.

The view has a context filter, but it is optional. In the routes table it has path /knowledge-base. When I setup view's routing as /knowledge-base/% Routes table starts to receive /knowledge-base/{arg_0}, but it becomes mandatory.

Is it possible to add an argument to the route, so I would be able to construct route /knowledge-base/ID from the code (new Url('route.name', ['arg_0' =&gt; $val])), but leave it optional?
What you can do is to define the path as a non-routed path:

&lt;?
$url = Url::fromUri('base://knowledge-base');


(The base:// stuff is a bit weird IMHO, don't ask me why it's named like that. @dawehner might know ;))

This has a few limitations, some methods will then not work (like getRouteName()), and the url will not be checked for access.

The only other way I can think of is to define two displays in your view, one without an argument and the other one that requires it.