PHP code example of secretstack / laravel-vault

1. Go to this page and download the library: Download secretstack/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/ */

    

secretstack / laravel-vault example snippets


$app->afterBootstrapping(
    \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
    fn ($app) => \Vaultenv\Vault\Bootstrap\VaultBootstrap::inject($app)
);

'key_map' => [
    'DB_PASSWORD' => 'database.connections.mysql.password',
],

config('database.connections.mysql.password'); // resolves the Vault value
env('STRIPE_SECRET');                           // resolves the Vault value

use Vaultenv\Vault\Facades\Vault;

Vault::get('STRIPE_SECRET');            // string|null
Vault::get('FEATURE_FLAG', 'default');  // with a default
Vault::all();                           // array<string, string>
Vault::refresh();                       // reload in-process (dev / cache-warm; NOT a prod hot-reload)

public function __construct(private \Vaultenv\Vault\Secrets\SecretStore $secrets) {}
// $this->secrets->get('STRIPE_SECRET');

use Vaultenv\Vault\Exceptions\SecretProviderException;

try {
    $value = Vault::get('STRIPE_SECRET');
} catch (SecretProviderException $e) {
    // log/handle — the message never contains a secret value
}

namespace Vaultenv\Vault\Contracts;

interface SecretProvider
{
    /**
     * @return array<string, string>
     * @throws \Vaultenv\Vault\Exceptions\SecretProviderException
     */
    public function fetch(): array;
}

$this->app->bind(\Vaultenv\Vault\Contracts\SecretProvider::class, MyProvider::class);
bash
php artisan vault:install
bash
php artisan vendor:publish --tag=vault-config
sh
set -e

php artisan vault:check --gate     # halts a bad rollout; old pods keep serving
php artisan config:cache           # freezes config WITH the injected Vault values
php artisan route:cache
php artisan view:cache

# start your server (php-fpm / octane / supervisord / …)