在同一个项目中使用 Yii1 和 Yii2 [英] Using Yii1 and Yii2 in the same project

查看:32
本文介绍了在同一个项目中使用 Yii1 和 Yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Yii1.x 中有一个项目,现在我在相同的项目中使用 Yii2

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

项目层次是这样的

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

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

现在我想知道是否可以在project1/protected/controllers

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

我怎样才能完成这个任务?

How can I achieve this task?

谢谢

推荐答案

我不建议这样做,相反,最好在 Yii2 中完全重写旧应用程序.

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) 修改入口脚本:

// 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) Yii 类的组合:

$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;

Yii 类的使用:

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

这篇关于在同一个项目中使用 Yii1 和 Yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