Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get project or root dir in Symfony 5

I use Symfony 5 and Solarium. I want to get the project dir (or root dir) in my Controller (extends from AbstractController).

I saw some anwers with kernel, creating new services, etc..

But, isn't it just a function that call the project dir without creating some custom services etc..?

Thanks !

like image 221
Brandys Avatar asked Jun 10 '20 10:06

Brandys


1 Answers

You don't need any service for this. You can get from container in your controller.

$this->getParameter('kernel.project_dir');

Other way is bind it on yaml.

    _defaults:
        bind:
            # pass this value to any $projectDir argument for any service
            # that's created in this file (including controller arguments)
            string $projectDir: '%kernel.project_dir%'

So you can inject each service and controller as $projectDir.

public class YourController {
   public function index(string $projectDir){
        // Your code
   }
}
public class YourService {
   public function __construct(string $projectDir){
        // Your code
   }
}

https://symfony.com/doc/current/configuration.html#accessing-configuration-parameters

like image 95
tolgakaragol Avatar answered Sep 27 '22 19:09

tolgakaragol