2017-04-11 33 views
2

如何擴展控制器以添加配置關係文件。如何擴展關係Config October CMS

現在我發現我可以在這種情況下添加一個新的文件中像這樣

myController::extend(function($controller){ 

$controller->relationConfig = '~/plugins/path/languages/config_relation.yaml'; 
     }); 

方法刪除我的配置文件已經存在,並添加一個新的,因此引發錯誤,因爲已經是他人之間的關係行爲不存在。

回答

1

這家最近進行了討論,並記錄在案here

myController::extend(function($controller) { 

    // Implement the relation controller if it doesn't exist already 
    if (!$controller->isClassExtendedWith('Backend.Behaviors.RelationController')) { 
     $controller->implement[] = 'Backend.Behaviors.RelationController'; 
    } 

    // Implement the relationConfig property with our custom config if it doesn't exist already 
    $myConfigPath = '~/plugins/path/languages/config_relation.yaml'; 
    if (!isset($controller->relationConfig)) { 
     $controller->addDynamicProperty('relationConfig', $myConfigPath); 
    } 
    else { 
     // Ensure that we have an instantiated config object to work with 
     $config = $controller->makeConfig($controller->relationConfig); 

     // Instantiate our custom config object to work with 
     $myConfig = $controller->makeConfig($myConfigPath); 

     // Merge the above two 
     $controller->relationConfig = (object) array_merge((array) $config, (array) $myConfig); 
    } 
} 

以下功能new,目前在develop分支:

因此,在未來,後
public function mergeConfig($configA, $configB) 
{ 
    $configA = $this->makeConfig($configA); 
    $configB = $this->makeConfig($configB); 
    return (object) array_merge((array) $configA, (array) $configB); 
} 

develop分支合併爲master,你將能夠使用以下代碼來合併配置:

UsersController::extend(function($controller) { 

    // Implement behavior if not already implemented 
    if (!$controller->isClassExtendedWith('Backend.Behaviors.RelationController')) { 
     $controller->implement[] = 'Backend.Behaviors.RelationController'; 
    } 

    // Define property if not already defined 
    if (!isset($controller->relationConfig)) { 
     $controller->addDynamicProperty('relationConfig'); 
    } 

    // Splice in configuration safely 
    $myConfigPath = '$/myvendor/myplugin/controllers/users/config_relation.yaml'; 

    $controller->relationConfig = $controller->mergeConfig(
     $controller->relationConfig, 
     $myConfigPath 
    ); 

} 
+0

真棒坦克@meysam –

+0

@TahaAzzabi文檔已更新,代碼略有更改:https://github.com/octobercms/docs/blob/master/services-behaviors.md#detecting-utilized-extensions – Meysam