Laravel: how to cache config files properly - php

I'm using Laravel 8.0 in my application and I added a new .env variable:
APP_URL_FRONT=https://tmsv00008:6204
And how I use it:
To access the project use the following link: LINK
When I deploy the application I cache the config files:
php artisan config:cache
But the env files is not visible and the strange thing is that if I execute:
php artisan config:clear
The variable is visible.
What I'm doing wrong? Because it doesn't make sense to me.
Also, in this article they suggest to execute the commands in this order:
php artisan config:cache
php artisan config:clear
But this doesn't make sense to me. Shouldn't be the other way around? And I think the cache commands also clears the cache first.

The env() function is strongly recommended to be used only in configuration file, since caching the configurations, will make the env function to return null
In other words, crate a configuration file, with that environment variable, or add it to an existing one
It's hidden in an old update guide this "note"

Related

Call to undefined method Closure::__set_state() error on php artisan optimize [duplicate]

My code in on production and I ran
php artisan config:clear
After that, my code was not running. The index pages and all other pages went white screen and gave 500 internal server error in firebug. When I tried to run
php artisan
it gave me error as
PHP Fatal error: Call to undefined method Closure::__set_state() in /var/www/live/vendor/config.php on line 56
My code is in production!! /vendor/config.php file was not present before, what happened with that code??
Have you faced any such error?
I had given all permissions to storage/ folder and vendor/.
Any help/guide would be much appreciated.
I had similar issues when I ran php artisan config:cache. Apparently, it is an issue when the application is trying to load cached configuration files that have closures in it. It won't be fixed in Laravel since it is a bad practice to have closures in config files. Refer this Github issue
The way I solved this is by undo-ing this.
Delete the cache for config.
It is located in here
bootstrap/cache/config.php
OR
vendor/config.php
I had faced the similar issue in past don't know what caused it but as of now you can delete the config.php from /vendor it won't break your code.
And your code will be start working..
Among other root-causes, this error results from calling php artisan config:cache when a closure is defined inside any configuration file that Laravel attempts to load. Laravel does not allow for closures in configuration files; see:
https://github.com/laravel/framework/issues/9625
Deleting the resultant cache file, usually located at bootstrap/cache/config.php, "fixes" the error.
The long-term solution is to eliminate closures from all configuration files. The problematic config file can be determined by inspecting the offending line, as mentioned in the error message.
If the offending file is third-party, it is best to open an issue with the library, so that the issue is fixed upstream.
Here is what I did to solve it:
Go to /vendor/tymon/jwt-auth/src/config/config.php and replace the lines for storage and auth with:
'auth' => 'Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter',
'storage' => 'Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter'
Go to /bootstrap/cache/config.php and delete it
Run the following commands in order:
A) php artisan config/cache
B) php artisan jwt:generate
C) php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
and that should do it!
Try to remove the config.php from bootstrap/cache folder. It worked for me.
Edit vendor\laravel\framework\src\Illuminate\Foundation\Support\Providers\RouteServiceProvider.php:108
and delete required.
Changing the config.php file inside the vendor/tymon/jwt-auth/src/config to this
'auth' => Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter::class`
and this
'storage' => Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter::class`
before runnning php artisan config:cache worked for me.
solved using
composer update
composer install

Run php artisan from one Laravel install to another

