Laravel env('APP_URL') is not returning the correct value for localhost
Solution 1
I found out there is an undocumented feature when loading .env files. If you have a .env.dev file in your project folder it will load this config over everything else if your application is in dev mode. It's documented that .env.testing is used in testing which makes sense but it also does it for dev with .env.dev
The application I was working on had a .env.dev and it was this file that had the http://localhost
string inside of it.
Solution 2
"localhost" is probably the default value. You error indicates that the .env file is not read at all. Make sure you're not editing .example.env, check if other env variables are accessible, check for typos, check file permissions on .env and if it's located in the root folder of the project.
Mark
Updated on September 01, 2022Comments
-
Mark over 1 year
When trying to retrieve the APP_URL from the Laravel config it returns the wrong URL for development only.
My env file has the following:
APP_URL=http://127.0.0.1:9000
However when I call
env('APP_URL')
it returns me:'http://localhost'
Which will not work with my current docker set-up it has to be 127.0.0.1:9000
In my config/app.php file I have the following:
'url' => env('APP_URL')
I have tried
php artisan config:cache
andphp artisan config:clear
but I still get the same result of http://localhostAny ideas on where it could be getting http://localhost from other than the .env or config/app.php?
Thought it would be worth noting using
config('app.url')
also returnshttp://localhost
-
Brian Thompson over 4 yearsDo you have the same problem with other env values or just this one? Is the env file named correctly/have correct permissions?
-
nmfzone over 4 yearsHow is it possible? Usually it's just because you're caching the env. But, you said that you've ran
config:clear
. Then, it's should be you're editing the wrong.env
file. -
Mark over 4 years@nmfzone it's because Laravel loads .env.dev files (not documented) if the application is in dev mode. See my answer below.
-
Robert Bryan Davis about 2 yearsIF I USE route() in my code it returns localhost. if i use route() in CLI, it returns the proper domain. app['url'] is set to "mydomain.us-1.sharedwithexpose.com. APP_URL is set to the same in env file. but why would route() return the proper domain and url in artisan tinker, but not do so when called from code? Any Suggestions? I do not have a .env.dev file present. i do have a .env.example. I changed the APP_URL there to no avail. I too am using docker, but I am using Expose from BeyondCode to share my localhost site with a client.
-
-
Alberto over 4 yearsThat is so interesting, can you link where you found this information?
-
nmfzone over 4 yearsThat's what I mean. You're editing the wrong
.env
file. It's documented in 4.2 here fyi. If you ever worked with Laravel 4.2, you'll familiar with it. -
Mark over 4 years@nmfzone I never used Laravel until version 5 so I had no idea about the legacy of this feature.
-
Mark over 4 years@Alberto I stumbled across it by chance when trying different things in the project. I don't think its documented anywhere, certainly not in the 5.x and 6.x docs. nmfzone links to some docs for version 4 although different (using .php extensions) it does mention having different .env per environment.