-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: route options are not merged (inner filter overrides outer filter) #7981
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to imagine scenarios in which this could have been intentionally used, and frankly, I can't. But I'm sure there will be devs who take advantage of current behavior.
public function testGroupNestedWithOuterAndInnerOption(): void | ||
{ | ||
$routes = $this->getCollector(); | ||
|
||
$routes->group( | ||
'admin', | ||
['filter' => ['csrf']], | ||
static function ($routes) { | ||
$routes->get('dashboard', static function () { | ||
}); | ||
|
||
$routes->group( | ||
'profile', | ||
['filter' => ['honeypot']], | ||
static function ($routes) { | ||
$routes->get('/', static function () { | ||
}); | ||
} | ||
); | ||
} | ||
); | ||
|
||
$expected = [ | ||
'admin/dashboard' => [ | ||
'filter' => ['csrf'], | ||
], | ||
'admin/profile' => [ | ||
'filter' => ['honeypot'], | ||
], | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still confusing to me but I guess it's because we can have only one filter when Config\Feature::multipleFilters
is disabled.
Will this gonna end up as:
$expected = [
'admin/dashboard' => [
'filter' => ['csrf'],
],
'admin/profile' => [
'filter' => ['csrf', 'honeypot'],
],
];
when multipleFilters
option will be enabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. it is just because using array_merge()
.
https://3v4l.org/tskmm
It is the current behavior that filters are not merged, and also the reason why the Note in the user guide was added.
One possible behavior would be to merge filters as well, but then it would not be possible to delete filters in inner group. Multiple filters are already the default in 4.5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point about removing filters.
I don't know... now I'm leaning towards staying with the current behavior and not inheriting anything. Because the new behavior seems even more confusing - it's inherited only if there are no other filters. I can imagine people adding a new filter over time and "losing" functionality.
Thanks to the current note, at least we have clear rules to follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current behavior is not that straightforward either.
The current behavior is that if you don't pass an options array to the inner group, the outer options are inherited.
That's why when you set outer filter and add namespace option to inner group, the filter that set in outer suddenly will be gone for the inner route.
It seems that this issue is not so easy to conclude.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a bit more complex than I thought at first. Especially if we add namespaces to it.
Personally, I would probably merge all the declared filters from the external to the internal group using array_merge_recursive
and introduce something like filterExclude
to remove the filters if needed. However, this seems a bit too complex... not sure if we want to make such big changes.
Although the rules would be clear and need no further explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am very sorry for my translation.
Yes, to exclude the outside filter, I gave an example of except
in the Config. Or we need to write such a filter for all routes except unnecessary ones (this is your option)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with the new feature to exclude route filters in the Config\Filter
.
The Config\Filter
and route filters are the same filters, but I think mixing these settings is a source of confusion.
Also, I don't like to add a new key for excluding filters in the route option.
It also is complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this does not cancel your method - we leave the outside filters empty and configure each route individually
I'm sorry, but what you don't like about the exception in the Config is a function of the framework. There is no other way to fine tune
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Config\Filter
defines filters for URI paths, not for routes.
except
excepts the filter to the URI path in Config\Filter
.
https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html#except-for-a-few-uris
Filters for a route are different things. If you specify a filter to a route, it is always applied.
The definitions in Config\Filter
does nothing to route filters.
What I don't like is to mix the two kind of filters. That is, except
in Config\Filter
excepts route filters, too.
It is really complicated and confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalsn @neznaika0 @MGatner I changed my mind. I sent another PR #8033 that behaves like michalsn expects in #7981 (comment)
👋 Hi, @kenjis! |
b2d49e9
to
f4013c4
Compare
👋 Hi, @kenjis! |
f4013c4
to
9cad762
Compare
Rebased to resolve a conflict. |
9cad762
to
0fd10e7
Compare
👋 Hi, @kenjis! |
0fd10e7
to
c8cb5cc
Compare
Rebased to resolve a conflict. |
c8cb5cc
to
8d62a57
Compare
Closed by #8033 |
Description
Fixes #7963
group()
options with innergroup()
optionsChecklist: