0

I want to be able to create paths like this:

/guy1
/guy2
/guy3

It's easy enough if I want /profile/guy1 ... then I can just do this:

gg_profile.viewUserProfile:
  path: '/profile/{username}'
  defaults:
    _controller: '\Drupal\gg_profile\Controller\GGProfileController::viewUserProfile'

But I want it to work with the /profile bit. Changing it as follows doesn't work:

  path: '/{username}'

How do I make it work without having the extra /profile bit?

  • Perhaps you should create routes specific to your module, so that they would use a unique pattern. Then in your module you could suggest desired route as a Pathauto pattern. So, /guy1 would be an alias to your route. – osman Apr 8 '18 at 20:28
3

You cannot: The part of the path after the first slash cannot be dynamic, so you cannot use /{username} as path. This is explicitly stated in Structure of routes, which describes path as follows.

path (required): The URL to the route, with a leading forward slash (e.g., path: '/book'). You can use dynamic properties by including them in curly braces. (e.g., path: '/node/{node}/outline'). These will be passed along as arguments via parameter converters to the controller/form (see below). Note that the first item of the path must not be dynamic.

You can use /profile/{username}, but /{username} doesn't work.

  • So to do this, I would need to not use Drupal? That sucks... – coderama Apr 8 '18 at 18:37
  • You could implement the equivalent of Drupal 7 hook_url_inbound_alter() but be careful of what you would alter, which means any route defined from other modules still have to work properly. – kiamlaluno Apr 8 '18 at 19:19
  • 1
    That is why a path like /{username} would not work. Drupal would not be able to understand if node in /node/1 is a username, or user in /user/1 is a username. – kiamlaluno Apr 8 '18 at 20:11
  • I get that, but I thought i could have custom logic and basically tell it to skip my logic if a certain condition is not met. – coderama Apr 9 '18 at 5:43
  • For that, you should alter the way Drupal checks for routes, if it is possible. By default, Drupal doesn't have an apply my logic if some conditions are met mechanism. – kiamlaluno Apr 9 '18 at 6:49
2

Perhaps the Pathauto Module could come in handy for this. https://www.drupal.org/project/pathauto

Your use case is kind of vague so I don't know exactly what you want but you can use Pathauto to create 'dynamic' URL endpoints when creating new content/users. You can use tokens within the URL to add variables.

  • I would have to use nodes if I want to do this. Which is probably what I'm going to end up doing anyway. – coderama Apr 9 '18 at 5:43
  • 2
    Pathauto supports any entity type but it looks like that is some special thing. The same still applies. You can't have top-level routes like that, but you can have aliases, you just have to create them yourself then – Berdir Apr 9 '18 at 7:28

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.