I want to assign multiple roles in my routing.yml file :

requirements: _role: "['role1', 'role2', 'role3']"

This is what I currently have, and it doesn't work.

Can someone tell me how to add this?

Thanks in advance!

  • use permissions, not roles.
    – sonfd
    Jul 28 at 14:54

I would strongly advise using permissions, not roles. Permissions are super simple to create in D8+, see How do you create a custom permission to administer a custom module?

The Structure or routes documentation says:

_role: A specific Drupal role, for example 'administrator'. You can specify multiple ones via "," for AND and "+" for OR logic.

For example, _role: organizer,participant,controller means a Drupal user needs to have all three roles to access the page. Note that, since the roles available may differ between sites, it's recommended to use permission-based access restriction when possible.

So if a user needs all three roles, it's:

_role: 'role1,role2,role3'

or if they just need one of the three roles, it's:

_role: 'role1+role2+role3'
  • It works! Thanks a lot guys!
    – metalac
    Jul 28 at 15:03

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.