php - 德鲁帕尔 8 : Custom permission not working properly

标签 php drupal drupal-modules drupal-8

我是 Drupal 8 的新手,目前正在尝试为其创建一个模块。然而,花了几个小时,我仍然无法让它发挥作用。

以下是其中的两个文件:

#testmodule.routing.yml
testmodule.testmodule:
  path: '/testmodule'
  defaults:
    _title: 'Index'
    _content:
      '\Drupal\testmodule\Controller\TestModuleController::testmodule'
    requirements:
      _permission: 'access content right away'


#testmodule.permissions.yml
access content right away:
    title: 'Access content'
    description: 'Access content right away.'

当我访问/testmodule时,它一直告诉我访问被拒绝。有人知道这个模块出了什么问题吗?

提前致谢。

最佳答案

routing.yml 文件的缩进存在问题。 yml 文件中的缩进非常重要。下面是正确缩进的routing.yml 文件:

#testmodule.routing.yml
testmodule.testmodule:
  path: '/testmodule'
  defaults:
    _title: 'Index'
    _content: '\Drupal\testmodule\Controller\TestModuleController::testmodule'
  requirements:
    _permission: 'access content right away'

发生的情况是,由于缩进错误,无法正确解析路由要求(以及权限),因此出现访问被拒绝错误。

请注意,如果您没有定义要求_permission(或者您拼写错误),您会收到拒绝访问错误。

关于php - 德鲁帕尔 8 : Custom permission not working properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26417218/

相关文章:

php - 如何返回一个空的 Doctrine_Collection?

php - Drupal 7 中具有多个文件字段的自定义字段

javascript - 在 Drupal 7 中使用 JQuery

php - 如何在没有 PDO 的情况下使 Drupal 工作

php - 在 Drupal 中使用美国军用通用访问卡 (CAC) 个人身份验证 (PIV) 进行身份验证

javascript - 将 .submit() 与 CakePHP 一起使用似乎不起作用

Javascript::将回调后的 JSON 字符串转换为纯 JavaScript 数组

php限制函数调用某些类

javascript - 使用 Jquery 了解 Drupal 7 表单中的匿名用户

json - 如何将 Drupal 服务默认响应格式更改为 JSON?