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/ */
// 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'));
});
// 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'),
];
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.