Skip to content

Commit cdf07b6

Browse files
authored
[11.x] Add URL::forceHttps() to enforce HTTPS scheme for URLs (#53381)
* add `forceHttps()` * add test
1 parent db76573 commit cdf07b6

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,19 @@ public function forceScheme($scheme)
728728
$this->forceScheme = $scheme ? $scheme.'://' : null;
729729
}
730730

731+
/**
732+
* Force the use of the HTTPS scheme for all generated URLs.
733+
*
734+
* @param bool $force
735+
* @return void
736+
*/
737+
public function forceHttps($force = true)
738+
{
739+
if ($force) {
740+
$this->forceScheme('https');
741+
}
742+
}
743+
731744
/**
732745
* Set the forced root URL.
733746
*

src/Illuminate/Support/Facades/URL.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @method static void defaults(array $defaults)
3131
* @method static array getDefaultParameters()
3232
* @method static void forceScheme(string|null $scheme)
33+
* @method static void forceHttps(bool $force = true)
3334
* @method static void forceRootUrl(string|null $root)
3435
* @method static \Illuminate\Routing\UrlGenerator formatHostUsing(\Closure $callback)
3536
* @method static \Illuminate\Routing\UrlGenerator formatPathUsing(\Closure $callback)

tests/Routing/RoutingUrlGeneratorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,20 @@ public function testForceRootUrl()
716716
$this->assertSame('https://www.bar.com/foo', $url->route('plain'));
717717
}
718718

719+
public function testForceHttps()
720+
{
721+
$url = new UrlGenerator(
722+
$routes = new RouteCollection,
723+
Request::create('http://www.foo.com/')
724+
);
725+
726+
$url->forceHttps();
727+
$route = new Route(['GET'], '/foo', ['as' => 'plain']);
728+
$routes->add($route);
729+
730+
$this->assertSame('https://www.foo.com/foo', $url->route('plain'));
731+
}
732+
719733
public function testPrevious()
720734
{
721735
$url = new UrlGenerator(

0 commit comments

Comments
 (0)