【问题标题】:Dynamically Loading Plugin Configuration Files in CakePHP 3在 CakePHP 3 中动态加载插件配置文件
【发布时间】:2018-02-11 19:02:00
【问题描述】:

问题:如何从插件/配置目录加载配置文件?

演示项目:https://github.com/CakePHPKitchen/CakeDC-Users-Permissions-Example

我正在使用 CakeDC/users 插件,它有一个 permissions.php 文件,它可以从中加载 RBAC 权限。据我所知,它要么加载用户插件配置文件夹中的默认权限文件,要么从 app/config 文件夹加载 permissions.php 文件。

现在对于我的应用程序框架,我在 app/config/permissions.php 中有一堆权限,但是,我不想修改该文件,因为我将从上游 repo 执行 git pulls,我想避免冲突。

所以我想做的是,在应用程序骨架引导中

我愿意

foreach(Plugin::loaded() as $plugin) {

     $path = Plugin::path($plugin) . 'config/permissions.php';

     if(file_exists($path)) {

        Configure::load($path, 'default', true);
     }
}

但我收到以下错误....

 Error: The application is trying to load a file from the /Users/jlroberts/Projects/JeffreyLRobertsCom/CakePHPKitchen/PluginDemos/plugins/SharpAgent/config/permissions plugin. 

 Make sure your plugin /Users/jlroberts/Projects/JeffreyLRobertsCom/CakePHPKitchen/PluginDemos/plugins/SharpAgent/config/permissions is in the /Users/jlroberts/Projects/JeffreyLRobertsCom/CakePHPKitchen/PluginDemos/plugins/ directory and was loaded.

关于如何从 Plugin/config 目录加载 permissions.php 文件的任何想法?

【问题讨论】:

    标签: cakephp cakephp-3.x cakedc cakephp-3.4


    【解决方案1】:

    已编辑:您可以像现在一样从插件加载 permissions.php 文件,但更改 permissions.php 的内容以保留配置中定义的现有权限,例如:

    config/permissions.php

    $permissions = [
        // add your app permissions here
        [
            // ...
        ],
    ];
    
    // there are more permissions in this config key, defined across your plugins
    $morePermissions = \Cake\Core\Configure::read('MyPermissions');
    $allPerms = array_merge($permissions, $morePermissions);
    
    return ['CakeDC/Auth.permissions' => $allPerms];
    

    然后在每个插件中你可以拥有:

    YOUR_PLUGIN/config/bootstrap.php

    $permissions = \Cake\Core\Configure::read('MyPermissions');
    $someMorePermissions = [
        [
            // permissions injected into the app from this plugin
        ]
    ];
    
    $permissions = array_merge((array)$permissions, $someMorePermissions);
    \Cake\Core\Configure::write('MyPermissions', $permissions);
    

    允许每个插件动态地向应用程序注入/管理权限。

    我在这里使用此代码创建了一个 c9.io 环境https://ide.c9.io/steinkel/users-35-custom-permissions

    【讨论】:

    • 我创建了一个演示项目,它有您在示例插件 bootstrap.php 中推荐的代码...github.com/CakePHPKitchen/CakeDC-Users-Permissions-Example
    • 插件引导加载时权限尚未添加到配置中,如果我在引导程序中为插件设置权限,它们最终会被覆盖...
    • 在我看来,在 CakeDC/auth 中的 SimpleRbacAuthorize.php 的构造函数被触发之前,permissions.php 文件不会被加载......对我来说,就像你所说的那样工作,它需要从 CakeDC/auth 或 CakeDC/users 的引导程序加载
    • 花了一个月的时间才重新开始,看起来很棒,非常感谢!
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 2017-12-02
    • 2013-01-27
    • 1970-01-01
    • 2013-06-22
    • 2015-06-24
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多