在CakePHP 3中动态加载插件配置文件

时间:2017-09-03 09:01:46

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

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

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

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

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

所以我想做的是,在app skeleton bootstrap中

我想

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文件的任何想法?

1 个答案:

答案 0 :(得分:1)

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

配置/ 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 /配置/ 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);

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

我已在此处使用此代码https://ide.c9.io/steinkel/users-35-custom-permissions

创建了一个c9.io环境