我认为问题是路径。无论如何,你应该考虑不使用Process
来调用Symfony命令。控制台组件允许调用命令,例如在控制器中。从文档
例子:
// src/Controller/SpoolController.php
namespace App\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;
use Symfony\Component\HttpKernel\KernelInterface;
class SpoolController extends Controller
{
public function sendSpoolAction($messages = 10, KernelInterface $kernel)
{
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => 'swiftmailer:spool:send',
// (optional) define the value of command arguments
'fooArgument' => 'barValue',
// (optional) pass options to the command
'--message-limit' => $messages,
));
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$application->run($input, $output);
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return new Response($content);
}
}
使用这种方式你确定代码将总是工作。当PHP处于安全模式时(exec
等被关闭)Process
组件是无用的。此外,您不需要关心路径和其他事情,否则您所称的“手动”命令就是命令。
你可以阅读更多关于从控制器here调用命令。
“不起作用”是什么意思?有没有错误信息? –