php – 如何获取Symfony控制台应用程序的运行路径?
作者:互联网
有没有办法在Symfony Console应用程序中获取运行路径?例如(假设PATH中的php解释器):
cd /tmp
php /home/user/myapplication/app/console.php mycommand
应该从/ tmp启动console.php返回/ tmp.
解决方法:
getcwd()会做你需要的.您可以从任何目录执行app / console,PHP将知道它是哪一个.
我使用以下示例来验证这一点.
<?php
namespace Acme\DemoBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DemoCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('demo:cwd')
->setDescription('Get Current Working Directory')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(getcwd());
}
}
标签:php,symfony,symfony-2-1 来源: https://codeday.me/bug/20190723/1508548.html