PHP code example of crenspire / yii2-inertia

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

    

crenspire / yii2-inertia example snippets


'view' => [
    'renderers' => [
        'inertia' => \Crenspire\Yii2Inertia\ViewRenderer::class,
    ],
],

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inertia.js App</title>
    <script type="module" crossorigin src="/dist/assets/index.js"></script>
    <link rel="stylesheet" crossorigin href="/dist/assets/index.css">
</head>
<body>
    <div id="app" data-page="<?= htmlspecialchars(json_encode($page), ENT_QUOTES, 'UTF-8') 

use Crenspire\Yii2Inertia\Inertia;

class HomeController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return Inertia::render('Home', [
            'title' => 'Welcome',
            'user' => Yii::$app->user->identity,
        ]);
    }
}

return Inertia::render('Dashboard', [
    'users' => User::find()->all(),
]);

// Single key-value
Inertia::share('appName', 'My App');

// Multiple values
Inertia::share([
    'user' => Yii::$app->user->identity,
    'flash' => Yii::$app->session->getFlash('message'),
]);

// Using closures
Inertia::share('timestamp', function () {
    return time();
});

// String version
Inertia::version('1.0.0');

// Callback version
Inertia::version(function () {
    return filemtime(Yii::getAlias('@webroot/dist/manifest.json'));
});

// Get current version
$version = Inertia::version();

return Inertia::location('/dashboard');

return inertia('Home', ['title' => 'Welcome']);

// Client sends: X-Inertia-Partial-Component: Dashboard
// Client sends: X-Inertia-Partial-Data: users,stats

// Only 'users' and 'stats' props will be returned (plus shared props)
return Inertia::render('Dashboard', [
    'users' => $users,
    'stats' => $stats,
    'other' => $other, // This will be excluded
]);

public function actionStore()
{
    // ... save data
    
    // For Inertia requests, returns 409 with X-Inertia-Location header
    // For regular requests, returns 302 redirect
    return Inertia::location('/dashboard');
}

// Automatically uses dist/manifest.json mtime if it exists
$version = Inertia::version();

// String version
Inertia::version('1.0.0');

// Callback version (evaluated on each request)
Inertia::version(function () {
    return filemtime(Yii::getAlias('@webroot/dist/manifest.json'));
});

Inertia::setRootView('@app/views/custom-inertia.php');

// In config/bootstrap.php or a base controller
use Crenspire\Yii2Inertia\Inertia;

// Share user data
Inertia::share('user', function () {
    return Yii::$app->user->identity;
});

// Share flash messages
Inertia::share('flash', function () {
    return [
        'success' => Yii::$app->session->getFlash('success'),
        'error' => Yii::$app->session->getFlash('error'),
    ];
});