Drupal 8 - path placeholders

Submitted by superadmin on Wed, 12/30/2015 - 11:37

In Drupal 8 we can have placeholders in our path. In Drupal 7 these were either wildcard arguments and they were sometimes autoloaded. The process of autoloading placeholders is called upcasting and it's a bit more involved in Drupal 8 and will cover it at a later post.

Placeholders will be replaced with values provided in the url and will be passed as arguments to the controller method. The name of the placeholders and parameter names must match, this is important because if they don't an exception will be thrown.

path placeholder

Our controller method looks like this

path placeholder controller method

Visiting whoami/John will display the following

path placeholder display

If no value for the placeholder is provided in the URL it will result in a 404 error.  If we want the path to still work and make the place holder optional you need to provide a default value for the placeholder in our .routing.yml

path placeholder default value

Now visting just '/whoami' will result in the following output

path placeholder default value output



Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.