-1

Hello I want to save the configuration of my web page in a php file.The idea is the configuration is available regardless of whether there is a connection or not to database. I want to be in php files to more secured even if there is not a file forbidding to read the file (json, txt ...). I need to edit the values or add new ones dynamically through the page rather than scrolling through the files.

To solve my problem I found 2 methods used: 1st to record everything in the form of an array:

<?php return array(
'host' => 'localhost',
'username' => 'root', );

2nd to record everything in a flat-file database, but I do not find one that writes the files in php except: This CodeIgniter Flat-File Database Library Who can not work alone without CodeIgniter.

Basically, I've come to the second option if I find something similar to the example. The 1st option implies the use of 1 file that will be difficult to service and it is possible to break something when recording. You can also give other options to dynamically change the values and record them.

Script47
  • 14,230
  • 4
  • 45
  • 66
bedamusa
  • 1
  • 2

2 Answers2

0

Probably the best way would be to create a static class which loads the config in the Registry on init. Take a look:

Class Core { /** @var array $registry Static array for registry variables */ public static $registry = []; }

Now when you want to load the config, you can just use the Core class:

Core::$registry['config'] = include('./config.php');

And now when you require the config, you can just access the array as follows:

Core::$registry['config'][%your_array_path_for_your_config_item%];

Do know that this can be used for many other things as well, think about saving your dispatches and other runtime variables here as well.

Enigmatic
  • 370
  • 2
  • 11
0

The first method is a nice solution. This way you can make multiple config files which are just arrays. If the configuration contains sensitive information you can make use of hidden .env files and in your configuration, you refer to those .env values. This way you can have multiple configurations for multiple environments without changing the config files themselves.

Laravel has a good approach for this problem imho. https://github.com/laravel/laravel

Example from laravel:

config/databases.php

'sqlite' => [
    'driver' => 'sqlite',
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
], 

The second value of the env() represent a default value. The actual value lives in a .env file which is outside of version control.

Christophvh
  • 12,586
  • 7
  • 48
  • 70
  • @bedamusa I am not suggesting you should work with the framework, but simply to use it as an example of how to implement your config setup. – Christophvh Jan 03 '19 at 11:30
  • your idea is to edit only the .env file? - [like that](https://stackoverflow.com/questions/32307426/how-to-change-variables-in-the-env-file-dynamically-in-laravel) – bedamusa Jan 03 '19 at 15:01