Symfony 4, get the root path of the project from a custom class (not a controller class)

后端 未结 4 1215
孤街浪徒
孤街浪徒 2020-12-30 03:56

In the src/Utils directory, I created a custom class Foo for various things. I\'m looking for a way to get the absolute root path of the symfony 4 project <

4条回答
  •  离开以前
    2020-12-30 04:49

    Without Kernel injection

    config/services.yaml

    services:
        _defaults:
            autowire: true
            autoconfigure: true
            bind:
                $projectDir: '%kernel.project_dir%'
    

    ....

    class Foo
    {
        private $projectDir;
    
        public function __construct(string $projectDir)
        {
            $this->projectDir = $projectDir;
        }
    }
    

提交回复
热议问题