简体   繁体   中英

What is the best way to store AND update global variables programatically for your website in laravel 8?

I need to store some global variables for my laravel website, but I need to update them programmatically. Here is my situation:

作为我的管理仪表板的一部分,当访问者访问网站时,管理员可以启用弹出窗口。

the admin should be able to enable popups, and configure which post it has to link, which will show when a visitor comes to the website.

Other answers and why they did not satisfy me.

  • Making a laravel config file.
  • A database table.
  • Static variable somewhere in a controller or model.

A Laravel config file seemed to be the best option at first, but it didn't fit with the need to update them at running time. I've readt answers that suggested to call an artisan cache clear in the controller in order to update the values. but this seems just off to me. I don't think its a good idea to mess with the cache like that.

A database is still an option, however, it has some downsides as well. Making just an SQL table for 2 config variables seems like a waste of tables, it also means i need to make 2 query's on the admin dashboard, and also 1 on the homepage (to get the popup config), which i rather keep database-free.

A static variable in a model or controller . I saw this suggestion as well altough noted: it is probably a very bad design choice. Nevertheless i tried it in a desperate attempt and it didnt work. It did not stay updated on page reload.

I'm a laravel noob in case you didn't notice. Is there anything I am doing or understanding wrong? Or is there a solution I am not aware of?

There is no need for me to save the variable when the website is offline. It would be nice if it did but its only a minor inconvience for the admin to set it on restart.

For your situation, spatie write a nice package .

Just install as in documentation and use.

Use a db table to store the configuration, also having one extra table does not have any serious downside and it won't hurt the performance, also most popular applications/frameworks use it.

To reduce the db queries, create a wrapper class for your db config load, and in your wrapper cache the data for some amount of time, and when you want to change the config, remember to invalidate the cache.

If you want global access to it, bind it to laravel service provider, and use Facade or other container methods to fetch it. Also with this approach you keep the exposed config interface the same even if you change the implementation different in future.

About file solution: If you have one admin, you can go with file solution, but you never know how they will grow in numbers in future and it will be a hassle to go around what you did in file.

You can set config values dynamically at runtime with config() helper:

config([ 'app.popup1' => true ]);

Another solution is to write the config value into session at startup and only update the session:

session([ 'app.popup1' => true ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM