Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Change base URL?

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"?

like image 602
user5893820 Avatar asked Feb 09 '16 23:02

user5893820


People also ask

How do I change the default URL in Laravel?

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.

How can I change URL in Laravel 8?

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.

How set API base URL in Laravel?

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.


2 Answers

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.

like image 150
Synn Avatar answered Sep 24 '22 01:09

Synn


.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);
like image 30
PHP Laravel Developer Avatar answered Sep 27 '22 01:09

PHP Laravel Developer