Laravel HTTPS路由 [英] Laravel HTTPS routes
问题描述
今天我决定将我的网站迁移到 HTTPS.早期,我的网站使用HTTP.
today I decided to move my website to HTTPS. Early my website work on HTTP.
我的问题在于误解了Laravel如何在辅助函数 route('name')
My problem is in misunderstood how Laravel pass HTTP and https in helpers function route('name')
我将 config/app.php
中的网站URL更改为 https://www.domain.name
,我认为,此解决方案对我有帮助.但是我得到了一个奇怪的结果.
I change my website URL in config/app.php
to https://www.domain.name
and I think, this solution helps me. But I got a strange result.
在 php artisan tinker
中,如果我通过 route('ROUTE.NAME')
,则我获得了正确的链接 https://www.domain.name/route/路径
但是在刀片模板中,我得到了 http://www.domain.name/route/path
In php artisan tinker
if I pass route('ROUTE.NAME')
I got right link https://www.domain.name/route/path
but in blade template I got http://www.domain.name/route/path
与 \ URL :: to('/')
也许有人可以向我解释为什么会这样?
Maybe someone can explain to me why this happened?
推荐答案
@dekts响应正确,但放置此类内容的"正确"位置是" app"/strong>启动方法上的/Providers/AppServiceProvider.php".
The @dekts response is correct, but the "right" place to put this kind of stuff is the "app/Providers/AppServiceProvider.php" on the boot method.
//file: app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\URL;
class AppServiceProvider extends ServiceProvider {
public function boot()
{
if (app()->environment('remote')) {
URL::forceSchema('https');
}
}
...
}
您还可以在" .env "文件中添加一个新变量,例如:
You can also add a new variable on your ".env" file, something like:
#file: .env
FORCE_HTTPS=true
并将条件更改为
//file: app/Providers/AppServiceProvider.php
public function boot()
{
if(env('FORCE_HTTPS',false)) { // Default value should be false for local server
URL::forceSchema('https');
}
}
希望获得帮助.
这篇关于Laravel HTTPS路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!