2016-02-09 154 views
5

当我使用secure_url()asset()时,它链接到我网站的域名没有“www”,即“example.com”。Laravel:更改基准网址?

如何将其更改为链接到“www.example.com”?

+0

将其更改为www.example.com的具体原因是什么? –

+0

我只是喜欢它有“www”。我也使用“www”来做所有的营销工作,我宁愿继续使用它的网站。 – user5893820

+0

我从来没有实现过,但现在我只是玩了一下。如果您将'config/app.php'中的'url'更新为'www.example.com',那么当您使用URL助手生成URL时,它似乎会生成带有'www'前缀的URL。给它一个去,让我知道你如何继续。 – James

回答

0

Laravel在生成secure_url和url()时使用当前域。但是你可以在config.php文件夹下设置你的defult url。

/* 
|-------------------------------------------------------------------------- 
| Application URL 
|-------------------------------------------------------------------------- 
| 
| This URL is used by the console to properly generate URLs when using 
| the Artisan command line tool. You should set this to the root of 
| your application so that it is used when running Artisan tasks. 
| 
*/ 
'url' => 'http://localhost', 
+0

它不起作用 –

18

首先改变你的应用程序URL文件配置/ app.php(或您的.ENV文件APP_URL值):

'url' => 'http://www.example.com', 

然后,使URL生成器使用它。加入放入系统的代码行到文件应用/供应商/ 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 
} 

这是所有乡亲。

+1

Laravel使用请求负载中的HTTP_HOST生成URL。这是改变这种状况的解决方案。 –

+0

你是救命的人。谢谢! – gradosevic

+1

请注意,它的URL :: forceScheme not forceSchem ** a ** –