如何扩展控制器以添加配置关系文件。
现在我发现我可以像这样添加一个新文件
myController::extend(function($controller){
$controller->relationConfig = '~/plugins/path/languages/config_relation.yaml';
});
在这种情况下擦除我的配置文件的方法已经存在,并添加一个新的,因此它会触发错误,因为其他已经存在的关系行为不存在。
答案 0 :(得分: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
);
}