Create a custom user permission in drupal 8

By Arun AK, 21 May, 2017

You may know how to define custom permission in drupal 7. We were using hook_permission() inside *.module file like below

function mycustommodule_permission() {
  'administer mycustommodule coniguration' => array(
      'title' => t('Administer My Custom module Configuration'),
      'description' => t('Configure My Custom module settings.'),
      'restrict access' => TRUE,

In drupal 8 we are following a different method. Instead of writing hook we can define user permissions inside mycustommodule.permissions.yml.

Create mycustommodule.permissions.yml file put it inside root of your module (here root of mycustommodule module). Define your custom permisson like below

administer mycustommodule coniguration:
  title: 'Administer My Custom module Configuration'
  description: 'Configure My Custom module settings.'
  restrict access: true

Then you can use custom permission for routing like below

  path: '/admin/config/mycustommodule'
    _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
    _title: 'My Custom module'
    _permission: 'administer mycustommodule coniguration'

Make sure you are not using tab instead of space inside *.yml files.