404

[ Avaa Bypassed ]




Upload:

Command:

elspacio@13.59.183.77: ~ $
---
layout: default
title: Basic Usage
description: Basic usage of the league/config library
---

# Basic Usage

There are three steps to using this library:

- [Defining the configuration schema](/1.0/schemas/)
  - The overall structure of the configuration
  - Required options, validation constraints, and default values
- [Applying user-provided values](/1.0/setting-values/) against the schema
- [Reading the validated options](/1.0/reading-values/) and acting on them

## Example

Simply define your configuration schema, set the values, and then fetch them where needed:

```php
use League\Config\Configuration;
use Nette\Schema\Expect;

// Define your configuration schema
$config = new Configuration([
    'database' => Expect::structure([
        'driver' => Expect::anyOf('mysql', 'postgresql', 'sqlite')->required(),
        'host' => Expect::string()->default('localhost'),
        'port' => Expect::int()->min(1)->max(65535),
        'ssl' => Expect::bool(),
        'database' => Expect::string()->required(),
        'username' => Expect::string()->required(),
        'password' => Expect::string()->nullable(),
    ]),
    'logging' => Expect::structure([
        'enabled' => Expect::bool()->default($_ENV['DEBUG'] == true),
        'file' => Expect::string()->deprecated("use logging.path instead"),
        'path' => Expect::string()->assert(function ($path) { return \is_writeable($path); })->required(),
    ]),
]);

// Set the values somewhere
$userProvidedValues = [
    'database' => [
        'driver' => 'mysql',
        'port' => 3306,
        'host' => 'localhost',
        'database' => 'myapp',
        'username' => 'myappdotcom',
        'password' => 'hunter2',
    ],
    'logging' => [
        'path' => '/var/log/myapp.log',
    ],
];

// Merge those values into your configuration schema:
$config->merge($userProvidedValues);

// Read the values and do stuff with them
if ($config->get('logging.enabled')) {
    file_put_contents($config->get('logging.path'), 'Connecting to the database on ' . $config->get('database.host'));
}
```

Filemanager

Name Type Size Permission Actions
basic-usage.md File 2.09 KB 0644
changelog.md File 618 B 0644
index.md File 1.41 KB 0644
installation.md File 693 B 0644
lazy-processing.md File 2.14 KB 0644
mutability.md File 1.04 KB 0644
philosophy.md File 2.53 KB 0644
reading-values.md File 2.44 KB 0644
schemas.md File 4.17 KB 0644
setting-values.md File 2.19 KB 0644