Use session, cache or global variables from Laravel console - laravel

I'm processing transactions for many users, from the console/command. For each user/transaction processed, I need the user_id value accessible in a package I've created.
What would be the best way to share the user_id value to the package? Session variable? Cache helper? A global variable?
Thanks

you can use cache or config.
for config, create a global.php file in the config directory
and set your variables by Config::set('global.user_id' , $user->id) and for get that use Config::get('global.user_id')
for more information about config files read this useful link
also you can use redis database

Related

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

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.

How to access storage file from .env file in Laravel

I created a JSON file to use BigQuery in my Laravel project => BigQuery docs. I put the file in the storage folder to limit its access.
I only need to access it from my .env file.
GOOGLE_APPLICATION_CREDENTIALS='/storage/file.json'
Naturally, I cannot access the folder that easily and I know there are ways to access it but creating a symbolic link would make the file accessible from anywhere and I don't want that. Is there a secure way to access that file in my .env file ? Or is there a better way, another folder in which I should put the JSON file ?
I highly discourage the usage of ENV variables, instead use a Secret Manager to load at runtime, or KMS (Key Management Service)
Look at laravel-env-security for implementation.

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.

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

Dynamically updating config data codeigniter

I have created my custom config file to store information about site such as if it is online or offline like-vice.
For that I have created new file in config folder and stores default values in global $config[] array with my own index.
I want to update these config data dynamically with admins control eg. he can select to put site in offline mode.
For that I have used function
$this->config->set_item('config_array_index','value_to_set');
but, I don't know why it is not working for me ?
I am not able to see any update in my config file. Also I am autoloading my config file.
Am I missing something?
Setting a config item only applies to the current session - it does not overwrite your actually codeigniter files.
If you want to permanetly take a site offline, your'll need some sort of persistant storage, such as a value in a database that is checked/updated as needed
you can create an empty config file, within your config directory , and then append your data to it using a functionality like fwrite ( you can check for some other CI function to use ).
and add it to your autoload file .

Resources