Laravel Project is Serving without .env file - laravel

If .env is removed on my Laravel project the command
$ php artisan key:generate
Causes this error
ErrorException : file_get_contents(/folder/projectlocation/.env): failed to open stream: No such file or directory at //folder/projectlocation/vendor/laravel/framework/src/Illuminate/Foundation/Console/KeyGenerateCommand.php:96
{
file_put_contents(
$this->laravel->environmentFilePath(),
preg_replace(
$this->keyReplacementPattern(),
'APP_KEY='.$key,
file_get_contents($this->laravel->environmentFilePath())
));
}
However the php artisan serve command works "Laravel development server started: <http://127.0.0.1:8000>"
I have cleared all the Laravel caches. Why will it serve but not fetch the .env file configuration?

That is correct. All application config comes from the config files in the config/ folder. Each of the options in here is configured to take either the value from the .env file or a default value.
For example, the application key in the config/app.php file:
'key' => env('APP_KEY'),
So when no value is set for APP_KEY in .env, the application key will be null (not recommended!).

Related

Laravel after upgrade, .env variable Undefined index

I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV
$_ENV['API_DOMAIN']
I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?
Try using the env helper instead, env('API_DOMAIN')
Run the below commands after making changes in env file. It will flush and repopulate all the env variable.
php artisan config:cache //flush all cached env variable
php artisan config:clear //repopulate all the env variable
php artisan cache:clear //flush all the cached content
You should not directly work with environment values in your application, I would create a new configuration file or use an existing one and add the value there, this way you can cache the configuration.
Lets use your API_DOMAIN as an example:
.env file
API_DOMAIN=www.mydomain.com
Config file
config/api.php for example
<?php
return [
'domain' => env('API_DOMAIN'),
];
Now you can use the config helper to use this variable in your application:
$value = config('api.domain');
You should never use the env() helper outside of config files, because when you cache your config, env() will return null.

Laravel can't change config value

I found the problem, i'll try to use a homestead or docker.
if i restart php artisan serve the changed values are applied.
Same my post: Laravel can't change config value
I make route
Route::get('/config', function () {
return dd(config('app'));
});
And cant change my config/app.php, for example i want to change
'url' => env('APP_URL', 'test'),
'client_url' => env('APP_CLIENT_URL', 'test'),
I set
APP_CLIENT_URL=str1
APP_URL=str2
and there is something inexplicable:
My "url" => "http://127.0.0.1:8000" !!! not default 'test' or from .env, I did not set such a value.
My client_url contains the old value, after I cached the config.
This is not production, i accidentally run config:cache, but after i remove all cache from bootsrap/cache, storage/framework/cache and running cache:clear, config:clear.
After running config:cache values from the .env were put in config, but I do not want to constantly change cache in development and it is not recommended in the documentation.
How can I easily change .env in in developing?

Laravel 5.1 rename project

What is the best solution if I want to rename my laravel 5.1 project, I just tried to rename one but it did not work properly got some errors.
Are there some kind of steps one has to do to rename a laravel project?
You should use php artisan app:name command to rename your app. It will change namespace in all project files for you. This is the only right way to rename your app.
https://laravel.com/docs/5.0/configuration#after-installation
From laravel version 5.3^ there is a function to call the app name
config('app.name');
You can change the default name 'Laravel' inside this file
config/app.php
'name' => 'Laravel',
And also inside .env file
APP_NAME=Laravel
I just recommend to:
go to .env file at APP-NAME =
put your desired name in "" (double quotes)
restart your server
And you're done.
You can change your project name using the following command:
php artisan app:name your_git_name\your_app_name

Why Laravel uses wrong database file after setting environment?

In bootstrap/start.php I have the following:
$env = $app->detectEnvironment(function()
{
if($myenv = getenv('APPLICATION_ENV')):
return $myenv;
else:
return 'local';
endif;
});
Ok so I setup a local folder and put in a database.php file with my local connections.
Just to make sure its picking up the correct environment I put in the template: {{ App::environment(); }} which outputs local.
But when making a DB call its giving me error: Undefined index: DB1_HOST
My base (production) database.php file has:
'host' => $_SERVER["DB1_HOST"],
'database' => $_SERVER["DB1_NAME"],
'username' => $_SERVER["DB1_USER"],
'password' => $_SERVER["DB1_PASS"],
Why is it looking at the production database file?
Laravel also stores the config information in
bootstrap/cache/config.php
In some cases it's not updated which may result in wrong database information. Deleting the file should resolve the issue.
If you are trying to use artisan in your terminal and you want to set the environment variable once and for all, you can do :
export APPLICATION_ENV=local
And check you current environment using php artisan env
Production config files are loaded first and then merged with overrides from other environments. If the production file generates an error (e.g. undefined index) the config loading will bail early without loading the overrides. In the production config file, check the value is set before attempting to use it and the local config file will then load correctly.
Clean the config cache as it may affect
php artisan config:cache

laravel 4: key not being generated with artisan

When running
php artisan key:generate
I can see the generated key in my shell, but the variable 'key' in app.php remains empty.
Running on localhost with windows-apache-php 5.4 - mysql.
Never had this problem before with laravel 4 beta version.
Had the same problem ...
Opened app.php
Remove the entry that says 'YourSecretKey!!!'
Ran 'php artisan key:generate'
Showed me a key in the console, but nothing in app.php!
Solution is ... unlike Laravel 3, don't delete the default YourSecretKey!!! in app.php, just run the command and it will work.
Hope this helps.
Bagwaa
You should not remove the original key, just go to your project directory and run
php artisan key:generate
it will work if you don't touch the previous key.
Have the same problem with L6 on Windows with xampp.
Remove the cached config php artisan config:clear
Regenerate key php artisan key:generate
Hope it helps.
The .env file needs to contain a line like this:
APP_KEY=
You don't have to specify a key, but you have to at least provide a .env file and also the above line.
first type any 32 character like 'hyhyhGGyhyhyhyhy23hyhy23hyhy23hy' this and then re execute the command in terminal/cmd.
Step 1:
go to app ---> Config --> app.php
step 2:
'key' => '10101010101010101010101010101010', type any 32 digit or character in that place.
step 3:
go to terminal / cmd
& type : "php artisan key:generate"
press enter
step 4:
see the key has been changed :)
[ It is because in Laravel 4 By using "php artisan key:generate" we simply can replace the default key any time. But if it is an empty space it can not be able to hold the place. ]
Enjoy coding :) \m/
The key generator will only update the APP_KEY in the .env file.
'key' => env('APP_KEY', 'YourSecretKey'),
config/app.php this is reading the APP_KEY from your .env file. The second parameter is a fallback.
Follow this simple steps:
Remove the cached config php artisan config:clear
Remove existing key or ensure that you have this line : APP_KEY= on your .env file.
Regenerate key php artisan key:generate
I was having the same issue. From my project directory I noticed I had the .env file, when I opened the project in atom (my code editor) I noticed that file appeared as .env.txt, I removed the .txt part and ran the command. It worked for me.

Resources