folded / config
Configuration utilities for your PHP web app.
Requires
- php: >=7.4.0
- folded/exception: 0.4.*
- illuminate/config: 7.*
- vlucas/phpdotenv: 5.*
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.*
- pestphp/pest: 0.3.*
- phpunit/phpunit: 9.*
README
Configuration and environment utilities for your PHP web app.
Summary
About
I created this library to get an easy to setup way to perform configuration/environment value fetch.
Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.
- folded/action: A way to organize your controllers for your web app.
- folded/config: Configuration and environment utilities for your PHP web app.
- folded/crypt: Encrypt and decrypt strings for your web app.
- folded/exception: Various kind of exception to throw for your web app.
- folded/history: Manipulate the browser history for your web app.
- folded/http: HTTP utilities for your web app.
- folded/orm: An ORM for you web app.
- folded/request: Request utilities, including a request validator, for your PHP web app.
- folded/routing: Routing functions for your PHP web app.
- folded/session: Session functions for your web app.
- folded/view: View utilities for your PHP web app.
Features
- Can get configuration or
.env
values (using dot syntax) - Can check if a configuration or an env value exist
- Eager load the configuration, such as it is only booted when you get a configuration or an env value
Requirements
- PHP version >= 7.4.0
- Composer installed
Installation
1. Install the package
In your project root directory, run this command:
composer require folded/config
2. Set up the library
Right before getting the config or env values, integrate this script:
use function Folded\setConfigFolderPath; use function Folded\setEnvFolderPath; setEnvFolderPath(__DIR__); setConfigFolderPath(__DIR__ . "/config");
You don't have to set up both if you only want to use one or the other. However, if you plan to use both, make sure that setEnvFolderPath()
is called before setConfigFolderPath()
in order to take advantage of env variables in configurations.
The env folder path is the folder that contains your .env
file.
Examples
Since this library uses vlucas/phpdotenv for the env feature, you can learn more on this file format in the documentation.
- 1. Getting a configuration value
- 2. Getting an env value
- 3. Checking if a configuration exist
- 4. Check if an env value exist
- 5. Get all config variables
1. Getting a configuration value
In this example, we will get a configuration value.
use function Folded\getConfig; echo getConfig("app.name");
Asuming your configuration file is located at config/app.php
, and contains:
return [ "name" => "Folded", ];
2. Getting an env value
In this example, we will get a value stored in the .env
file.
echo Folded\getEnv("APP_NAME");
Asuming your .env
file contains:
APP_NAME="Your app name"
3. Checking if a configuration exist
In this example, we will check if a configuration key exist.
use function Folded\hasConfig; if (hasConfig("app.name")) { echo "config exist"; } else { echo "config don't exist"; }
4. Check if an env value exist
In this example, we will check if an env variable exist.
use function Folded\hasEnv; if (hasEnv("APP_NAME")) { echo "has env"; } else { echo "has not env"; }
5. Get all config variables
In this example, we will get all the key/values inside the config file app.php
.
use function Folded\getAllConfig; $app = getAllConfig("app"); echo $app["name"];