I am building an application using the latest copy of Codeigniter 2.0. My application is dynamic and is kind of like a custom CMS I guess you could say. I have a database table called 'settings' with the following fields:
id
name
value
Basically what I am currently doing is using a helper function to retrieve specific settings from my settings table like the site name or current theme. However I've started thinking that maybe the constant amount of database calls for retrieving settings is a bit too much on the database.
Is there a way of retrieving settings for my application from the database and then appending them to my configuration file? I've noticed Mojomotor does something similar and it is a CI 2.0 application, however I would much rather the simplest and easiest code to do so.
I would preferably like to be able to check every so often if a setting in the database has changed and update the configuration file. The less strain on the database the better.
The best solution lies in the middle. Not zero DB calls; and not one DB call per setting. Do one DB call per page load instead, and get every setting in a recordset / object that the rest of your app can refer to as needed.
Related
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 ]);
I am having trouble with my API written with Laravel. Basically, I modify a value directly in the database used with Laravel, but it seems to get the original value(s) not the one that has been modified.
At first I thought maybe it could be cache related, but we aren't using any cache drivers. Other than the cache drivers, is there some sort of magic caching Laravel is doing behind the scenes? Is this normal behaviour for laravel apps?
Is there something else I need to clear each time I modify a value directly in the database?
If I were to modify this value via the API itself, then I am given the correct modified value.
Thank you!
This similar question have been asked here
I'm creating an application which stores some settings in the database and ideally it would be good to load these settings during bootstrapping and make them available via an object globally.
Can this be done and added to Yii::$app->params somehow?
Such as you can create a class and return the details as an array or object instance?
But in my case i have different settings for different users and want to be able to add config details such as language, preferred date format etc after a user has logged it, based on the user sittings option
Your question is a bit mysterious. Generally,
Yii::$app->params
is a table of parameters attached to an application singleton.
But there is also a
Yii::$app->session
and ou may use this object to set and get user's session specific data and this way make them available "globally" withing an application.
Have a read:
http://www.bsourcecode.com/yiiframework2/session-handling-in-yii-framework-2-0/
I hope this might be helpful!
I am building project based on CodeIgniter 3.
My first idea was to store user settings in additional config file and call ex. $this->config->item('item_name').
However I need not only read those settings but to save them.
So my cuurent idea is to build additional tab_settings and store all my config values as 1 row.
The question is how to call those settings in CodeIgniter controllers/views.
Do I need to make SQL Query in every controller, or is it a way to do it one time only?
Or maybe is better idea for storing user settings in CI?
You can save config settings in CI using:
$this->config->set_item('item_name', 'item_value');
Depending on what kind of setting your saving and how many there are, you may be better off with using the DB. If the settings are independent of each user, I would include the DB code in your user model, then when your user logs in, store their setting in the session to avoid excess DB calls. Otherwise create a separate model for the settings. You should not be interacting with the DB directly within a controller or view, always in a model.
I installed 2 WordPress installations in my site and they share the same database, only, wordpress1 has table prefix of wp1_ and wordpress2 with wp2_ . How can I query the posts from wordpress1(wp1_) to show in the sidebar widget of wordpress2? I can only show posts from wp1_ to wordpress1 (its own table).
It isn't too hard to pull off -- make a copy of the $wpdb object and tinker with it, and wrap calls to the native widget code in that. That is, assuming that the database access credentials are the same. But I wouldn't recommend going this route at all; it is difficult to maintain, and you really should practice proper separation of the concerns. It just takes one slip-up and database A will happily write all over database B.
Have you considered using an RSS widget instead? The http calls could be done over loopback and so shouldn't add much overhead, and the separate databases will remain... separate.