سؤال

Is there any way to get the running path in a Symfony Console application? For example (assuming php interpreter in PATH):

cd /tmp
php /home/user/myapplication/app/console.php mycommand

Should return /tmp as console.php was launched from /tmp.

هل كانت مفيدة؟

المحلول

getcwd() will do what you need. You can execute app/console from any directory, and PHP will know which one it is.

I used the following example to verify this.

<?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());
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top