定制 slavcodev/yii2-yii-bridge 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

slavcodev/yii2-yii-bridge

最新稳定版本:v0.1

Composer 安装命令:

composer require slavcodev/yii2-yii-bridge

包简介

Yii bridge between v1.1.x and v2.0

关键字:

README 文档

README

Warning: The extension was made for early Yii 2.0 releases and likely does not work with current version.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist slavcodev/yii2-yii-bridge "*"

or add

"slavcodev/yii2-yii-bridge": "*"

to the require section of your composer.json.

Usage

To use this bridge, edit your entry script (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();

Now you can use old models in your Yii2 application, i.e

// 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,
    ]);
}

统计信息

  • 总下载量: 37.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 12
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-02-28