今天我决定将我的网站转移到HTTPS。早期我的网站就是在HTTP上工作。
我的问题是误解了Laravel如何在助手函数route('name')
中传递HTTP和https
我将config/app.php
中的网站网址更改为https://www.domain.name
,我认为此解决方案可以帮助我。但是我得到了一个奇怪的结果。
php artisan tinker
如果我通过route('ROUTE.NAME')
,我的链接https://www.domain.name/route/path
但在刀片模板中,我得到http://www.domain.name/route/path
与\URL::to('/')
也许有人可以向我解释为什么会这样?
答案 0 :(得分:8)
@dekts响应是正确的,但 right “放置这种东西的地方是< app / Providers / AppServiceProvider.php ”上的< strong>启动方法。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> | Coming Soon!</title>
</head>
<body>
<div id="wrapper">
<IMG SRC="jewler/BackGround.png" width="100%" height="100%"/>
<div id="logo">
<IMG SRC="jewler/logo.png" width="100%" height="100%"/>
</div>
<div id="info">
Coming Soon
Adress:
Phone:
E-mail:
</div>
</div>
</body>
</html>
您还可以在“ .env ”文件中添加新变量,例如:
//file: app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\URL;
class AppServiceProvider extends ServiceProvider {
public function boot()
{
if (app()->environment('remote')) {
URL::forceScheme('https');
}
}
...
}
并将条件更改为
#file: .env
FORCE_HTTPS=true
希望得到这个帮助。
答案 1 :(得分:4)
我多年来一直在努力。我的解决方案是将以下内容放在我的routes.php文件中(您可能更喜欢不同的地方)
我还围绕这一行包装了一个条件语句,所以它只适用于我的远程配置:
if (App::environment('remote')) {
URL::forceSchema('https');
}
您可以尝试在boot
方法中添加AppServiceProvider。
在这里:app/Providers/AppServiceProvider.php
或另一种方式:
所以我明白了。这并不容易,但在AppServiceProvider.php
中,您必须在$this->app['request']->server->set('HTTPS', true);
方法中添加:register
。这就是我弄清楚它的方式。
顺便说一下。设置APP_URL
对您应用的HTTP
方无效,对工匠而言。
PS。 CloudFlare HTTPS
重写也是个好主意:)
答案 2 :(得分:0)
.env文件中的
FORCE_HTTPS=true
在app / Providers / AppServiceProvider.php
中public function boot()
{
if(env('FORCE_HTTPS',false)) {
URL::forceScheme('https');
}
}
5.4之后,方法名称为 forceScheme
如果您使用“ forceSchema ”,则会出现错误
"Method Illuminate\Routing\UrlGenerator::forceSchema does not exist."
答案 3 :(得分:0)
自动检测http / https
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Request;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
/* Use https links instead http links */
if (Request::server('HTTP_X_FORWARDED_PROTO') == 'https')
{
URL::forceScheme('https');
}
}
}