【发布时间】:2019-08-19 10:55:03
【问题描述】:
我正在编写一个 symfony 控制台命令,它可以通过使用“php bin/console app:mycommand”来执行(symfony 文档:https://symfony.com/doc/current/console.html#creating-a-command)。
在我的 MyCommand 类中,我需要使用 getDoctrine 函数,所以我必须扩展控制器,但我看不到如何做到这一点。有什么想法吗?
目前我在 CLI 上收到以下错误:尝试调用类“App\Command\MyCommand”的名为“getDoctrine”的未定义方法。
<?php
// src/Command/MyCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MyCommand extends Command
{
// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:mycommand';
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Not working, producing mentioned error
$em = $this->getDoctrine()->getManager();
}
}
?>
【问题讨论】:
-
您为什么希望能够调用该函数?是什么让您认为您“必须扩展控制器”?