Why calling env() function directly in controllers does not work? - laravel

Supporting Laravel 6.20 app, I see a lot of env() methods called in the controllers.
It does not work on my side, so I made a wrapper in the config/app.php file, and using it made it work.
And I have to make a lot of similar changes in the app to make it work properly...
But looks like that env() worked for my client on live and developers server.
I know that calling env() in controllers directly is a bad way but wonder why it worked on servers?
Are there some common PHP configurations?
Thanks!

you have probably cached your configurations by running php artisan config:cache
according to the doc
This command will combine all of Laravel's configuration files into a single, cached file, which greatly reduces the number of trips the framework must make to the filesystem when loading your configuration values.
and then
If you execute the config:cache command, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function for .env variables will return null.
you have to just run php artisan config:clear and this will clear the cache file. you will be able to get the env values again.
but yeah don't use any values directly from .env files. there are some issues. to get more performance, cache the configs. so wrap the env value in a config key like in config/app.php
'name' => env('APP_NAME', 'Laravel')
you are saying that use the APP_NAME key from the env file as your laravel app name and if there's no APP_NAME key or value in env use Laravel as default.
secondly if you make some requests within a very short period of time, like page loads and you are calling some xmlhttp requests at the same time, there's possibility of not getting the env values (returns null) if you have to use any. this is a php bug. so read env values from config files with a default value for performance and for avoiding bug.

Related

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

"Non-static method Redis::get() cannot be called statically" in production but not in local

While developing in the local environment I haven't any problems using Redis in my master.blade.php
<body class="{{Redis::get('app_theme')}}">
untill I deployed my project (Forge+DigitalOcean) and started getting:
ErrorException: Non-static method Redis::get() cannot be called statically
My only thought was is might be deal with PHP itself, but versions are the same.
What else it might be??
UPD:
in my config/app.php specified that
'Redis' => Illuminate\Support\Facades\Redis::class,
You may not be using PHPRedis locally vs on your production.
As per Laravel documentation, if you are using the Redis facade along with PHPRedis, you should use a different name than "Redis" when specifying the alias in your app.php config file:
If you plan to use PhpRedis extension along with the Redis Facade
alias, you should rename it to something else, like RedisManager, to
avoid a collision with the Redis class. You can do that in the aliases
section of your app.php config file.
'RedisManager' => Illuminate\Support\Facades\Redis::class,
https://laravel.com/docs/7.x/redis#phpredis

Whats is Production enviroment in Laravel?

I am curious to know what the APP_ENV entry is used for in Laravel environment files. Is it just for my own usage so I can detect it in code? If I create a fresh Laravel app and change APP_ENV to production what will it change under the hood? Nothing?
Thanks!
Typically you would have a different .env file on each server. It is up to you if you would like different parts of your app to work differently in different environments. The .env file is usually used so that your code can just grab values with the env helper function. This way when you change environments like switching from a test API key to a live API key for example, you can just edit the .env file and not touch the rest of your code.
https://laravel.com/docs/5.5/configuration#environment-configuration

Lumen file cache driver

I'm in Lumen, inside a Controller, and I would like to cache a computation's result in a simple and easy way, without using database or external services, so I was looking for save the cache in the filesystem. In Laravel's documentation there is cited the file driver:
By default, Laravel is configured to use the file cache driver, which
stores the serialized, cached objects in the filesystem.
And I can see it, configured as Default Cache Store, inside config/cache.php.
In Lumen's documentation i can't see anything about the file driver and I find nothing like the file cache.php inside Lumen installation.
So my question is if I can use the file cache driver in Lumen (by setting CACHE_DRIVER=file) or if it is discouraged, not supported, not implemented or something else?
In Lumen in .env.example you have by default:
CACHE_DRIVER=memcached
So all you need is to change filename from .env.example to .env and set
CACHE_DRIVER=file
If you read Caching in Lumen you'll see in example:
$value = Cache::store('file')->get('foo');
so file driver is supported by Lumen.
If you also read Lumen Configuration you can read here that you can copy configuration files you need (in case you need them) and load them manually. You can see default Luman cache config file here: https://github.com/laravel/lumen-framework/blob/5.1/config/cache.php

CakePhp2 defined CAKE_CORE_INCLUDE_PATH, now DS and CORE_PATH undefined in ShellDispatcher

In order to share CakePhp2 core between many sites I edited CAKE_CORE_INCLUDE_PATH in webroot/index.php to point to the cake directory. That works and I can reach my welcome page.
However, when I attempted to run the Cake Console, I ran into issues with ShellDispatcher.php not defining DS and CORE_PATH because they were only defined if CAKE_CORE_INCLUDE_PATH was NOT defined. Once I defined them out of the if statement which checks if CAKE_CORE_INCLUDE_PATH was not defined, I was good to go. However, I would prefer not to "hack" this file because I want to keep the cake core files clean. Are there any better and cleaner options?
I also had to defined CAKE_CORE_INCLUDE_PATH and use it to set $dispatcher in Console/cake.php which is part of the app of course.
Yes there are. As a start never define application-wide variables in Webroot/index.php. As a matter of fact you shouldn't be touching this file at all. You can define variables in
Config/bootstrap.php.
You were getting the error because when in a Shell you're not firing Webroot/index.php at all - you're actually running PHP's CLI and CakePHP fires off in a different manner.
It will go through the bootstrap.php file of course.
You should also use this file to define any global vars, but there is a little bottleneck
here when using PHP's $_SERVER variable's web server related members - e.g. HTTP_HOST and the definition: $myHost = $_SERVER['HTTP_HOST'];. If you start a shell Cake will try to set this variable and it will fail throwing an error. This is because as mentioned before PHP will be running in CLI mode when the CakeShell is envoked. There is a way to detect this of course - you can use $_SERVER['SCRIPT_FILENAME'] or $_SERVER['SCRIPT_NAME'] in bootstrap.php to identify if you're in CLI or not. :)

Resources