Repository navigation
Closed
Description
PHP Version
8.2
CodeIgniter4 Version
4.5.5
CodeIgniter4 Installation Method
Manual (zip or tar.gz)
Which operating systems have you tested for this bug?
Windows, Linux
Which server did you use?
apache
Database
MySQL
What happened?
New registrar with the same method with other registrars replaces their contents on array_merge which works on only first level array. I hope array_merge_recursive will be better for modules
Steps to Reproduce
\Codeigniter\Config\BaseConfig
/**
* Provides external libraries a simple way to register one or more
* options into a config file.
*
* @return void
*
* @throws ReflectionException
*/
protected function registerProperties()
{
if (! static::$moduleConfig->shouldDiscover('registrars')) {
return;
}
if (! static::$didDiscovery) {
$locator = service('locator');
$registrarsFiles = $locator->search('Config/Registrar.php');
foreach ($registrarsFiles as $file) {
$className = $locator->findQualifiedNameFromPath($file);
if ($className === false) {
continue;
}
static::$registrars[] = new $className();
}
static::$didDiscovery = true;
}
$shortName = (new ReflectionClass($this))->getShortName();
// Check the registrar class for a method named after this class' shortName
foreach (static::$registrars as $callable) {
// ignore non-applicable registrars
if (! method_exists($callable, $shortName)) {
continue; // @codeCoverageIgnore
}
$properties = $callable::$shortName();
if (! is_array($properties)) {
throw new RuntimeException('Registrars must return an array of properties and their values.');
}
foreach ($properties as $property => $value) {
if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value)) {
$this->{$property} = array_merge($this->{$property}, $value);
} else {
$this->{$property} = $value;
}
}
}
}Expected Output
Expected merge the old contents array with the new one
Anything else?
Yes i used array_merge_recursive. I discovered the issue to so i extend the base config to child class today fix this but i think the usage of array_merge_recursive is a better option help on this to update the core code
\Codeigniter\Config\BaseConfig
/**
* Provides external libraries a simple way to register one or more
* options into a config file.
*
* @return void
*
* @throws ReflectionException
*/
protected function registerProperties()
{
if (! static::$moduleConfig->shouldDiscover('registrars')) {
return;
}
if (! static::$didDiscovery) {
$locator = service('locator');
$registrarsFiles = $locator->search('Config/Registrar.php');
foreach ($registrarsFiles as $file) {
$className = $locator->findQualifiedNameFromPath($file);
if ($className === false) {
continue;
}
static::$registrars[] = new $className();
}
static::$didDiscovery = true;
}
$shortName = (new ReflectionClass($this))->getShortName();
// Check the registrar class for a method named after this class' shortName
foreach (static::$registrars as $callable) {
// ignore non-applicable registrars
if (! method_exists($callable, $shortName)) {
continue; // @codeCoverageIgnore
}
$properties = $callable::$shortName();
if (! is_array($properties)) {
throw new RuntimeException('Registrars must return an array of properties and their values.');
}
foreach ($properties as $property => $value) {
if (isset($this->{$property}) && is_array($this->{$property}) && is_array($value)) {
$this->{$property} = array_merge_recursive($this->{$property}, $value);
} else {
$this->{$property} = $value;
}
}
}
}Reacted by Sunchock
Metadata
Metadata
Assignees
Labels
No labels
Activity
michalsn commentedon Oct 21, 2024
Please give us an example of the problem: a config file and a registrar.
Are you defining the same registrar in many modules?
PANKAJNAGAR433 commentedon Oct 21, 2024
neznaika0 commentedon Oct 21, 2024
The problem is visible on the Filters. Try disabling only the Toolbar or adding filters to the previous config. They will be overwritten from the module
michalsn commentedon Oct 21, 2024
I will assume this is the proper example:
Now, you will get this:
But you expect this:
This is not a bug, but a feature request. Personally, I'm not a fan of this change.
You can avoid this problem very simply - by defining the structure of the config file differently.
neznaika0 commentedon Oct 21, 2024
how? let's say I initially want to disable the toolbar filter in app/Registrar.php, and add middlewares in other modules modules/auth, modules/job?
Some add data, others replace it.
the .env file will not work. It is not rational to create another config class - then why use a standard interceptor?
michalsn commentedon Oct 21, 2024
If we start using
array_merge_recursive, we will introduce a BC break.If modules add new options, they can use the Publisher class to add them to the main config.
I could be wrong, but I think the intention was to prevent everything from being easily overwritten from within modules, as there should be a "single source of truth" for some options.
neznaika0 commentedon Oct 22, 2024
Yes, this is BC. Therefore, another solution is needed. How about passing the current configuration properties to the Config?
This is about the order of work, code is inaccurate
ChibuezeAgwuDennis commentedon Oct 22, 2024
Certainly! Let's explore this issue in detail, starting with examples of the problem.
Example of the Problem: Config File and Registrar
In a typical modular CodeIgniter 4 application, you might have several independent modules, each with its own
Registrar.phpfile. These registrars are meant to register specific configuration settings, such as filters, routes, or other properties, into a central configuration system.Example Configuration Files
Blog Module Registrar:
Shop Module Registrar:
Paypal Module Registrar:
These files define filters that are supposed to be registered into the main configuration. The expectation is that all these configurations should be combined, so the application uses filters from all modules.
The Problem: Overwriting Instead of Merging
The issue arises due to the way the
registerPropertiesmethod handles the merging of configuration arrays. The current implementation usesarray_merge, which works fine for single-dimensional arrays but doesn't perform a deep merge. This means that when you have nested arrays, the nested arrays from one module can overwrite those of another module instead of merging them.For example, if the
filtersmethod is defined similarly in multiple modules, the last one processed will overwrite the previous ones rather than integrating all of them.Are You Defining the Same Registrar in Many Modules?
Yes, in this context, the same registrar method (
filters) is defined in multiple modules. Each module has its own implementation of thefiltersmethod within itsRegistrar.phpfile. The intention is for each module to contribute its filters to the central configuration.Solution: Deep Merging
To solve this issue, a deep merge function is needed. This function should recursively merge arrays to ensure that all configurations are retained and integrated properly. Here's a simple example of how I implement a deep merge function in PHP:
Applying the Solution
In the
registerPropertiesmethod, I replace thearray_mergeoperation with thisdeep_mergefunction to ensure that all configurations from different modules are properly merged.Solution Output:
After applying the
deepMergefunction, the combined configuration will look like this:Explanation of the Output
Aliases: The
aliasesarray now contains all filter aliases from the Blog, Shop, and Paypal modules, ensuring that each module's filter is registered.Globals: The
globalsarray for thebeforekey contains an array with all filter names, ensuring that all specified filters are applied before global processing.This method guarantees that all configuration data is retained, allowing each module to seamlessly contribute to the overall application setup. By using a deep merge function, the solution ensures that nested arrays are combined effectively, thereby preserving the unique configurations intended by each module's developer. This approach respects the configurations from all modules and integrates them cohesively, maintaining the integrity of the application's intended settings.
kenjis commentedon Oct 25, 2024
I think filter aliases can be configured in module, but all filters should be configured in
app/Config/Filters.phpfor security.neznaika0 commentedon Oct 25, 2024
What about the
appendBeforeProps(), appendAfterProps(), replaceProps()methodsRegistrar? Should I use the current (old$this) copy of the config inside?ChibuezeAgwuDennis commentedon Oct 26, 2024
What of the case the registrar it was stated in the doc that this should be better option
ChibuezeAgwuDennis commentedon Oct 26, 2024
Explain your question better
neznaika0 commentedon Oct 27, 2024
This is a solution option. New methods will add or replace Config properties.
neznaika0 commentedon Feb 7, 2025
Let's get back to the question.
In this case, there is already a security hole - the module can disable all filters.
Can we think about how to solve the issue painlessly? Add an option to the Feature class?
sanchawebo commentedon Jun 30, 2025
Any news on this issue? Just ran into it as i was trying to add ci-shield permissions for my module like this:
And after merging, 'shippinglabel-logos.*' is the only permission left for usergroup 'superadmin' which is very irritating.
michalsn commentedon Jun 30, 2025
There haven’t been any updates so far. Unless more maintainers express agreement on introducing such changes, we won’t be moving forward at this time.
IMHO, permission definitions should remain centralized in a single main file for security and maintainability reasons.
However, if you're confident in what you're doing, you can simply use
ReflectionClassin yourRegistrar.phpfile to merge the basesuperadminpermission array with additional items.neznaika0 commentedon Apr 8, 2026
The main reason that prevents implementation in the framework is the desire for a single point for changing parameters. If you don't have modules, then it looks strange: edit 20 files, make 100 changes, and check everything again when updating.
I expressed my opinion in the mind of the package. Try it, @ChibuezeAgwuDennis @warcooft @sanchawebo :
https://github.com/neznaika0/codeigniter-enhanced-config
ChibuezeAgwuDennis commentedon May 11, 2026
@sanchawebo @michalsn @neznaika0 here is another option of how all my modules filters work without registering them in the register.php by extending the discovering of the filter base class
Here is what we are using and it works perfectly for different modules
after this we update the Services::filter()
e.g
And we just copied the Config/Filter.php in app to our module Config changing it's namespace to our module namespace removing all the app/Config/Filter already set filters from our module modules/Acme/Blob/Config/Filter.php in order not to repeat it
e.g
So try it and give me feedback if it works perfectly. if this is the best solution, someone should update the source code with this
neznaika0 commentedon May 11, 2026
It works almost as originally - array_merge() and only one class. I suggested more freedom if you trust the module settings.
Sunchock commentedon Jun 18, 2026
Hello everyone,
We're encountering the exact same problem today in our project, within a modular architecture with multiple configuration files.
I just stumbled upon this discussion and I don't understand your reluctance to change.
On the one hand, a simple pull request would add seven characters, which would allow us to find a solution. The security argument is irrelevant, since it's already been demonstrated here that the initial configuration can be overridden at the first level.
Using a Publisher or the Reflection API simply because you don't want to use "_recursive" seems like a disproportionate and absurd solution.
The very principle of the Registrar is to be able to save configuration properties on the fly; it should therefore be able to do so for low-level properties in arrays.
Could we think of others and not block small improvements like this one, which could be useful to many projects?
We must think about what will help the greatest number of people, not what pleases you. An open-source project should not be a battleground for personal opinions. We must remain objective and improve what can be improved.
michalsn commentedon Jun 20, 2026
I want to clarify that this was never about bad will or personal preference. Registrars were originally designed around a shallow merge model. That behavior has existed for a long time, so changing it globally is not just a small internal tweak. It changes the contract that existing applications and packages already rely on.
The reason we should not simply replace
array_merge()witharray_merge_recursive()is that it would change registrar behavior globally, andarray_merge_recursive()does not express the semantics config files usually need.For example, scalar replacement becomes value collection:
That is probably not what a config merge should do. In many cases, the expected result is simply:
It can also introduce duplicates in list-style config:
And it does not give a clear way to say "replace this nested array with an empty array":
So the concern is not about blocking recursive merging as a concept. The concern is that changing the default merge algorithm would solve some cases while creating new, subtle behavior changes in others. As maintainers, we have to look at the whole API surface and preserve backward compatibility.
A solution for this is now proposed in #10329. This keeps the existing registrar behavior unchanged, and adds optional
Mergedirectives for cases that need more control. This lets simple registrars continue working as before, while advanced cases can explicitly choosebyKey(),replace(),append(),prepend(),before(), orafter()depending on the desired behavior.