-
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
URI::getSegment() returns non-existent segment #7246
Comments
This was already reported in #2962 |
The current behavior is difficult to understand and I still consider it a bug. If there is no segment, an exception should be thrown. |
As much as I agree this is strange behavior, the n+1 implementation to not throw an exception was intended from the beginning (from what I understand). However, there was no Changing this now for a minor version release may be problematic for many existing projects. |
What is the intention? Why only n+1 is okay? |
That was more convenient, I guess. n+1 means an additional, optional parameter in the URL that we can check without worrying about throwing an exception if it's not there. It can be for pagination, filtering, etc., a standard approach from my perspective. To be fair - for me throwing an exception by default was always a bad idea. In this particular case, I would replace it with a Anyway, I think that changing this behavior may be potentially harmful. |
I don't know why these changes were made in v4.0. |
This is the first commit for It seems to me that accepting |
@michalsn Do you think we should accept |
If this is a bug or not - I can't answer this because I don't know. For me, the idea of throwing an exception here is wrong. It's like we would throw an exception by default when there is no GET parameter.
No. |
I believe that code was copied straight over from v3, IIRC. If that's' true, it's how it has always been and then you're torn between how the developer has to handle it - check for null or a try/catch block. Is it an exceptional moment in the program? I'm not sure it always is, but I have no strong feelings either way for this. But there's possible a lot of code out there relying on it currently. Unnecessary BC break, if you ask me. |
I changed my opinion.
These use cases are likely, and I think getting n+1 segment is not exceptional. |
CodeIgniter version: 4.3.1
In this sample, there are three URI segments: 1:
users
, 2:15
, 3:profile
But you can get the 4th segment
$uri->getSegment(4, 'bar')
without Exception.But
$uri->getSegment(5, 'baz')
will throw Exception.https://codeigniter4.github.io/CodeIgniter4/libraries/uri.html#uri-segments
Related: #7233
The text was updated successfully, but these errors were encountered: