Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Yii2 controllers with Yii1 controllers

I have a rather large Yii1 application that I want to migrate slowly to Yii2. I would like to do this gradually.

I have followed the instructions in the Yii2 and Yii1 integration manual.

And my application is running fine.

I have also tried the yii2-yii-bridge trick and it also seems to work after some tweaks.

My question is, now that I know I have both versions of Yii in my application, how can I start adding Yii2 style controllers?

If I create a simple Yii2 style controller, like this:

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;


class SecondController extends Controller {

    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['index'],
                'rules' => [
                    // allow authenticated users
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    // everything else is denied
                ],
            ],
        ];
    }

    public function actionIndex()
    {
        Yii::trace("(!!!) SecondController::index called!!!");
        return "SecondController::index!!";
    }

} // class

I can't access it using the URL scheme that I am used to, so http://local.url/second/index doesn't seem to be found.

However if I create a Yii1 style the way I have always done, it resolves just fine:

class ThirdController extends Controller {

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index'),
                'users' => array('*'),
            ),
        );
    }


    public function actionIndex() {
        Yii::trace("ThirdController::index called!!!");
        return "ThirdController::index!!";
    }

}

Seems that the default url mapping of <controller>/<action> cannot deal with namespaced controllers is there a way around this?

like image 739
zilog Avatar asked Nov 08 '22 14:11

zilog


1 Answers

Seems that the default url mapping of / cannot deal with namespaced controllers is there a way around this?

To support namespaced controllers you need to use CWebModule::$controllerNamespace for module controllers or CWebApplication::$controllerNamespace for global application controllers. Then all controllers in given scope needs to be namespaced. If you want to use namepsaces only in some controllers, you need to use CWebApplication::$controllerMap or CWebModule::$controllerMap and configure controllers classes for specified routes.

However running Yii 2.0 controller in Yii 1.1 application most likely will not work at all. If you want to move only some part of application to Yii 2, I suggest to configure and run it as a separate application and route to proper application using rewrite rules. For example, if you move user module to Yii 2 you may use this in your .htaccess:

RewriteEngine on

# Yii 2 rules
RewriteRule ^user index-yii2.php [L]

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php [L]

In index.php file you should have bootstrapped your application in Yii 1.1, and in index-yii2.php you have Yii 2 application.

like image 105
rob006 Avatar answered Dec 25 '22 07:12

rob006