我有一个简单的 Symfony 4 控制台应用程序。
在 execute()
方法我需要打印应用程序根路径。
这个$this->getContainer()->get('kernel')->getRootDir()
- 不工作。
这个$this->get('kernel')->getRootDir();
- 不工作。
这个$this->get('parameter_bag')->get('kernel.project_dir');
- 不工作。
如何获取应用程序根路径?
最佳答案
我建议不要将容器注入(inject)命令,而是在服务定义中显式传递参数
src/命令
class YourCommand extends Command
{
private $path;
public function __construct(string $path)
{
$this->path = $path;
}
public function (InputInterface $input, OutputInterface $output)
{
echo $this->path;
}
}
配置/服务.yaml
services:
# ...
App\Command\YourCommand:
arguments:
$path: '%kernel.project_dir%'
关于php - 如何在 Symfony 4 控制台命令中获取应用程序根路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53497815/