1

I've a view with a page display on url "progetti".

On my controller I'm trying to create a link to this url but I can't find the right way. I've added \Drupal\Core\Url namespace on top of the file.

I'm creating an items list:

$url = Url::fromRoute('progetti');
$variables['items'][]['value'] = array(
  '#type' => 'link',
  '#title' => array('#value' => 'sergio'),
  '#url' => $url,
);

It doesn't work, also if I use "/progetti" as argument.

Logs say:

PHP Exception Symfony\\Component\\Routing\\Exception\\RouteNotFoundException: "Route "progetti" does not exist."
2

As you want to create url object from a path instead of route, use the function fromUri() instead of fromRoute().

$url  = Url::fromUri('internal:/progetti');
$variables['items'][]['value'] = array(
  '#type' => 'link',
  '#title' => array('#value' => 'sergio'),
  '#url' => $url,
);

In fromUri() function you need to pass the URI of the resource including the scheme. For user input that may correspond to a Drupal route, use internal: for the scheme.

  • 2
    It would be best to explain why the use of internal:/ or fromUri instead of just providing a working code answer. – mradcliffe Jul 28 '16 at 11:45

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.