Best way to have a config array file available to access everywhere in a clean, elegant and secure way?

I’ve built a simple mvc application for educational purposes to improve my skills however right now i’m stuck trying to implement a config feature that will allow the array data file seen below to be accessed anywhere throughout the application.

I know i could simply set this array as a constant however i feel like that’s cheating and i would rather attempt a laravel / cakephp config kind of implementation. $app->config('application.name')

i feel like i’m overthinking this wayyy too much such as should i build a simple container, should i use static, should i create a function and just load the config.php file in each time but performance etc etc. but i would just really appreciate any help in implementing an extremely simple solution to be able to constantly access this files array.

Thanks so much.

config.php:

return [
    'application' => [
        'name' => 'Application Name',
        'version' => '2021.1',
        'timezone' => 'UTC',
        'default_language' => 'en',
    ],

    'core' => [
        'environment' => 'development',
    ],

    'database' => [
        'default'  => [
            'driver'   => '',
            'host'     => '',
            'user'     => '',
            'password' => '',
            'name'     => '',
        ],
        'idk' => [
            'driver'   => '',
            'host'     => '',
            'user'     => '',
            'password' => '',
            'name'     => '',
        ],
    ]
];

Check out the Laracast Php Practitioner free video series. One episode is a simple container. I suggest just start from the beginning and going through the whole series. These are the only videos I have recommended to anyone.

https://laracasts.com/series/php-for-beginners

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.