1

I need to send the URL of a Drupal 8 route to a template. I read some documentation on Drupal API, but when I implement this in my module, nothing happens.

My route in my routing file looks like the following.

movies.addMovie:
    path: /movies/add/{system}/{id}
    defaults:
        _controller: 'Drupal\movies\Controller\InsertController:addMovie'
        _title: 'Add Movie'
        system: ''
        id: ''
    requirements:
        _permission: 'create movies_movie'

This is the code I am using to get the URL from the route.

use Drupal\Core\Url;
Url::fromRoute('movies.addMovie')

When I put the URL in a variable, the code stops when calling Url::fromRoute(). Can someone tell me what I'm doing wrong?

  • Can you paste the entire error? – Kevin Jan 9 '17 at 16:44
  • No errors are thrown. The line is just skipped. – JimmyD Jan 9 '17 at 16:46
  • So you assign a breakpoint and set a variable to the result of that call and nothing happens? – Kevin Jan 9 '17 at 16:50
  • Indeed. When I place a breakpoint on the call and one after the call, the one after the call is not be hit. – JimmyD Jan 9 '17 at 16:52
  • 2
    Looks like you found the twig wrapp function url() -- you should use that inside twig. If you're set on using Url::fromRoute(), then you probably want to call Url::toString() on the returned Url object. Like: $url_string = Url::fromRoute('add.movie')->toString(); – aaronbauman Jan 9 '17 at 17:26
2

should be: InsertController::addMovie

Needs a double-colon. Yours is single.

1

Found a way to generate the url of my route using twig template.

I'm now using the url function of twig to generate the full url to my page:

{{ url('movies.addMovie')}}

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.