Make config file in php

PHP config

PHP config tutorial shows how to create configuration files in PHP. It uses the hassankhan/config package.

$ php -v php -v PHP 8.1.2 (cli) (built: Aug 8 2022 07:28:23) (NTS) .

The hassankhan/config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files. If we work with YAML files, we need to install symfony/yaml package.

Setting up PHP config

First, we install the necessary packages.

$ composer req hassankhan/config symfony/yaml

We install two packages with composer.

This is composer.json file. We also enable autoloading.

PHP config JSON example

In the first example, we read the configuration data from a JSON file.

We have db.json in the config directory.

get('app.port') . "\n"; echo $conf->get('db.host') . "\n"; echo $conf->get('db.port') . "\n"; echo $conf->get('db.name') . "\n";

We load the configuration file either with Config::load or Config . The values are retrieved with the get method. The dot character is used to go through the hierarchy of attributes.

$ php read_json_cfg.php 3000 localhost 27017 ydb

PHP YAML example

In the second example, we read configuration data from a YAML file.

app: port: 3000 db: host: localhost port: 27017 name: ydb
get('app.port') . "\n"; echo $conf->get('db.host') . "\n"; echo $conf->get('db.port') . "\n"; echo $conf->get('db.name') . "\n";

The example reads configuration file from the db.yaml file.

$conf = new Config('config/db.yaml', new Yaml);

In the second parameter, we provide the configuration parser.

$ php read_yaml_cfg.php 3000 localhost 27017 ydb

Merging configuration files

The merge method groups configuration files.

app: port: 3000 db: host: localhost port: 27017 name: ydb

This is the first configuration file.

This is the second configuration file.

merge($conf2); echo $conf->get('db.port') . "\n"; echo $conf->get('db.name') . "\n"; echo $conf->get('version') . "\n";

In the example we merge the two configuration files. We can access attributes from both files with one object.

Code configuration with AbstractConfig

We can specify the configuration details in code by using AbstractConfig .

 'localhost', 'port' => 80, 'servers' => [ 'host1', 'host2', 'host3' ] ]; > >

The configuration is specified in the AbstractConfig’s getDefaults file.

get('host') . "\n"; echo $conf->get('port') . "\n"; echo $conf->get('servers')[0] . "\n";

The example reads the configuration from the code.

Читайте также:  Парсинг номера телефона python

In this tutorial, we have shown how to read configuration files in PHP with hassankhan/config package.

Источник

How to make config file for php

Solution 1: You can either use which could be or reset your directory by navigating in upper level folder and then going to your require file So you can manage to include with As stated in documentation Edited Let’s analyze include path. config file php config file php Question: I am developing a project where I am not able to include the files.

How to make config file for php

 '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.
 'localhost', 'username' => 'root', 'pass' => 'password', 'database' => 'db' ); ?>

Create Config Files in PHP, We can create an array of the configuration items to form a config file in PHP. An

How To Work With PHPs Configuration File

The php.ini file is the settings or the configuration file for your PHP, each version can have Duration: 7:26

PHP Forms Tutorial: Create the Config File

In this tutorial, you’re going to learn how to create and HTML form that submits data to a
Duration: 2:23

Configuring phpList #3 — Editing the config.php file

Some settings for phpList are available only by logging into the web server and editing the Duration: 31:08

Including config file in PHP

I am developing a project where I am not able to include the files.

--Myproject -----Config ----------config.php -----Includes ----------Images ---------------image.jpg ----------CSS ---------------test.css ----------JS ---------------test.js -----Modules ----------Home ---------------index.php ----------Contact ----------MyPage 

I am trying to access the config file which is inside the Config/config.php in my Modules/Home/index.php

Читайте также:  Java lang illegalargumentexception at java util random nextint

But I am not able to include the config file?

1. define("ROOT", __DIR__ ."/"); 2. define("HTTP", ($_SERVER["HTTP_HOST"] == "localhost") ? "http://localhost/myproject/" : "http://your_site_name.com/" );  3. define('PROJECT_ROOT', getcwd()); 4. $_SERVER['DOCUMENT_ROOT']; Ref: [link][2] 5. echo $_SERVER['SERVER_NAME']; 

How can I like a config.php which is out side the folder structure but inside my project?

You can either use absolute path which could be /Myproject/Config/config.php or reset your directory by navigating in upper level folder and then going to your require file ../../Config/config.php

So you can manage to include with

As stated in include documentation

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

Let’s analyze include path. We are actually in Modules/Home/ folder. te reach root level and can get inside Config folder we need to go two level upper, and we can do this by doing ../ for each level, so in our case ../../ . Now that we are in root directory we can navigate through Config/ and get our desired file config.php . Now mixing all toghter will will have ../../Config/config.php .

What you could do, is include the following lines to your index.php path :

define("LOCAL_PATH_MODULES", dirname(__DIR__)); define("LOCAL_PATH_APP", dirname(LOCAL_PATH_MODULES)); define("LOCAL_PATH_CONFIG", MODULES_PATH . DIRECTORY_SEPARATOR . 'config'); require LOCAL_PATH_CONFIG . DIRECTORY_SEPARATOR . 'config.php'; 

What you should do, is put a file named bootstrap.php in your Modules folder.

Instead of the code hereabove, add the following code to your index file.

define("LOCAL_PATH_BOOTSTRAP", dirname(__DIR__)); require dirname(__DIR__) . 'bootstrap.php'; 

Now, add this to your bootstrap file :

