deesoft/yii2-inertia

Yii2 inertia js

Maintainers

👁 mdmunir

Package info

github.com/deesoft/yii2-inertia

Type:yii2-extension

pkg:composer/deesoft/yii2-inertia

Statistics

Installs: 18

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

dev-master / 1.0.x-dev 2026-04-29 13:30 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 34ca143507bedfefbb663c2b81e81ee41252578f

  • mdmunir <misbahuldmunir.woop@gmail.com>

extensionyii2inertia

This package is auto-updated.

Last update: 2026-05-29 13:37:48 UTC


README

This is the Yii 2 server-side adapter for Inertia.

With Inertia you are able to build single-page apps using classic server-side routing and controllers, without building an API.

See client-side setup for client instalation.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist deesoft/yii2-inertia "*"

or add

"deesoft/yii2-inertia": "*"

to the require section of your composer.json file.

Initialization

# php yii inertia/init

Usage

Once the extension is installed, simply use it in your controller :

 public function actionIndex()
 {
 $query = User::find();
 $request = Yii::$app->getRequest();
 $query->andFilterWhere([
 'id' => $request->get('id'),
 'active' => $request->get('active'),
 ]);

 $query->andFilterWhere(['ilike', 'username', $request->get('username')])
 ->andFilterWhere(['ilike', 'email', $request->get('email')])
 ->andFilterWhere(['ilike', 'phone', $request->get('phone')]);

 if ($q = $request->get('q')) {
 $query->andWhere([
 'OR',
 ['ilike', 'username', $q],
 ['ilike', 'email', $q],
 ['ilike', 'phone', $q],
 ]);
 }

 $sortAttrs = [
 'id',
 'username',
 'email',
 'phone',
 ];
 $dataProvider = new ActiveDataProvider([
 'query' => $query,
 'sort' => [
 'attributes' => $sortAttrs,
 ]
 ]);
 return Inertia::render('user/index', [
 'data' => $dataProvider
 ]);
 }

 public function actionCreate()
 {
 $model = new User();

 if ($this->request->isPost) {
 if ($model->load($this->request->post(), '') && $model->save()) {
 return $this->redirect(['view', 'id' => $model->id]);
 }
 }

 return Inertia::render('user/create', [
 'model' => $model,
 ]); 
 }

// deferred prop
 return Inertia::render('user/index', [
 'data' => Inertia::defer(fn() => $dataProvider),
 ]);
// other prop
 return Inertia::render('user/index', [
 'data' => Inertia::scroll(User::find()->where(['active' => true]))->merge(),
 'warehouses' => Inertia::once(fn() => Warehouse::find()->all()),
 'prop1' => Inertia::optional(fn() => Branch::find()->all()),
 'prop2' => Inertia::merge(fn() => Branch::find()->all())->prepend(),
 'prop3' => function(){
 return Branch::find()->all();
 }
 ]);

// shared props
 Inertia::shared([
 'user' => fn() => Yii::$app->user->identity,
 ]);

Configuration

Add configuration to Application $params config.

 'components' => [
 ...
 ],
 'params' => [
 'inertia' => [
 'tag' => 'div', // default div
 'id' => 'app', // default app
 'register_vite_asset' => true, // set false when you want handle your vite asset
 'view_file' => '@app/views/app.php', // default @dee/inertia/views/app.php
 'encrypt_history' => true, // default false
 ],
 'inertia.shared' => [
 'user' => function(){
 if(!Yii::$app->user->isGuest){
 return ['id' => Yii::$app->user->id];
 }
 }
 ],
 ],

Vite Asset Bundle

If you want to handle vite asset with your own, set Yii::$app->params['inertia.register_vite_asset'] to false. To use ViteAsset bundle, ensure in vite.config.js value of build.manifest is true.

// vite.config.js

 ...
 build: {
 rollupOptions: {
 input: [
 'client/app.js',
 ],
 },
 manifest: true,
 outDir: 'client/dist',
 },
// config/web.php

 'components' => [
 ...
 'assetManager' => [
 'bundles' => [
 \dee\inertia\ViteAsset::class => [
 'bootstrap' => 'client/app.js', // same as build.rollupOptions.input
 'sourcePath' => '@client/dist', // same as build.outDir
 ]
 ]
 ]
 ]

Create Url

Use function yiiUrl to generate url from route. It's equivalent with yii\helpers\Url::to().

const {yiiUrl} = window;

const url = yiiUrl('product/view', {id: row.id}); // equivalent Url::to(['/product/view', id' => $row->id])