2017-04-11 64 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