Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web-controllers of modules are falsely auto-detected by the help command on the console #2821

Closed
schmunk42 opened this issue Mar 19, 2014 · 9 comments
Assignees
Milestone

Comments

@schmunk42
Copy link
Contributor

If you register a module for the console (eg. gii) it's web-controllers are detected as console commands, somewhere around here: https://github.com/yiisoft/yii2/blob/master/framework/console/controllers/HelpController.php#L159

I wasn't sure if Reflection would be better and check instanceof console\Controller.

@cebe
Copy link
Member

cebe commented Mar 19, 2014

Yeah seen this before, you need to separate console and web controllers. A convention could be to ha ve commands dir like in basic app. Will work on this.

@cebe cebe self-assigned this Mar 19, 2014
@qiangxue qiangxue added this to the 2.0 RC milestone Apr 16, 2014
@cebe
Copy link
Member

cebe commented Jun 9, 2014

A simple fix for this would be to change Module::init() to the following:

    public function init()
    {
        if ($this->controllerNamespace === null) {
            $class = get_class($this);
            if (($pos = strrpos($class, '\\')) !== false) {
                $this->controllerNamespace = substr($class, 0, $pos) . '\\'
                    . (Yii::$app instanceof \yii\console\Application ? 'commands' : 'controllers');
            }
        }
    }

But then it would not be possible to configure different namespaces for different environments anymore. When using console and web controllers in a module one is bound to the default values.
@qiangxue any suggestions on this?

@qiangxue
Copy link
Member

qiangxue commented Jun 9, 2014

I think the help command needs to load the controller classes and detect their parent classes to determine whether they are console commands.

@cebe
Copy link
Member

cebe commented Jun 9, 2014

then there has to be a check in Module::createControllerByID() to not allow loading console controllers in web app and web controllers in console.

@qiangxue
Copy link
Member

qiangxue commented Jun 9, 2014

Yes, I think so, but this probably should be done in Application. It's not a very trivial change though.

@cebe
Copy link
Member

cebe commented Jun 15, 2014

Still the best way of separation would be to have different directories so it is clear from the location where the controller belongs to. The same issue is also with modules that can be used in different applications for example backend and frontend.

@qiangxue
Copy link
Member

Agree. It doesn't make sense to mix two types of controllers in the same directory.

@schmunk42
Copy link
Contributor Author

I'd also like to see two different directories, looks like to most intuitive solution to me.
Should Module get an commandNamespace property then?

@qiangxue
Copy link
Member

Fixed the help command issue.

Regarding whether Module should get commandNamespace, my answer is no. A module should be either used for Web or console, but not both. This is just like you can't use a console application for Web, or vice versa.

So for the gii console support, we should introduce a ConsoleModule (which can inherit from the gii Web module) to manage the console commands.

tvdavid pushed a commit to tvdavid/yii2 that referenced this issue Jul 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants