如何获取Symfony控制台应用程序的运行路径?

gre*_*emo 4 php symfony symfony-2.1

有没有办法在Symfony控制台应用程序中获取运行路径?例如(假设php解释器PATH):

cd /tmp
php /home/user/myapplication/app/console.php mycommand
Run Code Online (Sandbox Code Playgroud)

应返回/tmpconsole.php是推出/tmp.

Ada*_*ney 11

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());
    }
}
Run Code Online (Sandbox Code Playgroud)