An open API service indexing awesome lists of open source software.

https://github.com/ahmadasjad/yii1plusyii2

Migrate from yii1 to yii2
https://github.com/ahmadasjad/yii1plusyii2

php yii1 yii1-and-yii2 yii2 yii2-and-yii1

Last synced: about 1 month ago
JSON representation

Migrate from yii1 to yii2

Awesome Lists containing this project

README

        

# yii1 Plus Yii2

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

### Steps to follow

>**: 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` and `Yii2Yii1.php` file from `ahmadasjad/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
```php
'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` before `render('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:
```php
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:
```php
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' => [...],
...
];
```

### Tips
- Remove unnecessary codes generated by GII crud like `index.php`, `_view.php` files from view and `actionIndex()` method from controller

### Problems and solutions

### Conversion
- [zii.widgets.jui.CJuiDatePicker -> yii\jui\DatePicker\DatePicker](docs/datePicker.md)
- [CStarRating -> \yii2mod\rating\StarRating](docs/starRating.md)
- [zii.widgets.jui.CJuiAutoComplete -> \yii\jui\AutoComplete](docs/autocomplete.md)
- [zii.widgets.CDetailView -> \yii\widgets\DetailView](docs/detailView.md)

### Links helped during the development
* https://github.com/yiiext/yii2-yii-bridge
* https://github.com/neam/yii-yii2-bridge
* https://www.yiiframework.com/doc/guide/2.0/en/tutorial-yii-integration#using-both-yii2-yii1
* https://www.yiiframework.com/doc/guide/2.0/en/intro-upgrade-from-v1