Laravel在尝试在Blade文件中使用url函数时未获取到APP_URL参数。

huangapple go评论72阅读模式
英文:

Laravel not getting the APP_URL parameter when we try to use url function inside the blade file

问题

url() 返回的 URL 是 http:,但它应该是 https:

系统运行在 Docker 容器内。

我已运行了所有清除缓存、视图、路由的命令,甚至运行了 optimize 命令。

有人能建议我配置有什么问题吗?或者有什么方法可以解决这个问题?

我需要 {{ url('') }} 返回 .env 文件中的 APP_URL

英文:

I'm facing a weird situation, just look at the following code in a blade file, and the output of it

Blade

{{ url('xero_invoice_authorised') }}</br>
config app url - {{ config('app.url') }}</br>
env app url - {{ env('APP_URL') }}

Output

http://xxxx/xero_invoice_authorised
config app url - https://xxxx/
env app url - https://xxxx/

url() is returning the URL as http: but it should be https:.

The system is inside a docker container.

I have run all commands to clear the cache, view, routes, and even ran optimize command.

Can anyone suggest to me what's wrong with my configuration? or any method to rectify this issue?

I need {{ url('') }} to return the APP_URL in the .env file.

答案1

得分: 2

没有任何错误,它按预期工作。

首先,永远不要在config文件夹之外使用env(),因为当你运行php artisan optimizephp artisan config:cache时,env()将始终返回null。更多信息请参阅文档。我理解如果这只是一个测试,但请牢记这一点。

其次,url()将始终返回http而不是https。如果你想要https,你必须使用secure_url()。查看url()secure_url()的文档。

第三,只是为了向你展示它是如何工作的,这里是url()的官方源代码,UrlGenerator是处理http://https://的部分,你可以尝试调试它正在读取什么以及为什么它在使用不安全的选项而不是另一个选项。

英文:

There is no error at all, it is working as expected.

First of all, never use env() outside the config folder, because when you run php artisan optimize or php artisan config:cache, env() will always return null. More info in the documentation. I understand if this is just a test, but have it always in mind.

Second, url() will ALWAYS return http instead of https. You have to use secure_url() if you want https. Check the documentation for url() and secure_url().

Third, just to show you how it works, here is the official source code of url(), the UrlGenerator is the one processing the http:// or https://, you can try debugging what it is reading and checking why it is using insecure instead of the other one.

答案2

得分: 0

I updated the AppServiceProvider with the following to force HTTPS on the production environment.
File: app/Providers/AppServiceProvider.php

public function boot()
{
    if (env('APP_ENV') === 'production') {
        URL::forceScheme('https');
    }
}
英文:

After different tryouts found a working method
I updated the AppServiceProvider with the following to force the HTTPS on production environment.
File : app/Providers/AppServiceProvider.php

public function boot()
    {
            if (env('APP_ENV') === 'production') {
                URL::forceScheme('https');
            }
    }

答案3

得分: 0

Sure, here is the translated content:

除了在配置文件中使用env()之外,请不要在任何地方使用它;一旦配置被缓存,.env 中将不再可用。

如果AMP是第三方服务,请将密钥放入 config/services.php

// config/services.php

<?php

return [
	// ...

	'amp' => [
		'key' => env('AMP_KEY'),
	],

],

然后在视图中:

<meta name="amp_key" content="{{ config('services.amp.key') }}">
英文:

Don't use env() anywhere except in your con fig files; once config has been cached, there will be nothing available from .env.

If AMP is a third party service, then put the key into config/services.php:

// config/services.php

&lt;?php

return [
	// ...

	&#39;amp&#39; =&gt; [
		&#39;key&#39; =&gt; env(&#39;AMP_KEY&#39;),
	],

],

Then in the view:

&lt;meta name=&quot;amp_key&quot; content=&quot;{{ config(&#39;services.amp.key&#39;) }}&quot;&gt;

huangapple
  • 本文由 发表于 2023年6月29日 18:15:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定