PHP code example of baconfy / prompt

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

    

baconfy / prompt example snippets


$prompt = prompt('welcome', ['name' => 'John']);

$prompt->content;           // rendered string
$prompt->metadata['model']; // 'claude-opus-4-5'
(string) $prompt;           // same as ->content (implements Stringable)

$prompt = prompt('welcome', ['name' => 'John']);

$response = $anthropic->messages()->create([
    'model'       => $prompt->metadata['model'],
    'temperature' => $prompt->metadata['temperature'],
    'messages'    => [['role' => 'user', 'content' => (string) $prompt]],
]);

prompt('auth.login');               // resources/prompts/auth/login.md
prompt('emails.welcome.subject');   // resources/prompts/emails/welcome/subject.md

use Baconfy\Prompt\Models\Prompt;

Prompt::create([
    'name'    => 'welcome',
    'content' => <<<'MD'
        ---
        model: claude-opus-4-5
        

// config/prompt.php
'drivers' => [
    'system' => [
        'driver' => 'file',
        'folder' => resource_path('prompts/system'),
    ],
    'user' => [
        'driver' => 'database',
        'table'  => 'user_prompts',
    ],
],

Prompt::driver('system')->find('welcome');
Prompt::driver('user')->find('welcome');

prompt(string $name, array $data = []): RenderedPrompt

use Baconfy\Prompt\Facades\Prompt;

Prompt::get('welcome', ['name' => 'John']);   // RenderedPrompt
Prompt::source('welcome');                     // ParsedFrontMatter|null
Prompt::driver('database');                    // Driver instance (defaults to active)

$prompt->content;    // rendered string
$prompt->metadata;   // array<string, mixed>
(string) $prompt;    // same as ->content

$source = Prompt::source('welcome');
$source->metadata['model'];   // 'claude-opus-4-5'
$source->content;             // raw template, with Blade tags untouched

use Baconfy\Prompt\Models\Prompt as PromptModel;

PromptModel::create([
    'name'    => 'welcome',
    'content' => <<<'MD'
        ---
        model: claude-opus-4-5
        ---
        Hello {{ $name }}!
        MD,
]);

PromptModel::where('name', 'welcome')->update(['content' => 'Hi {{ $name }}!']);

use Baconfy\Prompt\Facades\Prompt;
use Baconfy\Prompt\RenderedPrompt;

Prompt::fake([
    'welcome' => 'Hello stub!',
    'auth.login' => new RenderedPrompt('Stub login.', ['model' => 'gpt-4']),
]);

// code under test
prompt('welcome', ['name' => 'whatever']);

Prompt::assertCalled('welcome');
Prompt::assertNotCalled('checkout');

use Baconfy\Prompt\Models\Prompt;

Prompt::factory()->create();
Prompt::factory()->count(5)->create();
Prompt::factory()->create(['name' => 'welcome', 'content' => 'Hi!']);

return [
    'default' => env('PROMPTS_DRIVER', 'file'),

    'drivers' => [
        'file' => [
            'driver' => 'file',
            'folder' => env('PROMPTS_FOLDER', resource_path('prompts')),
        ],
        'database' => [
            'driver'     => 'database',
            'connection' => env('PROMPTS_CONNECTION'),
            'table'      => env('PROMPTS_TABLE', 'prompts'),
        ],
    ],
];

use Baconfy\Prompt\Exceptions\PromptNotFoundException;
use Baconfy\Prompt\Exceptions\MissingRequiredVariablesException;

// AppServiceProvider::boot() — closure style (like Horizon::auth)
use Baconfy\Prompt\Panel;

Panel::auth(fn ($user = null) => $user?->email === '[email protected]');

// or via a regular Gate
use Illuminate\Support\Facades\Gate;

Gate::define('viewPrompts', fn ($user = null) => $user?->isAdmin());

// config/prompt.php
'panel' => [
    'enabled'    => env('PROMPTS_PANEL_ENABLED', true),
    'path'       => env('PROMPTS_PANEL_PATH', '_prompts'),
    'gate'       => 'viewPrompts',
    'middleware' => ['web'],
],
bash
php artisan vendor:publish --tag=prompt-views