We have a Laravel deployment website set up under deploy.mysite.com which handles deployments for a multitude of other websites.
One other website I'm trying to deploy which is also a Laravel site resides under site2.myothersite.com.
Both are on the same server. Deploy calls a script on site2, this deploy script runs various commands after cd'ing to the project directory. We use the following to update the database structure.
php artisan migrate --force
Ordinarily when this is run directly via SSH when in the project root, it runs just fine.
However when this is run via the deployment script (using php exec() to run these commands) the process does work - however, instead of updating the project that we've cd'd into, it updates the database structure of the deployment site!
It seems as if the php artisan migrate command ignores the fact I've cd'd into another project and it takes the database values from the current directory.
How can I go about changing this behaviour?
After playing around with multiple different solutions I eventually came to realise that the problem was the .env file from the project that I was in was setting the environment variables and then weren't being overwritten by anything therefore was then essentially running the code as the wrong site.
What I did to solve this
In my deploy script I manually loaded up the .env file and overwrote the environment variables with overload rather than just load.
Note: This will only work on versions below V5.
$dotenv = Dotenv::create(__DIR__);
$dotenv->overload();
UPDATE for V5
As per #nibnut's comment, since dotenv V5 (now in Laravel 7.0) this loads the environment variables as immutable. In my case I'm happy to allow these to be mutable, in which case I can do the following instead of the above.
$dotenv = Dotenv::createMutable(__DIR__);
$dotenv->load();
https://github.com/vlucas/phpdotenv#immutability-and-repository-customization
This was all I had to do to get my original script working.
NOTE: as this is a laravel install at all ends, the dotenv package was already installed. This could be used for any project by installing it separately.
Rather than cd to the directory, you could change the command to something similar to this:
php /var/www/sitea/artisan migrate --force
That will run the artisan command for the provided directory.
$log = shell_exec("unset DB_HOST &&
unset DB_PORT &&
unset DB_PASSWORD &&
unset DB_USERNAME &&
unset DB_DATABASE &&
composer dump-autoload &&
cd /var/www/yourproject/ && php /var/www/yourproject/artisan migrate:fresh &&
php /var/www/yourproject/artisan db:seed &&
php /var/www/yourproject/artisan seed:translation");
novocaine's answer unfortunately no longer works in Laravel 7.x, since the DotEnv file is loaded immutable.
My current workaround is to create a temporary .env file in my project (i.e. ".env.temp") then use the --env argument when calling my artisan commands:
php artisan migrate --env=temp
This may not work for all use cases, but if you're calling artisan commands in project B from within project A, it should work fine.
novocaine's answer involve reloading .env file. But you can also empty environment variables with env -i.
This method will avoid having some variables not overwritten due to different .env file variable name. For example, your deployment site may have a variable called DEPLOY_KEY in the .env that is non-existent on the .env of site2. Reloading .env file may leave the DEPLOY_KEY variable available on the site2 script. This may leads into security issues.
So, instead of running
exec('php artisan migrate --force');
you'll have just to add env -i:
exec('env -i php artisan migrate --force');
Source: https://unix.stackexchange.com/a/48995/375005

Should I be using env() outside my config files?

I stumbled across this https://laravel.com/docs/5.4/configuration#configuration-caching in the documentation and it confused me a bit.
When I want an environment variable I use the env() function to return what I want. According to the above link it says I should be using the config() function instead to ensure that on production I am accessing the values through a cache.
e.g. These both return the same thing
env('APP_URL')
vs
config('app.url')
So should I be using config() or env() inside my app?
I assume that if I add a new env variable I will also need to update my config files?
You should never use env() in the code directly. It's a good practice to use config(). In config files use env() to get the data from .env file.
In this case, you can easily override config values at runtime or during testing.
You also can use config caching.
To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command.
Another reason is described in the docs:
You should typically run the php artisan config:cache command as part of your production deployment routine. If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files.

laravel : php artisan not working

I know there is bunch of answer out there and none of them solved my problem. I am giving a try again in 2016 may be some one can help.
php artisan make:migration createxxxtable
don't show any error and it don't create the file
neither of these function output anything too.
php artisan list
php artisan help migrate
Its not new project. I had been working on this project earlier and it was working fine. Created bunch of migration.
I tried deleting everying inside vendor and do composer install and composer update still no luck.
I did tried command like dump-autoload , optimize , clear-compiled etc. too
Thanks
Ok found the solution. I don't know whats the reason. But it was because I was using url() helper in a custom config file i created in config folder. The config file and value of url and others works fine all across the website but it just break artisan command. I don't know why.
Removing that url() helper from my custom config file solved the problem.
Normally when this issue arises it's due to an error in your code. Check for these coporates
Could be a typo in your .env file
Could be a syntax error in your .env
Entering in wrong configuration(syntax error) in your config folder
can you please try php artisan does that open up the artisan commands?
If these doesn't show anything then check your path to project in console.

Laravel 5 on php artisan config:clear generated Closure::__set_state() error

My code in on production and I ran
php artisan config:clear
After that, my code was not running. The index pages and all other pages went white screen and gave 500 internal server error in firebug. When I tried to run
php artisan
it gave me error as
PHP Fatal error: Call to undefined method Closure::__set_state() in /var/www/live/vendor/config.php on line 56
My code is in production!! /vendor/config.php file was not present before, what happened with that code??
Have you faced any such error?
I had given all permissions to storage/ folder and vendor/.
Any help/guide would be much appreciated.
I had similar issues when I ran php artisan config:cache. Apparently, it is an issue when the application is trying to load cached configuration files that have closures in it. It won't be fixed in Laravel since it is a bad practice to have closures in config files. Refer this Github issue
The way I solved this is by undo-ing this.
Delete the cache for config.
It is located in here
bootstrap/cache/config.php
OR
vendor/config.php
I had faced the similar issue in past don't know what caused it but as of now you can delete the config.php from /vendor it won't break your code.
And your code will be start working..
Among other root-causes, this error results from calling php artisan config:cache when a closure is defined inside any configuration file that Laravel attempts to load. Laravel does not allow for closures in configuration files; see:
https://github.com/laravel/framework/issues/9625
Deleting the resultant cache file, usually located at bootstrap/cache/config.php, "fixes" the error.
The long-term solution is to eliminate closures from all configuration files. The problematic config file can be determined by inspecting the offending line, as mentioned in the error message.
If the offending file is third-party, it is best to open an issue with the library, so that the issue is fixed upstream.
Here is what I did to solve it:
Go to /vendor/tymon/jwt-auth/src/config/config.php and replace the lines for storage and auth with:
'auth' => 'Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter',
'storage' => 'Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter'
Go to /bootstrap/cache/config.php and delete it
Run the following commands in order:
A) php artisan config/cache
B) php artisan jwt:generate
C) php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
and that should do it!
Try to remove the config.php from bootstrap/cache folder. It worked for me.
Edit vendor\laravel\framework\src\Illuminate\Foundation\Support\Providers\RouteServiceProvider.php:108
and delete required.
Changing the config.php file inside the vendor/tymon/jwt-auth/src/config to this
'auth' => Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter::class`
and this
'storage' => Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter::class`
before runnning php artisan config:cache worked for me.
solved using
composer update
composer install

Categories