Issue in accessing server key from env (Laravel) - laravel

I have my server key in .env on production but sometimes notification stops working as server key not accessable .
But when I run the command php artisan config:cache then it again starts working .

Do not access the variable of .env directly in controller or inside any file. First you have to access that variable in any config file.
Like below:
For e:g; inside config/app.php define your server key
'server_key' => env('server_key')
after defining above key in config/app.php you have to access it inside any file as below:
config('app.server_key')
By doing this you will permanantly resolve your issue.

Related

Laravel route returns localhost despite I changed the env variable

I have APP_URL env variable set to domainname.com and I have url set to the same domain in config file as well. route('route.name') in artisan tinker returns the proper domain. yet when used in the applicaiton code, it returns localhost. Any thoughts from you would be appreciated.
Edit: My environment is Github codespaces
Try running php artisan optimize:clear command to clear cache,
routes etc all in one go.
If you have separate frontend then change it there as well to hit the
right backend with your provided domain name.
Restart your docker containers if you are using Docker.

production.ERROR: No application encryption key has been specified in Laravel

In my Laravel-8, I deleted .env when I'm about to deploy to production.
In the .env, I have:
APP_KEY=base64:JMKPPddG+sPK/ufZKNGwGTStKbMLO2Vnv/4i2fA3j1c=
and config/app:
'key' => env('APP_KEY'),
Everything was working fine until when I deleted .env
When I run the application, I got this error:
production.ERROR: No application encryption key has been specified in Laravel
I don't want to use .env
How do I get this resolved?
Thanks
first run this command to cache configurations:
php artisan config:cache
then u can remove .env
note: calling env() helper from your code will always return null because .env values can be accessed only from config files.
blocking access to .env file in the server is a better solution though.

Laravel env('APP_URL') is not returning the correct value for localhost

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 and php artisan config:clear but I still get the same result of http://localhost
Any 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 returns http://localhost
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.
Just in case anyone is having this issue in production, these are the steps that worked for me:
Make sure that the .env file is specifying that the app is in production. APP_ENV=production
Run php artisan optimize:clear
"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.

Replace localhost with real domain name in reset password of Laravel 5

I am using the default resetting passwords in Laravel 5.4. The function works fine, and the email with reset password link is successfully sent/received.
However, it still not show the real domain but 'localhost' like something below:
http://localhost/password/reset/4c16a78f3c1216de566bc0689694980ac53555311d0214bbb73fbc72870a91f5
Can you tell me how to replace 'localhost' with domainname.com?
And is it OK to do this with http:// instead of https:// ?
Change the APP_URL parameter in your .env file to adapt the app's domain name.
APP_URL=http://localhost to APP_URL=https://yourdomain.com
If you cache your config (which you shouldn't do in local or test environment but definitely in production), don't forget to use the php artisan config:clear command.
update APP_URL in .env file and clear the cache to reflect changes
You can update both .env and config>app.php files with the correct domain url

getenv not working in EC2 Server

I'm trying to deploy to production my app that is working well in local.
Thing is when try:
dd(getenv('APP_ENV'));
it returns "false"
but when I connect ssh and type:
php artisan env
I get
production
Any idea why it stopped working???
For the record, In production, my deploy script execute 3 commands:
composer dump-autoload -o
php artisan route:cache
php artisan config:cache
I mention it because it is possibly the only software config that is different.
EDIT: I identify that the problematic command is:
php artisan config:cache
If if do:
php artisan config:clear
problem is solved.
Tx!
When using a cached config the .env file is not used anymore so getenv is useless, because the config is loaded from:
bootstrap/cache/config.php
Instead you can get the current environment from the loaded application configuration like so:
config('app.env');
Or directly using the app helper function:
app('env');
As a third option you can always use the environment method to get the current environment:
app()->environment(); // or App::environment()
Laravel uses the dotenv library internally to load the configuration keys from the .env file and add them to the environment variables using putenv, but when you cache your configuration that loading part is not done anymore because Laravel detects that there is a cache file present and uses that instead, so those keys from .env are not loaded into the environment, this not accessible via getenv.
And because configuration values from the .env file are only cached when they are used in an actual configuration file from the config directory, you need to create a configuration option for them to be cached and accessible when you're using the cache.
So if you want to have a BASE_URL key in your .env file with this value:
BASE_URL=http://domain.com/
If you want to be able to access its value when the configuration is cached, you need to use it in a configuration file. For example, you can add it to your config/app.php file like so:
'base_url' => env('BASE_URL')
Then you can access even when the configuration iit using:
config('app.base_url')
You can read more about accessing configuration values in the Laravel Documentation.

Resources