Laravel:更改基本URL?

use*_*820 11 laravel laravel-4

当我使用secure_url()或时asset(),它链接到我的网站的域名没有 "www",即"example.com".

如何更改链接到"www.example.com"?

Syn*_*ynn 38

首先在文件config/app.php(或.env文件的APP_URL值)中更改您的应用程序URL :

'url' => 'http://www.example.com',
Run Code Online (Sandbox Code Playgroud)

然后,让URL生成器使用它.在引导方法中将几行代码添加到文件app/Providers/AppServiceProvider.php:

\URL::forceRootUrl(\Config::get('app.url'));    
// And this if you wanna handle https URL scheme
// It's not usefull for http://www.example.com, it's just to make it more independant from the constant value
if (\Str::contains(\Config::get('app.url'), 'https://')) {
    \URL::forceScheme('https');
    //use \URL:forceSchema('https') if you use laravel < 5.4
}
Run Code Online (Sandbox Code Playgroud)

这就是所有人.

  • @AJBrown是的,对于laravel&gt; = 5.4的更改在那里:https://github.com/laravel/framework/commit/39e8c83af778d8086b0b5e8f4f2e21331b015b39,我已经更新了答案thx (2认同)