PHP code example of yii2-extensions / inertia

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

    

yii2-extensions / inertia example snippets


return [
    'bootstrap' => [\yii\inertia\Bootstrap::class],
];

use yii\inertia\Inertia;
use yii\web\Controller;
use yii\web\Response;

final class SiteController extends Controller
{
    public function actionIndex(): Response
    {
        return Inertia::render(
            'Dashboard',
            ['stats' => ['visits' => 42]],
        );
    }
}

use yii\inertia\web\Controller;
use yii\web\Response;

final class SiteController extends Controller
{
    public function actionIndex(): Response
    {
        return $this->inertia(
            'Dashboard',
            [
                'stats' => ['visits' => 42],
            ]
        );
    }
}

use yii\inertia\web\ResponseRendererInterface;
use yii\web\{Controller, Response};

final class SiteController extends Controller
{
    public function __construct(
        $id,
        $module,
        private readonly ResponseRendererInterface $renderer,
        $config = [],
    ) {
        parent::__construct($id, $module, $config);
    }

    public function actionIndex(): Response|string
    {
        return $this->renderer->render('Dashboard', ['stats' => ['visits' => 42]]);
    }
}

use yii\inertia\Manager;

return [
    'bootstrap' => [\yii\inertia\Bootstrap::class],
    'components' => [
        'inertia' => [
            'class' => Manager::class,
            'id' => 'app',
            'rootView' => '@app/views/layouts/inertia.php',
            'version' => static function (): string {
                $path = dirname(__DIR__) . '/public/build/manifest.json';

                return is_file($path) ? (string) filemtime($path) : '';
            },
            'shared' => ['app.name' => static fn(): string => Yii::$app->name],
        ],
    ],
];

'components' => [
    'inertiaVite' => [
        'class' => \yii\inertia\Vite::class,
        'devMode' => YII_ENV === 'dev',
        // ...
    ],
],

use yii\inertia\Inertia;

return Inertia::render(
    'Dashboard',
    [
        'stats' => $stats,                                              // regular prop
        'users' => Inertia::defer(fn () => User::find()->all()),        // loaded after render
        'activity' => Inertia::optional(fn () => $user->getActivity()), // only on partial reload
        'auth' => Inertia::always(fn () => ['user' => $identity]),      // always 

if (!$model->validate()) {
    Yii::$app->session->setFlash('errors', $model->getErrors());

    return $this->redirect(['create']);
}

Yii::$app->session->setFlash('success', 'Record created.');

return $this->redirect(['view', 'id' => $model->id]);

'request' => [
    'class' => \yii\inertia\web\Request::class,
    'cookieValidationKey' => 'your-secret-key',
],