首页 > 解决方案 > 插件内的 CakePHP 3.7 Shell 命令无法执行

问题描述

namespace Admin\Shell;
use Cake\Console\Shell;
class AdminAlertShell extends Shell{
  ...
  ...
}

这里'Admin'是插件,所以我在插件文件夹结构中创建了这个文件。文件路径:/plugins/Admin/src/Shell/AdminAlertShell.php

试图在 CLI 中运行它

bin/cake admin_alert

但是抛出异常

例外:未知命令cake admin_alert。运行cake --help以获取有效命令的列表。在 [localpath/vendor/cakephp/cakephp/src/Console/CommandRunner.php,第 346 行]

它正在工作。但我不知道这是怎么回事。我已将 cakephp 3.5 升级到 3.7。但是,我不确定这是否会导致问题。

标签: cakephp-3.7

解决方案


我刚刚在我的项目中找到了问题的根源。

在我的插件里面有一个文件:src/Plugin.php

在这个类里面有以下几行代码:

/**
 * @inheritDoc
 */
public function console(CommandCollection $commands): CommandCollection
{
    // Add console commands here.
    return $commands;
}

这可能是通过烘焙生成的。

我看到没有打电话给父母。在父路径中添加。

将此方法更改为如下所示:

/**
 * @inheritDoc
 */
public function console(CommandCollection $commands): CommandCollection
{
    // Add console commands here.
    $commands = parent::console($commands);
    return $commands;
}

现在调用父级并将路径添加到命令集合中。

作为旁注,我还看到中间件没有调用它的父级。

认为修复那个也是一个好主意。

作为替代方案,您可以清除该类并应使用所有默认值。

希望这可以节省我花在解决这个问题上的时间。


推荐阅读