2016-02-09 176 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 ** –