Skip to content
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 segments passed in as method parameters skips segments with value as 0 (zero) #2032

Closed
fijas opened this issue May 30, 2019 · 3 comments · Fixed by #2043
Closed

URI segments passed in as method parameters skips segments with value as 0 (zero) #2032

fijas opened this issue May 30, 2019 · 3 comments · Fixed by #2043

Comments

@fijas
Copy link
Contributor

fijas commented May 30, 2019

Describe the bug
URI segments passed in as method parameters doesn't seem to work as expected if the segment contains 0 (integer zero).

CodeIgniter 4 version
4.0.0-beta3

Affected module(s)
Controllers & Routing

Expected behavior, and steps to reproduce if appropriate
If the URI segment contains zero as it's value, the segment is skipped when passed in as the method parameter.

class TestController extends Controller {
    public function segmentTest($param1 = 'x', $param2 = 'y') {
        var_dump($param1);
        echo '<br/>';
        var_dump($param2);
    }
}

If the URL called is something like

test/segmentTest/0/abc, (notice the third segment is zero, so $param1 should be set as 0)

but the result was:

string(3) "abc"
string(1) "y"

whereas I was expecting something like:

string(1) "0"
string(1) "abc"

Is this intentional? I couldn't find anything in the documentation. There are multiple situations where passing in 0 as the url param is valid.

Also please note that if no default values are given for the parameters, an error is encountered:

ArgumentCountError
Too few arguments to function App\Controllers\Test::segmentTest(), 1 passed in 

Context

  • OS: Linux
  • PHP version - 7.2.17
@InsiteFX
Copy link
Contributor

Each section of the path between the slashes is a single segment. The URI class provides a simple way to determine what the values of the segments are. The segments start at 1 being the furthest left of the path.

@fijas
Copy link
Contributor Author

fijas commented May 31, 2019

Sorry if my question was not clear. I understand how segments work. My issue is that if the value of a segment is 0, then that value is not passed to the controller as a method parameter and the next segment if any is passed instead. The example I've given in the bug report illustrates the problem. I've edited the issue to try and make it a bit more clear...

@fijas fijas changed the title URI segments passed in as method parameters skips segments with 0 value URI segments passed in as method parameters skips segments with value as 0 (zero) May 31, 2019
@fijas
Copy link
Contributor Author

fijas commented May 31, 2019

So i've tracked down why this happens and the following line is where the 0 valued segments are filtered out:

$segments = array_filter($segments);

This line will filter out any values that evaluate to FALSE. So 0s will get dropped. I think that having 0s in the URL are valid and should not be filtered out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants