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' => $val])), but leave it optional?

1 Answer 1


What you can do is to define the path as a non-routed path:

$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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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