tebe/yii2-inertia
Composer 安装命令:
composer require tebe/yii2-inertia
包简介
The Yii 2 adapter for 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.
To use Inertia you need both a server-side adapter as well as a client-side adapter.
Be sure to follow the installation instructions for the client-side framework you use.
Demo
Installation
Composer require dependency:
composer require tebe/yii2-inertia
Edit config/web.php:
<?php return [ ... 'bootstrap' => ['inertia'] ... 'components' => [ 'inertia' => [ 'class' => 'tebe\inertia\Inertia' 'rootElementId' => 'app' // optional per https://inertiajs.com/client-side-setup#defining-a-root-element ], 'request' => [ 'cookieValidationKey' => '<cookie_validation_key>', 'enableCsrfValidation' => false, 'enableCsrfCookie' => false, 'parsers' => [ 'application/json' => 'yii\web\JsonParser', ] ] ] ... ];
Note that CSRF protection is disabled.
Controllers
Your backend controllers should extend from tebe\inertia\web\Controller.
Instead of the render method within your actions you should use the inertia method.
<?php namespace app\controllers; use tebe\inertia\web\Controller; class DemoController extends Controller { public function actionIndex() { $params = [ 'data' => [], 'links' => [] ]; return $this->inertia('demo/index', $params); } }
Routing
Use your Yii server-side routes as usual. There is nothing special.
CSRF protection
Axios is the HTTP library that Inertia uses under the hood. Yii's CSRF protection is not optimized for Axios.
The easiest way to implement CSRF protection is using the customized tebe\inertia\web\Request component.
Simply edit config/web.php file:
<?php return [ 'components' => [ 'request' => [ 'class' => 'tebe\inertia\web\Request', 'cookieValidationKey' => '<cookie_validation_key>' ] ] ];
Please see the security page for more details.
Shared data
The Yii 2 adapter provides a way to preassign shared data for each request. This is typically done outside of your controllers. Shared data will be automatically merged with the page props provided in your controller.
Massive assignment of shared data:
<?php $shared = [ 'user' => [ 'id' => $this->getUser()->id, 'first_name' => $this->getUser()->firstName, 'last_name' => $this->getUser()->lastName, ], 'flash' => $this->getFlashMessages(), 'errors' => $this->getFormErrors(), 'filters' => $this->getGridFilters() ]; Yii::$app->get('inertia')->share($shared);
Shared data for one key:
<?php $user = [ 'id' => $this->getUser()->id, 'first_name' => $this->getUser()->firstName, 'last_name' => $this->getUser()->lastName ]; Yii::$app->get('inertia')->share('user', $user);
A good strategy when using shared data outside of your controllers is to implement an action filter.
<?php namespace app\components; use yii\base\ActionFilter; class SharedDataFilter extends ActionFilter { public function beforeAction() { $shared = [ 'user' => $this->getUser(), 'flash' => $this->getFlashMessages(), 'errors' => $this->getFormErrors() ]; Yii::$app->get('inertia')->share($shared); return parent::beforeAction($action); } }
And then use this action filter as a behaviour in your controller.
<?php namespace app\controllers; use app\components\SharedDataFilter; use tebe\inertia\web\Controller; class ContactController extends Controller { public function behaviors() { return [ [ 'class' => SharedDataFilter::class ] ]; } public function actionIndex() { // your action code } }
Please see the shared data page for more details.
Client-side setup
To use Inertia you need to setup your client-side framework. This primarily includes updating your main JavaScript file to boot the Inertia app. Please see the client-side setup page for more details.
More about Inertia
Visit inertiajs.com to learn more.
tebe/yii2-inertia 适用场景与选型建议
tebe/yii2-inertia 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112.92k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2020 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tebe/yii2-inertia 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tebe/yii2-inertia 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 112.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 72
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-18