Problem/Motivation

There are a few situations in core where links are generated when the system is running outside of the primary front controller or from a special route. These include

  • update.php
  • core/authorize.php
  • core/rebuild.php
  • core/install.php (?)

When links are generated, there are two scenarios. One, a link needs to be generated relative to the entry point (eg, update.php). Two, a link needs to be generated relative to the site root. When this happens, the base_url gets passed into Url() as an option, but to get the base_url you either need to rely on the global or perform some shenanigans to construct it on your own. This is bad.

Proposed resolution

Options for discussion:

Remaining tasks

Discuss, do it.

User interface changes

None.

API changes

Url() and/or the link generator may have a new option.

Data model changes

Should be none.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mpdonadio created an issue. See original summary.

mpdonadio’s picture

Title: Add option to Url() to force the site base_url to be used. » Add option to Url() to force the site base_url to be used

Actually, I am starting to think this is a bug. If you are passing a route to Url(), then I don't see why links should ever be relative to anything other than the primary front controller for the site (adjusted for language, etc).

?

David_Rothstein’s picture

Actually, I am starting to think this is a bug. If you are passing a route to Url(), then I don't see why links should ever be relative to anything other than the primary front controller for the site (adjusted for language, etc).

I agree. In Drupal 7 this just worked - seems like a regression if in Drupal 8 you have to mess with the base URL (and force the link to be absolute) yourself.

stefan.r’s picture

Category: Task » Bug report

yes, right now I think the only way to work around this is by using toAbsolute()

mpdonadio’s picture

Status: Active » Needs review
FileSize
1.03 KB

Fugly patch, but I think it shows the general problem. The base_url is being set up properly by the kernel. The problem is that UrlGenerator::generateFromRoute() grabs the base_url from the RequestContext when it isn't explicitly specified. And, when the RequestContext context is created in the UrlGenerator, it doesn't set up the base_url; it will be derived from the Request object, which will grab it from the path to the front controller that was the entry point. And this last part is in Symfony land, and out of our control.

When I apply this on top of the patch from #1885564: theme.maintenance.inc (authorize.php) - Convert theme_ functions to Twig, and then comment out the 'absolute' and 'base_url' options from Module::postInstallTasks(), then I get the proper URLs in the "Authorize file system changes" page.

Not sure how best to handle this, but I suspect we need to inject the base_url into the UrlGenerator service for use with routes instead of grabbing them from the context?

Status: Needs review » Needs work

The last submitted patch, 5: add_option_to_url_to-2548095-5.patch, failed testing.

dawehner’s picture

The more I think about the issue the more I think we should actually make it that the URL is generated like we would have visited index.php. It is what people expected.
If you still want to generate one relative to the current front controller we could make that behaviour opt in.

mpdonadio’s picture

Assigned: Unassigned » mpdonadio
Issue summary: View changes

authorize.php has a @todo and update.php will have the @todo referencing this.

rebuild.php does

$base_path = dirname(dirname($request->getBaseUrl()));
header('Location: ' . $base_path);

I would really be surprised if install.php doesn't do something to work around this, too, for the post installation links, but I didn't look into it too much.

Later today I'll fix as many places in code that use shenanigans so we can have a failing patch to test with. I may wait to see if the update one gets in today.

catch’s picture

Bumping to major, this would have simplified #2540416: Move update.php back to a front controller.

mpdonadio’s picture

Assigned: mpdonadio » Unassigned
Issue summary: View changes
Status: Needs work » Needs review
FileSize
6.85 KB

OK, first pass at fixing Url usage. Didn't look at installer yet (I totally forget how the post installation redirect works). Also didn't address the actual problem. So, this should at least start to show some failures (or show where we are lacking test coverage).

No interdiff; the patch in #5 was just a demo.

mpdonadio’s picture

If the site has been properly installed, then this should work when we fix the underlying problem.

catch’s picture

Priority: Normal » Major

Actually bumping this time.

The last submitted patch, 11: add_option_to_url_to-2548095-11.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 12: add_option_to_url_to-2548095-12.patch, failed testing.

mpdonadio’s picture

mpdonadio’s picture

Issue summary: View changes
dawehner’s picture

Status: Needs work » Needs review
FileSize
2.34 KB
3.13 KB

What about something like this? I mean what we want is a different thing, so we should also name it different.

Status: Needs review » Needs work

The last submitted patch, 18: 2548095-18.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
2.35 KB
552 bytes

Muh.

Status: Needs review » Needs work

The last submitted patch, 20: 2548095-20.patch, failed testing.

Crell’s picture

  1. +++ b/core/lib/Drupal/Core/Routing/RequestContext.php
    @@ -64,9 +97,13 @@ public function getCompleteBaseUrl() {
    +   * @return $this
    

    We normally return static, not $this, in the docs.

  2. +++ b/core/lib/Drupal/Core/Routing/RequestContext.php
    @@ -64,9 +97,13 @@ public function getCompleteBaseUrl() {
    +    return $this;
    

    Ibid.

I'm a bit unclear on how this patch is supposed to help, though...

tim.plunkett’s picture

Please disregard #22.1.
We use @return $this when it's the same instance.
@return static is for when it's a factory method or similar.

mpdonadio’s picture

Status: Needs work » Needs review
FileSize
5.16 KB
3.6 KB

Fixed a problem in #20 and updated most of the @todo that reference this issue, except the ones in DbUpdateController. Unit tests pass locally, let's see what a full run does now.

Status: Needs review » Needs work

The last submitted patch, 24: add_option_to_url_to-2548095-24.patch, failed testing.

tim.plunkett’s picture

Category: Bug report » Task

When this happens, the base_url gets passed into Url() as an option, but to get the base_url you either need to rely on the global or perform some shenanigans to construct it on your own. This is bad.

Sure it's ugly, but this isn't a major bug.

joelpittet’s picture

Just out of curiosity is there a better name than mainBaseUrl? Since it's referring to a front controller, could it be named as such?
mainFrontControllerUrl? assuming there are more than one front controllers(naive, but is there?)

Alternatives:

  1. whoIsOnFirstBaseUrl
  2. starKillerBaseUrl
  3. AllYourMainBaseRbelongsToUrl

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

seanB’s picture

I just ran in to this issue. Is there another way around this? Using Url() in my update hook always adds /update.php/ to all URLs. If there is no way to fix this, it is probably a bug right?

joelpittet’s picture

@seanB yes that sounds like a separate issue, please have a search if someone has created it first and then if not create a new one

seanB’s picture

Ok thnx, I created issue https://www.drupal.org/node/2824581

Wim Leers’s picture

mpdonadio’s picture

Issue tags: +Needs reroll

No real surprise, but #24 doesn't apply, so this needs a reroll. Probably needs to be a hand re-roll; since #1475510: Remove external dependencies from the core repo and let Composer manage the dependencies instead I haven't really successfully rebased against 8.0.x.

vprocessor’s picture

Assigned: Unassigned » vprocessor
vprocessor’s picture

Status: Needs work » Needs review
vprocessor’s picture

Issue tags: -Needs reroll
vprocessor’s picture

Assigned: vprocessor » Unassigned

Status: Needs review » Needs work

The last submitted patch, 36: add_option_to_url_to-2548095-36.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.