deesoft/yii2-inertia
Composer 安装命令:
composer require deesoft/yii2-inertia
包简介
Yii2 inertia js
关键字:
README 文档
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.
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, ]); }
Configuration
Add configuration to Application $params config.
'components' => [ ... ], 'params' => [ 'inertia' => [ 'tag' => 'div', // default div 'id' => 'app', // default app 'view_file' => '@app/views/app.php', // default @dee/inertia/views/app.php 'serializer' => ['class' => dee\inertia\Serializer::class], //default 'encrypt_history' => true, // default false ], 'inertia.shared' => [ 'user' => function(){ if(!Yii::$app->user->isGuest){ return ['id' => Yii::$app->user->id]; } } ], ],
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])
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-24