We can achieve this in two steps
- Using base controller from Yii1
- Using base controller from Yii2
For every step, I have created two repository. We'll use it accordingly
**: Mandatory
*: Somehow mandatory
(no star): optional
-
Make your project composer compatible and use yii1 with composer dependency**
Add classmap in autoloader, and put all you resources folder in its value
composer dump-autoload
Resolve all warning for Ambiguous class resolution
-
Install my both repo using composer**
composer require ahmadasjad/yii1plushyii2 composer require ahmadasjad/yii2plusyii1
-
Create two branch of git one with yii1Controller and another with yii2Controller
-
Create a yii2 config directory inside site/protected/config/
yii2
* -
Copy
index.php
andYii2Yii1.php
file fromahmadasjad/yii2plusyii1
repo to your site directory or customize index.php according to your files structure and config * -
Create a custom Controller and extend it from
\ahmadasjad\yii1PlusYii2\Controller
**Now, in every controller extend from your customized controller. This will help you to change only inside your base custom controller in future when you use the controller from Yii2
-
Replace View class*
Add this to your Yii2 config
'components' => [ 'view' => ['class' => \ahmadasjad\yii1PlusYii2\View::class], ],
-
Register Yii2 assets in yii1*
Add
Yii::$app->getView()->registerYii2Assets();
inside the layout or view file. This will create a bridge in publishing Yii2 assets in yii1. -
add
return
beforerender('yourView', ['model' => $model])
call inside every controller. -
Make your controllers namespaced *: https://www.yiiframework.com/doc/guide/1.1/en/basics.namespace#namespaced-controllers
-
Add following function inside the config while bootstrapping the app:
function() { //Set modelName converter for Yii1 models class in html form CHtml::setModelNameConverter(function ($model){ $className = get_class($model); $reflector = new ReflectionClass($className); return $reflector->getShortName(); }); }
Now your config should look like:
return [ ..., 'aliases' => [...], 'bootstrap' => [ ..., function() { //Set modelName converter for Yii1 models class in html form CHtml::setModelNameConverter(function ($model){ $className = get_class($model); $reflector = new ReflectionClass($className); return $reflector->getShortName(); }); }, ..., ], 'components' => [...], ... ];
- Remove unnecessary codes generated by GII crud like
index.php
,_view.php
files from view andactionIndex()
method from controller