processwire-issues icon indicating copy to clipboard operation
processwire-issues copied to clipboard

`$page->url()` produces incorrect URLs for some parameters

Open michaellenaghan opened this issue 1 year ago • 0 comments

Short description of the issue

$page->url() produces incorrect URLs for some parameters

Expected behavior

See the examples below, but basically: $page->url() should produce the same output as $input->url().

Actual behavior

In my home page template:

  • "Allow Page Numbers?" is checked
  • "Should page number URLs have a trailing slash?" is set to "No"
  • "Allow URL Segments?" is unchecked

$input->url() produces correct URLs:

  • $input->url(['pageNum' => 2]) returns "/page2"

$page->url() does not:

  • $page->url(2) returns "/page2/"

  • $page->url('-') returns "/-/" on page 1

  • $page->url('+') returns "/-/page2/" on page 1

  • $page->url(['pageNum' => '+']) returns "/page2/" on page 1

  • $page->url('-') returns "/-/" on page 2

  • $page->url('+') returns "/-/page3/" on page 2

  • $page->url(['pageNum' => '+']) returns "/page3/" on page 2

Note that:

  • $page->url() is prepending '/-' when '-' and '+' are passed in (but not when they're passed in via options!)
  • $page->url() is appending slashes when it shouldn't

Optional: Suggestion for a possible fix

The issues are fairly straightforward.

For example, this line:

    'urlSegmentStr' => is_string($options) ? $options : '',

should be changed to something like this:

    'urlSegmentStr' => (is_string($options) && !in_array($options, array('+', '-'))) ? $options : '',

and this line:

    if($template->slashPageNum) $url .= '/';

should be changed to something like this:

    if($template->slashPageNum === 1) $url .= '/';

(When "Should page number URLs have a trailing slash?" is set to "No", $template->slashPageNum is set to -1, a truthy value.)

Steps to reproduce the issue

In a template:

  1. Check "Allow Page Numbers?"
  2. Set "Should page number URLs have a trailing slash?" to "No"
  3. Leave "Allow URL Segments?" unchecked

In the code for the template:

  1. Call $page->url('+')

Setup/Environment

  • ProcessWire version: 3.0.229

michaellenaghan avatar May 04 '24 17:05 michaellenaghan