2017-09-03 96 views
0

問:如何從插件/ config目錄加載配置文件?在CakePHP中動態加載插件配置文件3

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

我使用CakeDC /用戶插件,它有它加載從RBAC權限permissions.php文件。從我可以告訴,它要麼加載用戶插件的配置文件夾中的默認權限文件,要麼從app/config文件夾加載permissions.php文件。

現在對於我的應用程序骨架,我在app/config/permissions.php中擁有一堆權限,但是,我不想修改該文件,因爲我將從上游回購中執行git pull操作,而且我希望避免衝突。

所以我想要做的是,在應用程序骨架引導

什麼,我想

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

編輯:因爲它現在正在做的,您可以加載插件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

+0

我創建了一個示範項目c9.io環境,它擁有您在示例插件推薦代碼的bootstrap.php ... https://github.com/CakePHPKitchen/CakeDC-Users-Permissions-Example –

+0

當插件引導程序加載時,權限尚未添加到配置中,並且如果我在引導程序中爲插件設置權限,它們最終會被覆蓋... –

+0

It在我看來,permissions.php文件不會被加載,直到在CakeDC/auth中SimpleRbacAuthorize.php的構造函數被觸發爲止......對於我來說,對於你所說的工作,它需要從一個啓動CakeDC/auth或CakeDC /用戶 –