Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Yii1 and Yii2 in the same project

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

like image 207
Mike Ross Avatar asked Sep 28 '15 05:09

Mike Ross


People also ask

How do I convert Yii1 to Yii2?

As you know, there's no way to upgrade from Yii1 to Yii2 without re-writing code, since the Yii framework has been completely rewritten from the ground up for Yii 2.0.

Is Yii2 a good framework?

The Benefits of Yii2 Development Like Symphony and Laravel, Yii2 is an open-source framework for both back-end and front-end programming. A lot of explanatory documentation helps to sort out many issues and the framework is also supported by a community of significant size. Yii also supports Object-Relational Mapping.

What is difference between Yii and Yii2?

Conceptually Yii1 and Yii2 are quite similar, however Yii2 runs on newer PHP versions and utilizes namespaces, traits etc. Yii2 has also support for dependency injection through $container singleton available after initializing the framework.

What is Yii2 framework?

Yii is a generic Web programming framework, meaning that it can be used for developing all kinds of Web applications using PHP.


1 Answers

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'
like image 200
arogachev Avatar answered Sep 28 '22 07:09

arogachev