3

Here is my directory (the screen-shot in this link). Also I have one more file named config.php which contains some variables, like this:

<?php

    // db
    $dbname = 'mydb';
    $db_username = 'root';
    $db_password = '';

    // login
    $l_email = 'some@one.com';
    $l_password = 'mypas'

    // details
    $sleep_on_error = 1; // per sec

    // and etc 
    // .
    // .
    // .

?>

As you see, I've some variables that are stored into config.php file .. I may need each of those variable in every part of my project. So I need to make config.php file accessible in the whole of project. So I can add config.php file into composer to make it accessible in the whole of project. like this:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/classes/config.php"
    ]
},

All I'm trying to do is implementing a easy-configuration-system for my project. Can please tell me am I doing that correctly? Or should I define a class for doing that? or all I'm doing is wrong and I should do something else?

In short, what's the best structure to make a configuration for the project?

Community
  • 1
  • 1
stack
  • 10,280
  • 19
  • 65
  • 117
  • Configs are usually just key/value pairs, so the format should be simple and easy to maintain. – Jay Blanchard Dec 13 '16 at 13:15
  • @JayBlanchard Do you mean I have to make a file *(not `.php` file, something like a xml file)* and parse it for using? – stack Dec 13 '16 at 13:17
  • Are you aware that PHP actually supports proper constants? Because you're using variables here. – Erik Dec 13 '16 at 13:17
  • That's one way. We sometimes use *.ini* as the file extension, sometimes *.txt*. It is a personal preference. – Jay Blanchard Dec 13 '16 at 13:17
  • 1
    @JayBlanchard The reason many PHP projects actually use a PHP module for configuration is that in constrast to normal text files, under normal circumstances PHP files will never be sent to the client as text, but will always be parsed by the PHP processor and thus don't generate any output, whereas with normal text files you need to make sure with other means that the configuration is never actually sent to the client's browser. So this is actually an additional security feature and your solution - if done wrong - is actually dangerous. – Thorsten Dittmar Dec 13 '16 at 13:19

5 Answers5

4

The Laravel way is using config files. Create my.php and put it in /config directory and then access constant values globally:

config('my.variable')

Here's an example of my.php:

<?php

return [
    'variable' => 15,
    'some-array' => [1, 2, 5],
];
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 1
    wow great .. the most of your answers are perfect .. thank you man .. just can you please add a working example *(show us how `my.php` looks like)* to your solution? – stack Dec 13 '16 at 13:19
  • 1
    Thanks. ) Added an example. – Alexey Mezenin Dec 13 '16 at 13:22
  • Is it possible to do the same but for a function? I mean I want to define a function accessible in all controllers. Is that possible? – Martin AJ Aug 02 '17 at 04:35
  • @MartinAJ sure. Read this https://stackoverflow.com/questions/37339475/how-to-create-helper-methods-on-laravel-not-a-facade/37339565#37339565 – Alexey Mezenin Aug 02 '17 at 07:53
2

You can create a file like @Alexey Mezenim has suggested - say admin-settings.php in your /config directory and populate it with an array containing the constants/values you need globally

//admin-config.php
<?php

    return[
        'db' => [
                   'name' => 'mydb',
                   'username' => 'root',
                   'password' =>''
                ],
         'login' => [
                       'email' =>'some@one.com',
                       'password' => 'mypas'
                    ],
         'sleep_on_error' => 1
    ];  

Then you can access the values anywhere in your application as

config('admin-config.db.name')

config('admin-config.login.email')

config('admin-config/sleep_on_error')  

//and so on
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • 1
    U'r welcome. Thanks for the upvote :). Just expanded on Alexey's answer to align it more closely with your question's context. – Donkarnash Dec 13 '16 at 14:06
2

This is very simple, just in two steps:

1) Create a file say constants.php under config/app and place value in an form of array like:

return [
    'APP_NAMEIS' => 'myappname'
];

After above step,
2) use that code at anywhere by using Config Facade like:

Config::get('constants.APP_NAMEIS');

Hope this clear that how can use the constant anywhere. See attached reference for more detail.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
0

you can easily add constant configs from files like

<? // conf.php
$config = ['dbname' => 'test'];
return $config;

define('config', include('conf.php'));

echo config['dbname'];

chorn
  • 150
  • 2
0

This is how I did in my Laravel 5.2 App

First created a php file with name constants.php under my_app/bootstrap/ and written all the constants needed in the whole app in this file;

Sample View of constants.php

if (!defined('LOGO_URL')) {
    define('LOGO_URL', SITEURL . '/images/' . 'logo.png');
}

if (!defined('GRAVATAR_SIZE')) {
    define('GRAVATAR_SIZE', 100);
}

if (!defined('SITE_NAME')) {
    define('SITE_NAME', 'My App');
}

then require it to my_app/bootstrap/autoload.php like this

require __DIR__ . '/constants.php';

Now I can access this variable throughout the application!

Hope this helps!

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97