Git Product home page Git Product logo

yii2-yii-bridge's Introduction

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

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

yii2-yii-bridge's People

Contributors

slavcodev avatar acorncom avatar icevita avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.