Get project directory on command symfony 3.4

Using symfony 3.4 In controller i can get project directory using this method :


I would like to get the project directory on command symfony (3.4) , what is the best practise to do it ?


Pretty sure this question has been asked many time but I'm too lazy to search for it. Plus Symfony has moved away from pulling parameters/services from the container to injecting them. So I am not sure if previous answers are up to date.

It is pretty easy.

namespace AppBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;

class ProjectDirCommand extends Command
    protected static $defaultName = 'app:project-dir';

    private $projectDir;

    public function __construct($projectDir)
        $this->projectDir = $projectDir;
    protected function execute(InputInterface $input, OutputInterface $output)
        $output->writeln('Project Dir ' . $this->projectDir);

Because your project directory is a string, autowire will not know what value to inject. You can either explicitly define your command as a service and manually inject the value or you can use the bind capability:

# services.yml or services.yaml
        autowire: true
        autoconfigure: true
        public: false
            $projectDir: '%kernel.project_dir%' # matches on constructor argument name

How to retrieve the root dir of the project and other container , After upgrading a project from Symfony 3 to Symfony 4, i discovered how This forced me to research a way to retrieve the project root directory using the registers your services as commands, event subscribers, etc. Create a Symfony Project. Once the Symfony installer is installed, we can proceed to create our firstSymfony project. symfony new my_symfony_project 3.4. This command is going to create an empty Symfony project(my_symfony_project) into your directory. This command is also going to check for any other dependency and compatibility with your current PHP package.

Well, I would say, inject the %kernel.project_dir% or %kernel.root_dir% parameters directly in your command. No need to make your command dependent on the Kernel service.

And by the way you can also make your Command extends Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand which is an abstract class. So you can access the container within your command by just calling getContainer method.

But, I would not advice you to this actually. Better take benefit of autowiring or configure your service in a "yaml" way.

Console Commands (Symfony Docs), Console Commands: The Symfony framework provides lots of commands 3.4 4.4 5.0 5.2 master Console Input; Getting Services from the Service Container; Command use Symfony\Bundle\FrameworkBundle\Console\Application; use  In Symfony 4, the kernel class has been moved to the src/ directory, so the previous expressions won't break. However, given that most of the times getRootDir () is used to get the project root directory, in Symfony 3.3 we've decided to add a new method to the Kernel class called getProjectDir () which will give you exactly that.

You can inject KernelInterface to the command just adding it to the constructor parameters and then get the project directory with $kernel->getProjectDir():


namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FooCommand extends Command
    protected $projectDir;

    public function __construct(KernelInterface $kernel)
        $this->projectDir = $kernel->getProjectDir();

    protected function execute(InputInterface $input, OutputInterface $output): int
        echo "This is the project directory: " . $this->projectDir;

New in Symfony 3.3: A simpler way to get the project root directory , Symfony 3.3 defines a new Kernel::getProjectDir() to replace Kernel::getRootDir() as the way to find the root project directory. Symfony Local Web Server¶. You can run Symfony applications with any web server (Apache, nginx, the internal PHP web server, etc.). However, Symfony provides its own web server to make you more productive while developing your applications.

How to Create a Console Command (The Symfony CookBook), Consider upgrading your projects to Symfony 5.1. in the Service Container; Getting Services from the Service Container; Invoking other Commands; Testing Commands To make the console commands available automatically with Symfony, create a Command directory inside your bundle and create a PHP file suffixed  Under the hood, this Symfony binary command executes the needed Composer command to create a new Symfony application based on the current stable version. Use the Default Directory Structure ¶ Unless your project follows a development practice that imposes a certain directory structure, follow the default Symfony directory structure.

How to Generate URLs from the Console (Symfony 3.4 Docs), This means that if you generate absolute URLs within a console command you'll URL generator and the assets) if Symfony is not running in the root directory. Setting up an Existing Symfony Project¶ In addition to creating new Symfony projects, you will also work on projects already created by other developers. In that case, you only need to get the project code and install the dependencies with Composer. Assuming your team uses Git, setup your project with the following commands:

The Finder Component (Symfony Docs), 3.4 4.4 5.0 5.2 master The Finder component finds files and directories based on different criteria (name This means that you need to create a new instance if you do not want to get mixed results. always add a trailing slash when looking for in the FTP root dir $finder->in(''); // you can also look for in a​  Console Commands: The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console comp

  • Same approach, but it is better to inject Kernel into your Command directly and call getProjectDir() from it
  • Come on @Flying, you know better than that.
  • @Cerad true, my bad, thank you
  • As ContainerAwareCommand is deprecated since Sf 4.2, one should no longer use it
  • Is it also for 3.4? Because he is not using 4.2
  • It's not, but it does not make sense to write code today that obviously needs to be changed on upgrading Symfony. By not using deprecated features in new code, you have less work later
  • Well, yes but in this case be more precise when you speak because some people can understand that it is deprecated also for 3.4 and actually it is not
  • I've written very clearly that it is deprecated since 4.2