Problem/Motivation

Over in #2339219-15: [meta] Finalize URL generation API (naming, docs, deprecation) @Crell said

By this point most things should be using a path with leading slash, no? That's what all of the new code is built on... If we haven't actually done the job of converting to that then we've kinda screwed ourselves. :-)

But PathValidator::getUrIIfValid() does exactly that - ie doesn't support paths with a leading '/'.

Proposed resolution

Make PathValidator::getUrIIfValid() accept paths with or without a leading /

Remaining tasks

Fix failing tests (if any)
Review

User interface changes

None

API changes

None

Comments

larowlan’s picture

StatusFileSize
new593 bytes
new2.24 KB

@chx pointed out ltrim would be more efficient

chx’s picture

// Add.
$path = ltrim($path, '/');
// No change necessary.
$request = Request::create('/' . $path);
$attributes = $this->getPathAttributes($path, $request);
larowlan’s picture

StatusFileSize
new687 bytes
new2.07 KB

/me tips hat to simplicity

The last submitted patch, 1: path-validator-support-leading-slash.2.patch, failed testing.

The last submitted patch, path-validator-support-leading-slash.1.patch, failed testing.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Quite sensible. Thanks, larowlan!

dawehner’s picture

+1

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 966aed8 and pushed to 8.0.x. Thanks!

  • alexpott committed 966aed8 on 8.0.x
    Issue #2340379 by larowlan: Allow PathValidator::getUrlIfValid to...
catch’s picture

Status: Fixed » Needs work

Sorry I've reverted this, don't think it's the right change.

We validate against the leading slash to avoid storing a path with a leading slash in the database. That allows paths to be generated correctly on rendering. If a site moves in/out/between subdirectories then it would require a data migration to get the right path. I once worked on a site that had development installs in sub-directories but the main site at example.com/. Have also seen people run sites in a subdirectory alongside a legacy site, then move them up a level after a full migration.

dawehner’s picture

Well, for external URLs this doesn't matter. For internal URLs the path validator though actually stores the route name + parameters, so at render time you don't use the path any longer.

catch’s picture

Status: Needs work » Fixed

Hmm that's a good point, although it doesn't cover the case from #2339219: [meta] Finalize URL generation API (naming, docs, deprecation) where the URL points to the same domain but it's not handled by Drupal. In that case we fail validation, but that discussion is covered in the other issue as to how that could be handled.

Realised I didn't push the revert, so just moving back to fixed.

Crell’s picture

More to the point, the routing system uses a leading slash, always, from the front controller (index.php). We should be consistent with that elsewhere in code. We've not been. :-(

dawehner’s picture

@Crell
The routing system does, but we have to present the URLs to the user.

chx’s picture

Edit: nevermind.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.