// src/AppBundle/Controller/SpoolController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
class SpoolController extends Controller
{
public function sendSpoolAction($messages = 10)
{
$kernel = $this->get('kernel');
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => 'swiftmailer:spool:send',
'--message-limit' => $messages,
));
// 如果你不想输出使用NullOutput()
$output = new BufferedOutput();
$application->run($input, $output);
// 获取命令返回的内容
$content = $output->fetch();
// 如果你是使用NullOutput;使用new Response("");
return new Response($content);
}
}
展现带色彩的输出
- 安装组件
composer require sensiolabs/ansi-to-html
- 使用
// src/AppBundle/Controller/SpoolController.php
namespace AppBundle\Controller;
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Response;
// ...
class SpoolController extends Controller
{
public function sendSpoolAction($messages = 10)
{
// ...
$output = new BufferedOutput(
OutputInterface::VERBOSITY_NORMAL,
true // true for decorated
);
// ...
// return the output
$converter = new AnsiToHtmlConverter();
$content = $output->fetch();
return new Response($converter->convert($content));
}
}