如何在 Symfony 4 控制台命令中获取应用程序根路径
How to get application root path in Symfony 4 Console Command
我有一个简单的 Symfony 4 控制台应用程序。
在 execute()
方法中,我需要打印应用程序根路径。
这个 $this->getContainer()->get('kernel')->getRootDir()
- 不工作。
这 $this->get('kernel')->getRootDir();
- 不起作用。
这 $this->get('parameter_bag')->get('kernel.project_dir');
- 不起作用。
如何获取应用程序根路径?
我建议不要将容器注入命令,而是在服务定义中显式传递参数
src/Command
class YourCommand extends Command
{
private $path;
public function __construct(string $path)
{
$this->path = $path;
}
public function (InputInterface $input, OutputInterface $output)
{
echo $this->path;
}
}
config/services.yaml
services:
# ...
App\Command\YourCommand:
arguments:
$path: '%kernel.project_dir%'
我有一个简单的 Symfony 4 控制台应用程序。
在 execute()
方法中,我需要打印应用程序根路径。
这个 $this->getContainer()->get('kernel')->getRootDir()
- 不工作。
这 $this->get('kernel')->getRootDir();
- 不起作用。
这 $this->get('parameter_bag')->get('kernel.project_dir');
- 不起作用。
如何获取应用程序根路径?
我建议不要将容器注入命令,而是在服务定义中显式传递参数
src/Command
class YourCommand extends Command
{
private $path;
public function __construct(string $path)
{
$this->path = $path;
}
public function (InputInterface $input, OutputInterface $output)
{
echo $this->path;
}
}
config/services.yaml
services:
# ...
App\Command\YourCommand:
arguments:
$path: '%kernel.project_dir%'