When I use secure_url()
or asset()
, it links to my site's domain without "www", i.e. "example.com".
How can I change it to link to "www.example.com"?
You should set this to the root of | your application so that it is used when running Artisan tasks. | */ 'url' => env('APP_URL', 'http://localhost'), This tells that this url is only used for the local artisan task.
To change the App URL of a Laravel application, you can specify it from the . env (environment) file. By having the APP_URL updated instead of using the "http://localhost" you will have a pretty URL when generating images, assets path and etc.
You can use that code but replace the line $generator->forceSchema('https'); in the provider boot method, with $generator->forceRootUrl(Request::getScheme() . '://www.yourdomain.com'); . And now your URL should be generated like you want.
First change your application URL in the file config/app.php (or the APP_URL value of your .env file):
'url' => 'http://www.example.com',
Then, make the URL generator use it. Add thoses lines of code to the file app/Providers/AppServiceProvider.php in the boot method:
\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
}
That's all folks.
.env
file change in
APP_URL='http://www.example.com'
config/app.php :
'url' => env('APP_URL', 'http://www.example.com')
In controller or View call with config method
$url = config('app.url');
print_r($url);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With