PHP code example of smithingdev / laravel-vault

1. Go to this page and download the library: Download smithingdev/laravel-vault library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smithingdev / laravel-vault example snippets


use Smithingdev\Vault\Facades\Vault;

Vault::read('myapp/database');

use Smithingdev\Vault\VaultService;

class RotateSecrets
{
    public function __construct(private VaultService $vault) {}

    public function handle(): void
    {
        $this->vault->read('myapp/database');
    }
}

Vault::write('myapp/database', ['username' => 'app', 'password' => '...']);

$secrets = Vault::read('myapp/database');              // array|null
$old     = Vault::read('myapp/database', version: 2);

$keys = Vault::list('myapp');                          // array<string>

Vault::delete('myapp/database');                 // soft-delete latest version (recoverable)
Vault::destroy('myapp/database');                // permanent: all versions + metadata

use Smithingdev\Vault\Exceptions\VaultException;

try {
    $secrets = Vault::read('myapp/database');
} catch (VaultException $e) {
    // log, alert, fall back, ...
}

$prod = app(VaultService::class)->withNamespace('acme/prod');
$prod->write('myapp/database', $secrets);

$prod = app(VaultService::class)->withKvNamespace('acme/prod/kv-secrets');
$prod->write('myapp/database', $secrets);

Vault::namespace();    // current Enterprise namespace (header mode), e.g. "acme/dev"
Vault::kvNamespace();  // current KV namespace path (path mode), e.g. "acme/dev/kv-secrets"