$npx -y skills add OthmanAdi/openui-forge --skill openui-forge-phpOpenUI generative UI with a PHP (Laravel 13.x) backend. Forwards OpenAI's SSE stream verbatim via response()->stream().
| 1 | # OpenUI Forge — PHP |
| 2 | |
| 3 | Build generative UI apps with a React frontend + Laravel backend. Streams the OpenAI API's native SSE response straight through `response()->stream()`. |
| 4 | |
| 5 | ## Activation Triggers |
| 6 | |
| 7 | - "openui php", "openui laravel", "openui php backend" |
| 8 | - "generative ui php", "laravel streaming ui backend" |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | - Node.js >= 22 (24 LTS recommended) + React >= 18.3.1 (19+ recommended) (frontend) |
| 13 | - PHP >= 8.3 + Laravel 13.x (backend; Laravel 13 requires PHP 8.3 minimum and supports 8.3 through 8.5) |
| 14 | - Composer; `guzzlehttp/guzzle` ships with Laravel and backs the `Http` facade (no extra dependency to call OpenAI) |
| 15 | - `OPENAI_API_KEY` environment variable set |
| 16 | |
| 17 | ## Quick Start |
| 18 | |
| 19 | 1. Create the React frontend and install OpenUI deps: |
| 20 | ```bash |
| 21 | npm install @openuidev/react-ui @openuidev/react-headless @openuidev/react-lang lucide-react zod |
| 22 | ``` |
| 23 | 2. Generate the system prompt: |
| 24 | ```bash |
| 25 | npx @openuidev/cli generate ./src/lib/library.ts --out backend/storage/app/system-prompt.txt |
| 26 | ``` |
| 27 | 3. Create the Laravel backend (see Full Code below). On a fresh app, enable API routes once with `php artisan install:api`. |
| 28 | 4. Run: `php artisan serve` on `:8000`, frontend on `:3000` |
| 29 | |
| 30 | ## Full Code |
| 31 | |
| 32 | ### Backend: `composer.json` (require block) |
| 33 | |
| 34 | ```json |
| 35 | { |
| 36 | "require": { |
| 37 | "php": "^8.3", |
| 38 | "laravel/framework": "^13.0" |
| 39 | } |
| 40 | } |
| 41 | ``` |
| 42 | |
| 43 | > Laravel bundles `guzzlehttp/guzzle`, so the `Http` facade can call OpenAI with no extra package. The OpenAI SSE passthrough below needs nothing beyond the framework. |
| 44 | |
| 45 | ### Backend: `routes/api.php` |
| 46 | |
| 47 | ```php |
| 48 | <?php |
| 49 | |
| 50 | use App\Http\Controllers\ChatController; |
| 51 | use Illuminate\Support\Facades\Route; |
| 52 | |
| 53 | // `php artisan install:api` creates this file and prefixes it with /api, |
| 54 | // so this route is reachable at POST /api/chat. |
| 55 | Route::post('/chat', [ChatController::class, 'chat']); |
| 56 | ``` |
| 57 | |
| 58 | ### Backend: `app/Http/Controllers/ChatController.php` |
| 59 | |
| 60 | ```php |
| 61 | <?php |
| 62 | |
| 63 | namespace App\Http\Controllers; |
| 64 | |
| 65 | use Illuminate\Http\Request; |
| 66 | use Illuminate\Support\Facades\Http; |
| 67 | use Symfony\Component\HttpFoundation\StreamedResponse; |
| 68 | |
| 69 | class ChatController extends Controller |
| 70 | { |
| 71 | // Loaded once per worker process, then reused across requests. |
| 72 | private static ?string $systemPrompt = null; |
| 73 | |
| 74 | private function systemPrompt(): string |
| 75 | { |
| 76 | if (self::$systemPrompt === null) { |
| 77 | $path = storage_path('app/system-prompt.txt'); |
| 78 | if (! is_file($path)) { |
| 79 | abort(500, 'system-prompt.txt not found at ' . $path); |
| 80 | } |
| 81 | self::$systemPrompt = (string) file_get_contents($path); |
| 82 | } |
| 83 | |
| 84 | return self::$systemPrompt; |
| 85 | } |
| 86 | |
| 87 | public function chat(Request $request): StreamedResponse |
| 88 | { |
| 89 | // Read config(), never env(), in a controller: after `php artisan |
| 90 | // config:cache` (standard in production) env() returns null outside |
| 91 | // config files. See the config/services.php block below. |
| 92 | $apiKey = config('services.openai.key'); |
| 93 | if (! $apiKey) { |
| 94 | abort(500, 'OPENAI_API_KEY not set'); |
| 95 | } |
| 96 | |
| 97 | $incoming = $request->validate([ |
| 98 | 'messages' => 'required|array|min:1', |
| 99 | 'messages.*.role' => 'required|string', |
| 100 | 'messages.*.content' => 'required|string', |
| 101 | ])['messages']; |
| 102 | |
| 103 | // Prepend the system prompt; never trust a client-sent system message. |
| 104 | $messages = array_merge( |
| 105 | [['role' => 'system', 'content' => $this->systemPrompt()]], |
| 106 | array_map( |
| 107 | fn (array $m) => ['role' => $m['role'], 'content' => $m['content']], |
| 108 | $incoming, |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | $baseUrl = rtrim(config('services.openai.base_url'), '/'); |
| 113 | $model = config('services.openai.model'); |
| 114 | |
| 115 | // stream => true returns the Guzzle PSR-7 response with its body still |
| 116 | // on the wire, so we read it chunk-by-chunk instead of buffering the |
| 117 | // whole completion in memory. |
| 118 | $upstream = Http::withToken($apiKey) |
| 119 | ->withOptions(['stream' => true]) |
| 120 | ->acceptJson() |
| 121 | ->post("{$baseUrl}/chat/completions", [ |
| 122 | 'model' => $model, |
| 123 | 'stream' => true, |
| 124 | 'messages' => $messages, |
| 125 | ]); |
| 126 | |
| 127 | if ($upstream->failed()) { |
| 128 | abort($upstream->status(), 'OpenAI request failed: ' . $upstream->body()); |
| 129 | } |
| 130 | |
| 131 | $body = $upstream->toPsrResponse()->getBody(); |
| 132 | |
| 133 | return response()->stream(function () use ($body): void { |
| 134 | // Forward OpenAI's SSE bytes verbatim. OpenAI already emits |
| 135 | // `data: {chunk}\n\n` frames plus a final `data: [DONE]`, which is |
| 136 | // exactly what openAIAdapter() parses, so no re-framing is needed. |
| 137 | while (! $body->eof()) { |
| 138 | $chunk = $body->read(8192); |