Why it’s important to change APP_URL in Laravel .env file

Founder of QuickAdminPanel
By default, Laravel installation comes with .env.example file, which has default value APP_URL=http://localhost. I see many people forget to change it to real URL of your app, why is it important?
To be precise, this value is actually used in config/app.php, like this:
'url' => env('APP_URL', 'http://localhost'),
So, you can call that value by config(‘app.url’).
Funny thing is that your application will still work even if you don’t change that URL. The problem will appear when using some functions that are calling config(‘app.url’) or directly env(‘APP_URL’).
Inside of Laravel itself, you can find these:
In short, your email notifications will point to the wrong URL, if you don’t set it properly.
Also, console commands won’t work properly, because console doesn’t really know what the URL is, as it’s not called from the browser.
But it’s not only that. A lot of external packages rely on APP_URL setting. For example, mega-popular Spatie Media Library for file uploads (which we use inside of our QuickAdminPanel), uses configured APP_URL to form the URLs of actual files to be returned.
For example, if you call this:
$user->getFirstMediaUrl('main_photo');
It will use APP_URL to form the final URL. And if you leave it at http://localhost instead of actual URL, you will probably get a broken file or 404 image on your website.
So, my advice is to always change APP_URL in your .env file immediately after project install – on your local computer, and all servers – staging or live.
Read more about Laravel configuration in the official documentation.
Try our QuickAdminPanel generator!
16 Comments
Leave a Reply Cancel reply
Recent Posts
Try our QuickAdminPanel Generator!
How it works:
1. Generate panel online
No coding required, you just choose menu items.
2. Download code & install locally
Install with simple "composer install" and "php artisan migrate".
3. Customize anything!
We give all the code, so you can change anything after download.
Always a good reminder. Thanks
What about multi-domains app? One app that served various domains using common database.
Thanks for the comment, it’s more complicated then, and quite individual, there’s no one correct way to structure it.
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$url = $this->app->request->getHost();
/**
* make a protocol
*/
$protocol = (env(‘IS_HTTPS’) == true ) ? ‘https://’ : ‘http://’;
/**
* concatenation the string
*/
$addressUrl = $protocol.$url;
config()->set(‘app.url’,$addressUrl);
cool
Well it doens’t work in the context of console commands and some mail sending, as pointed in the article.
Raised a nice point regarding updating the APP_URL, also after updating the APP_URL, the config cache needs to be cleared as well.
for production purpose what will be the APP_URL
how to change APP_URL? because my picture/image can’t appear
It should be in your .env file on the server.
Or, if you don’t have it, then in config/app.php
Why doesn’t laravel auto detect the app url? … as in get it from the request
Its also useful while displaying uploaded images. Images take the correct path only when app url is defined properly.
image) }}” alt=”” title=””>
Thank you very much!! I change my APP_URL from http://localhost to http://127.0.0.1:8000 and it work! My broken picture is appear
Hello, i know it’s been a lot since your comment but i am using xampp and i made that change a still my images don’t show. How do you get the image? via the getUrl() function?
I use this middleware to force the site to use the APP_URL instead of any URL
“`
path();
if ($request->url() || $correct_url) {
return redirect($correct_url, 301);
}
return $next($request);
}
}
i did change app_url in .env and config/app.php to url of the server but i still get 404 not found when i click on uploaded image
the project is deployed on heroku