Jump to content
salmiak

Can't access last url segment

Recommended Posts

I'm trying to get the last url segment with the new API and when I try to access the last segment I get the one second to last.

For example in the url: https://domain.com/page/seg1/seg2/seg3/seg4/

$input->urlSegment(-1) and $input->urlSegmentLast return "seg3"
$input->urlSegment(-2) returns "seg2" 
$input->urlSegment(-3) returns "seg1" 

as expected:
$input->urlSegment(1) returns "seg1" 
$input->urlSegment(4) returns "seg4"

I tried the following
$input->urlSegment(0) returns "seg1" 
$input->urlSegment(-0) returns "seg1" 

What's going wrong here? I'm using ProcessWire: 3.0.165

 

Share this post


Link to post
Share on other sites
2 hours ago, salmiak said:

What's going wrong here?

It's a bug - please open an issue on GitHub to bring it to Ryan's attention. The URL segments array has a 1-based index but this is lost to a 0-based index when the array is reversed here: https://github.com/processwire/processwire/blob/ee4c46a442f09cf81f9e1800c5fdfcd1176f538a/wire/core/WireInput.php#L576

As a workaround for now you could do this:

$segments = $input->urlSegments;
$last_segment = end($segments);

 

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...