In my module.routing.yml (and using D8), I have a module_new route associated with the /module/name path.
I want to render this path in my module. How can I show http://example.org/module/name on a page?
In my module.routing.yml (and using D8), I have a module_new route associated with the /module/name path.
I want to render this path in my module. How can I show http://example.org/module/name on a page?
Twig
You can use the route name in the twig function url()
:
<a href="{{ url('module_new') }}">{{ 'View my module content'|t }}</a>
Controller
Or you can do the same in a controller:
use Drupal\Core\Url;
$url = Url::fromRoute('module_new', [], ['absolute' => TRUE]);
// get the path as string (not recommended)
$path = $url->toString;
// better put the complete url object in a render array and let the template render it
$build['link'] = [
'#title' => $this->t('My module content'),
'#type' => 'link',
'#url' => $url,
];
return $build;