// ----------------------------------------------------------------------- // DEFINE SEPERATOR ALIASES // ----------------------------------------------------------------------- define("URL_SEPARATOR", '/'); define("DS", DIRECTORY_SEPARATOR); define("PS", PATH_SEPARATOR); define("US", URL_SEPARATOR); // ----------------------------------------------------------------------- // DEFINE ROOT PATHS // ----------------------------------------------------------------------- define("RELATIVE_PATH_ROOT", ''); define("LOCAL_PATH_ROOT", $_SERVER["DOCUMENT_ROOT"]); define("HTTP_PATH_ROOT", isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : ( isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : '_UNKNOWN_')); // ----------------------------------------------------------------------- // DEFINE RELATIVE PATHS // ----------------------------------------------------------------------- define("RELATIVE_PATH_APP", dirname(LOCAL_PATH_BOOTSTRAP)); define("RELATIVE_PATH_LIBRARY", RELATIVE_PATH_APP . DS . 'Lib'); define("RELATIVE_PATH_ADMIN", RELATIVE_PATH_APP . DS . 'Admin'); define("RELATIVE_PATH_CONFIG", RELATIVE_PATH_APP . DS . 'Config'); define("RELATIVE_PATH_MODULES", RELATIVE_PATH_APP . DS . 'Modules'); define("RELATIVE_PATH_ASSET", RELATIVE_PATH_APP . DS . 'Includes'); define("RELATIVE_PATH_ASSET_IMG", RELATIVE_PATH_ASSET . DS . 'Images'); define("RELATIVE_PATH_ASSET_CSS", RELATIVE_PATH_ASSET . DS . 'CSS'); define("RELATIVE_PATH_ASSET_JS", RELATIVE_PATH_ASSET . DS . 'JS'); // ----------------------------------------------------------------------- // DEFINE LOCAL PATHS // ----------------------------------------------------------------------- define("LOCAL_PATH_APP", LOCAL_PATH_ROOT . RELATIVE_PATH_APP); define("LOCAL_PATH_LIBRARY", LOCAL_PATH_ROOT . RELATIVE_PATH_LIBRARY); define("LOCAL_PATH_ADMIN", LOCAL_PATH_ROOT . RELATIVE_PATH_ADMIN); define("LOCAL_PATH_CONFIG", LOCAL_PATH_ROOT . RELATIVE_PATH_CONFIG); define("LOCAL_PATH_MODULES", LOCAL_PATH_ROOT . RELATIVE_PATH_MODULES); define("LOCAL_PATH_ASSET", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET); define("LOCAL_PATH_ASSET_IMG", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET_IMG); define("LOCAL_PATH_ASSET_CSS", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET_CSS); define("LOCAL_PATH_ASSET_JS", LOCAL_PATH_ROOT . RELATIVE_PATH_ASSET_JS); // ----------------------------------------------------------------------- // DEFINE URL PATHS // ----------------------------------------------------------------------- define("HTTP_PATH_APP", HTTP_PATH_ROOT . RELATIVE_PATH_APP); define("HTTP_PATH_LIBRARY", false); define("HTTP_PATH_ADMIN", false); define("HTTP_PATH_CONFIG", false); define("HTTP_PATH_MODULES", false); define("HTTP_PATH_ASSET", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET); define("HTTP_PATH_ASSET_IMG", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET_IMG); define("HTTP_PATH_ASSET_CSS", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET_CSS); define("HTTP_PATH_ASSET_JS", HTTP_PATH_ROOT . RELATIVE_PATH_ASSET_JS); 

Now, you have a constant for both the local and HTTP variant of the most important paths in your application.

See the PHP PowerTools Boilerplate for a demo of this bootstrapping technique.

How To Work With PHPs Configuration File, The php.ini file is the settings or the configuration file for your PHP, each version can have Duration: 7:26

Creating a config file for a website in PHP

I am creating a website template that will have an index file that will include a phrase from config.php.

Let’s say I have a file called config.php with the following code:

How would I take $sitename from that config file to an HTML file with the code like this:

Would this work? If not, how could I fix it?

Yes, logic is correct, but code no.

Using PHP to pass absolute file paths from config file to HTML, Whenever I try to use either of these stored paths to access files in my project I get nothing. Here is my configuration file and excerpts from

Источник

My Blog

Profile Picture

Hi, welcome to the place where I write stuff on the Internet!

Creating Configuration Files in PHP

Creating your own configuration files in PHP is a relatively simple process. You can do this using the include construct.

Example config.php

 return [ 'db' => [ 'username' => 'admin', 'password' => '', 'dbname' => 'testdb', ], 'memcached' => [ 'server' => '127.0.0.10' ], ]; 

Now we can load the config file like so…

 $config = include 'config.php'; echo $config['db']['username']; // admin 

Using return allows us to load the config in any scope and store it in any desired variable wihtout having to rely on global variables.

Some people do the following…

 $config = [ 'db' => [ 'username' => 'admin', 'password' => '', 'dbname' => 'testdb', ], 'memcached' => [ 'server' => '127.0.0.10' ], ]; 

and then load the config like so…

 include 'config.php'; echo $config['db']['username']; // admin 

However, this is bad because you’re relying on global variables. If you’re already doing this and want a cleaner solution it would be to include the file in a function and return the value from that function instead.

 function loadConfig($fileName) < include $fileName; return $config; > $config = loadConfig('config.php'); echo $config['db']['username']; // admin 

Источник

Оцените статью