PHP code example of gilads-otiannoh254 / inertia-protocol

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

    

gilads-otiannoh254 / inertia-protocol example snippets


use Inertia\Protocol\Inertia;
use Inertia\Protocol\Support\NativeRequest;

// 1. Build an Inertia response
$response = Inertia::render('Users/Index', [
    'users' => fn() => UserModel::paginate(),
    'siteName' => Inertia::always('My App'),
    'analytics' => Inertia::defer(fn() => Analytics::get(), group: 'analytics'),
]);

// 2. Evaluate against incoming request
$decision = $response->toDecision(NativeRequest::fromGlobals());

if ($decision->isJson()) {
    // Send HTTP JSON response (status: 200, headers: decision->headers, body: decision->content)
    http_response_code($decision->statusCode);
    foreach ($decision->headers as $name => $value) {
        header("{$name}: {$value}");
    }
    echo json_encode($decision->content);
} else {
    // Render full HTML root template with embedded page object
    echo view('app', ['page' => $decision->pageObject->toArray()]);
}

// External URL visit
$decision = Inertia::location('https://external-payment.com/checkout');

// URL Fragment Redirect
$decision = Inertia::redirectWithFragment('https://my-app.com/posts#comment-42');

$decision = Inertia::precognitionSuccess();
// Emits 204 No Content with Precognition-Success headers