yiisoft / yii2-debug

Debug Extension for Yii 2
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
201 stars 149 forks source link

Exception in routes panel if a module has dependencies #448

Closed Eseperio closed 3 years ago

Eseperio commented 3 years ago

What steps will reproduce the problem?

An application with a module which has yii2-usuario. AppModule->SubModule->Yii2-usuarioModule

What's expected?

See all available routes in routes panel

What do you get instead?

Argument 2 passed to Da\User\Controller\SettingsController::__construct() must be an instance of Da\User\Module, instance of namespace\of\submodule given

Additional info

Happens since 2.1.14

Q A
Yii version 2.0.39.3
PHP version 7.3.24
Operating system MacOS
yii-bot commented 3 years ago

Thanks for posting in our issue tracker. In order to properly assist you, we need additional information:

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

bizley commented 3 years ago

I'll try to reproduce it.

bizley commented 3 years ago

So far I cannot reproduce it with simple example.

Screenshot from 2020-12-06 13-06-37

'modules' => [
        'test1' => [
            'class' => 'app\modules\test1\Module',
            'modules' => [
                'test2' => 'app\modules\test1\modules\test2\Module',
            ],
        ]
    ]
namespace app\modules\test1;
class Module extends \yii\base\Module {}
namespace app\modules\test1\modules\test2;
class Module extends \yii\base\Module {}
namespace app\modules\test1\controllers;
class TestController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return true;
    }
}
namespace app\modules\test1\modules\test2\controllers;
class TestController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return true;
    }
}

Debug panel works fine: Screenshot from 2020-12-06 13-13-37

The problem might be with special config for yii2-usuario. I'll try to check now again with this package exactly.

bizley commented 3 years ago

Still cannot reproduce.

'modules' => [
        'test1' => [
            'class' => 'app\modules\test1\Module',
            'modules' => [
                'test2' => 'app\modules\test1\modules\test2\Module',
                'user' => 'Da\User\Module',
            ],
        ],
    ]

Screenshot from 2020-12-06 13-55-06

Eseperio commented 3 years ago

Ok, some information missing.

Settings controller has custom constructor with dependencies defined, which is why expects to be Da\User\Module (see image below).

I think this is not a problem from yii2-usuario library or our project config because it only happens since Yii2-debug 2.1.14, previous versions worked fine (tested 2.1.0, 2.1.10,2.1.11,2.1.12,2.1.13,2.1.14).

image

bizley commented 3 years ago

Ok, this is why I asked how this is configured in the Slack thread. I'm asking again then.

Eseperio commented 3 years ago

Ok, i managed to reproduce the issue. I´ve created a sample project with all the conditions. Clone it and run composer install.

The issue happens if one controller in first level module is extending from a controller of another module which has custom constructor dependencies.

In the example we have example\emulated\frontend\controllers\OtherController which extends from Da\User\Controller\SettingsController, and that is what is causing the problem.

bizley commented 3 years ago

Thanks. I'm looking into it.

bizley commented 3 years ago

Indeed debug panel is showing that error but it happens on first/other route. The controller handling it is:

namespace example\emulated\frontend\controllers;
use Da\User\Controller\SettingsController as BaseSettingsController;
class OtherController extends BaseSettingsController {}

and there is no surprise it throws the exception since there is different module calling it. At the same time route first/user/setting (which is handled by the original controller Da\User\Controller\SettingsController) works perfectly fine in the Debug panel. Additionally when you visit /first/other address in the browser the same exception is thrown.

So far two things are possible:

  1. This controller is misconfigured and Debug is just revealing it faster.
  2. I'm doing something wrong with this app.

@Eseperio please take a look.

Eseperio commented 3 years ago

Direct access to the controller must throw an exception, because Module::createController is always passing $id and $module to the constructor, and as you said, it is being called from another module. The controller exists to be used with controllerMap property, to override default one. But seems like Yii2-debug is creating an instance of this, and that is why it is getting the same exception.

bizley commented 3 years ago

So in controllerMap you are passing the module there? Could you instruct here how do you configure it? I could not find anything in your app.

Eseperio commented 3 years ago

I´ve updated the project. Pull changes to see how settings controller is overrided.

bizley commented 3 years ago

I cannot see the rendered view for first/user/settings but I can see it is run (so constructor is resolved) so indeed this is a bug in Debug panel. Although the fix might be tricky. We'll see.

bizley commented 3 years ago

Hmm, wait. Debug stops on first/settings so this is the same situation as before and that configuration changes nothing.

bizley commented 3 years ago

The problem looks more like a case when the custom controller serving as a replacement for the original one is causing the Debug crash because it can be run directly but is misconfigured to do it properly.

The simplest solution for this is to keep this class out of the path that is configured as controllerNamespace.

@Eseperio can you confirm this?

Eseperio commented 3 years ago

Moving outside controllerNamespace stops the problem.

bizley commented 3 years ago

Ok, closing the issue then. Thanks for all the help.

Eseperio commented 3 years ago

Maybe Yii2 controller map guide needs an update indicating that overrided controllers should not be within controllerNamespace.

bizley commented 3 years ago

I'm not sure, there might be a case where you want to run it directly.