Using Yii1 and Yii2 in the same project

两盒软妹~` 提交于 2019-12-09 12:04:45

问题


I had a project in Yii1.x and now I am using Yii2 for the same projects

Project hierarchy is something like this

Project1(yii1)/all yii files +  project2(yii2)

project2(yii2)/frontend + /common + /backend

Now I want to know if is it possible to use project2/common/models in project1/protected/controllers

How can I achieve this task?

Thank you


回答1:


I wouldn't recommend doing it, instead it's better to completely rewrite old application in Yii2.

But in case of partial migrating, please read this paragraph in Special Topics Section in Official Guide.

Here are some important code snippets from there:

1) Modification of entry script:

// include the customized Yii class described below
require(__DIR__ . '/../components/Yii.php');

// configuration for Yii 2 application
$yii2Config = require(__DIR__ . '/../config/yii2/web.php');
new yii\web\Application($yii2Config); // Do NOT call run()

// configuration for Yii 1 application
$yii1Config = require(__DIR__ . '/../config/yii1/main.php');
Yii::createWebApplication($yii1Config)->run();

2) Combination of Yii classes:

$yii2path = '/path/to/yii2';
require($yii2path . '/BaseYii.php'); // Yii 2.x

$yii1path = '/path/to/yii1';
require($yii1path . '/YiiBase.php'); // Yii 1.x

class Yii extends \yii\BaseYii
{
    // copy-paste the code from YiiBase (1.x) here
}

Yii::$classMap = include($yii2path . '/classes.php');
// register Yii 2 autoloader via Yii 1
Yii::registerAutoloader(['Yii', 'autoload']);
// create the dependency injection container
Yii::$container = new yii\di\Container;

Usage of Yii class:

echo get_class(Yii::app()); // outputs 'CWebApplication'
echo get_class(Yii::$app);  // outputs 'yii\web\Application'


来源:https://stackoverflow.com/questions/32816134/using-yii1-and-yii2-in-the-same-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!