我试图为getMime方法的参数获取KernelInterface对象,但我不知道在哪里查找以及如何找到它。我需要此方法来运行命令。
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
class MimeController extends AbstractController
{
public function getMime($video, KernelInterface $kernel)
{
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'get-video-mime',
'fooArgument' => $video,
]);
$output = new BufferedOutput();
try {
$application->run($input, $output);
} catch (\Exception $e) {
}
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return $content;
}
}发布于 2020-05-23 01:52:58
为了完整性起见,kernel服务是公共的,可以从容器中获取(您必须将'kernel' => KernelInterface::class添加到AbstractController的订阅服务中)
但是,这不是解决问题的好方法:
Application对象。Mime组件,它公开了一个服务,您可以在控制器中使用该服务来计算文件的mime类型。Process组件。它是专门设计用来执行shell脚本并让您访问结果的。不要将此作为一种捷径,这是一种糟糕的做法,可能会导致非常糟糕的问题(难以调试)。https://stackoverflow.com/questions/61924198
复制相似问题