Symfony:从控制器内部调用自定义命令

问题描述 投票:0回答:1

我找不到这个问题的答案。

不确定是否与此问题相关,但我的项目在 Ubuntu 中的 Limactl VM 上运行。

我试图从控制器内部调用自定义命令,但我无法理解收到的异常,因为它指出应用程序命名空间中没有定义命令,但我可以从控制台调用它们.

命令 bin/console app:execute {slug} {command} 在终端中工作正常,但当我从控制器调用它时却不行。实际上,当我尝试运行 debug:twig 时,它会做同样的事情。

难道是使用的工作目录没有指向正确的位置?如果是这样,我该怎么做,然后 Limactl VM 对我如何解决这个问题很重要吗?

方法是这样的:

use Symfony\Bundle\FrameworkBundle\Console\Application;

    protected function executeCommand(String $command,String $slug, KernelInterface $kernel)
    {
      $application = new Application($kernel);
      $application->setAutoExit(false);

      $input = new ArrayInput([
        'command' => "bin/console app:execute",
        'cmd' => $command,
        'slug' => $slug,
      ]);

      $output = new BufferedOutput();
      $application->run($input, $output);

      $content = $output->fetch();

      return $this->render('command_results/index.html.twig', [
        'output' => $content
      ]);
    }

这是我的 dev.log 中的一行:

[2023-08-25T15:14:27.095303+00:00] console.CRITICAL: Error thrown while running command "'bin/console app:execute'". Message: "There are no commands defined in the "bin/console app" namespace." {"exception":"[object] (Symfony\\Component\\Console\\Exception\\NamespaceNotFoundException(code: 0): There are no commands defined in the \"bin/console app\" namespace. at /Users/me/dev/runner/runner/vendor/symfony/console/Application.php:622)","command":"'bin/console app:execute'","message":"There are no commands defined in the \"bin/console app\" namespace."} {"uid":"c6e8985","file":"/Users//dev/runner/runner/vendor/symfony/console/EventListener/ErrorListener.php","line":48,"class":"Symfony\\Component\\Console\\EventListener\\ErrorListener","callType":"->","function":"onConsoleError"}

感谢您给我的任何帮助或建议!如果之前确实有人问过这个问题,我很抱歉,我只是超级盲目。

symfony symfony4
1个回答
0
投票

尝试以通过类实例而不是字符串传递命令的方式更改此部分:

$input = new ArrayInput([
    'command' => new AppExecuteCommand(),
    'cmd' => $command,
    'slug' => $slug,
]);

<AppExecuteCommand>
应替换为您要运行的命令的真实路径。

© www.soinside.com 2019 - 2024. All rights reserved.