slavcodev / yii2-yii-bridge
此包已被废弃且不再维护。未建议替代包。
1.1.x 版本与 2.0 版本的 Yii 之间桥接
v0.1
2014-08-30 02:43 UTC
Requires
- yiisoft/yii: >=1.1.16
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2022-02-01 12:45:03 UTC
README
警告:此扩展是为早期 Yii 2.0 版本制作的,可能不适用于当前版本。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist slavcodev/yii2-yii-bridge "*"
或者
"slavcodev/yii2-yii-bridge": "*"
将以下内容添加到您的 composer.json 的 require 部分。
使用方法
要使用此桥接,请编辑您的入口脚本(index.php
)
// Define project directories. $rootPath = dirname(dirname(__DIR__)); /** * Include composer autoloader * @var \Composer\Autoload\ClassLoader $loader Registered composer autoloader. */ $loader = require($rootPath . '/vendor/autoload.php'); // Load Yii 1 base class define('YII1_PATH', $rootPath . '/vendor/yiisoft/yii/framework'); // Load Yii 2 base class define('YII2_PATH', $rootPath . '/vendor/yiisoft/yii2'); // Override base class until v1.1.17 will released. // You need version of file after this commit // @link https://github.com/yiisoft/yii/commit/e08e47ce3ce503b5eb92f9f9bd14d36ac07e1ae9 // define('YII1_BASE_PATH', $rootPath . '/vendor/slavcodev/yii2-yii-bridge/YiiBase.php'); // Include Yii bridge class file. require($rootPath . '/vendor/slavcodev/yii2-yii-bridge/Yii.php'); // Create old application, but NOT run it! $gaffer = Yii::createWebApplication($v1AppConfig); // Create new application and run. Have fun! $application = new yii\web\Application($v2AppConfig); $application->run();
现在您可以在您的 Yii2 应用程序中使用旧模型,例如
// Access new application echo Yii::$app->user->id; // Access old application echo Yii::app()->user->id; // Use Yii2 grid, data provider with Yii1 ActiveRecords echo yii\grid\GridView::widget([ 'dataProvider' => new \yii\data\ArrayDataProvider([ 'allModels' => User::model()->with('address.country')->findAll(), ]), 'columns' => [ ['attribute' => 'id'], ['attribute' => 'name'], ['attribute' => 'address.country.name'], ] ]); // Save Yii1 AR in Yii2 controller public function actionCreate() { $user = new User(); if ($data = Yii::app()->request->getPost(CHtml::modelName($user))) { $model->attributes = $data; if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('create', [ 'model' => $user, ]); }