lesssugar
lesssugar

Reputation: 16181

Laravel action() helper ignores APP_URL environment variable

In my .env file, I have the APP_URL set like this:

APP_URL=https://example.com

However, anywhere I use the action() helper to generate a URL, the path is created with http instead of https.

I don't understand why this happens. The site itself runs on SSL. I also tried settings BASE_URL environment variable using "https" - no luck.

Shouldn't Laravel respect my base URL?

Laravel version: 5.2

This happens for the action() helper, as well as when using HTML & Forms Laravel Collective Package to generate forms (which include action property).

Upvotes: 2

Views: 2109

Answers (3)

Franck
Franck

Reputation: 610

More up to date solution with Laravel 9+ : use the URL facade.

URL::forceRootUrl(config('app.url'));

$absoluteLink = URL::action(
    [\App\Http\Controllers\MyController::class, 'getMyRedirect'],
    ['param' => $param],
);

Upvotes: 0

lesssugar
lesssugar

Reputation: 16181

Also, in addition to what Antonio suggested in his answer, you can also use the forceSchema() method:

AppServiceProvider.php

public function boot() {

    if (App::environment('production')) {
        URL::forceSchema('https');
    }
}

Notice the code uses Facades, but you could also use the global app() helper.

Upvotes: 0

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Unfortunately action() is not based on app.url, but in your request root, so if you are browsing https, it should give you an https scheme based url. But can force it to be whatever you need by doing:

app('url')->forceRootUrl(env('APP_URL'));

I suggest you to do that on a Service Provider.

Upvotes: 4

Related Questions