// 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); } }
展示帶色彩的輸出php
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)); } }