Creating a config file in PHP

前端 未结 10 2113
难免孤独
难免孤独 2020-11-27 10:36

I want to create a config file for my PHP project, but I\'m not sure what the best way to do this is.

I have 3 ideas so far.

1-Use Variable<

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 10:59

    I use a slight evolution of @hugo_leonardo 's solution:

     'localhost',
        'username' => 'root',
        'pass' => 'password',
        'database' => 'db'
    );
    
    ?>
    

    This allows you to use the object syntax when you include the php : $configs->host instead of $configs['host'].

    Also, if your app has configs you need on the client side (like for an Angular app), you can have this config.php file contain all your configs (centralized in one file instead of one for JavaScript and one for PHP). The trick would then be to have another PHP file that would echo only the client side info (to avoid showing info you don't want to show like database connection string). Call it say get_app_info.php :

    app_info);
    
    ?>
    

    The above assuming your config.php contains an app_info parameter:

     'localhost',
        'username' => 'root',
        'pass' => 'password',
        'database' => 'db',
        'app_info' => array(
            'appName'=>"App Name",
            'appURL'=> "http://yourURL/#/"
        )
    );
    
    ?>
    

    So your database's info stays on the server side, but your app info is accessible from your JavaScript, with for example a $http.get('get_app_info.php').then(...); type of call.

提交回复
热议问题