symfony 4 : How to get "/public" from RootDir

55,626

Solution 1

You can use either

$webPath = $this->get('kernel')->getProjectDir() . '/public/'; 

Or the parameter %kernel.project_dir%

$container->getParameter('kernel.project_dir') . '/public/';

Solution 2

It is a bad practice to inject the whole container, just to access parameters, if you are not in a controller. Just auto wire the ParameterBagInterface like this,

protected $parameterBag;

public function __construct(ParameterBagInterface $parameterBag)
{
    $this->parameterBag = $parameterBag;

}

and then access your parameter like this (in this case the project directory),

$this->parameterBag->get('kernel.project_dir');

Hope someone will find this helpful.

Cheers.

Solution 3

In Controller (also with inheriting AbstractController):

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

Solution 4

In config/services.yaml:

parameters:
    webDir: '%env(DOCUMENT_ROOT)%'

In your controller:

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

...

public function yourFunction(Parameterbag $parameterBag)
{
   $webPath = $parameterBag->get('webDir')
}

If you need to access a directory within public, change the last line to the following:

$webPath = $parameterBag->get('webDir') . '/your/path/from/the/public/dir/'

Solution 5

You can inject KernelInterface to the service or whatever and then get the project directory with $kernel->getProjectDir():

<?php

namespace App\Service;

use Symfony\Component\HttpKernel\KernelInterface;

class Foo
{
    protected $projectDir;

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

    public function showProjectDir()
    {
        echo "This is the project directory: " . $this->projectDir;
    }
}
Share:
55,626

Related videos on Youtube

Mouna Ben Hmida
Author by

Mouna Ben Hmida

software developer . Now , I am working on a Symfony4 application for resume parsing . meanwhile, I write stories about life experience on Medium I write about knowledge and technologies

Updated on December 22, 2021

Comments

  • Mouna Ben Hmida
    Mouna Ben Hmida over 2 years

    I have an image under the public folder.
    How can I get my image directory in symfony4 ?
    In symfony 3, it's equivalent is :

    $webPath = $this->get('kernel')->getRootDir() . '/../web/';
    
  • Carlos Delgado
    Carlos Delgado over 5 years
    Worth to mention that the controller needs to extend the Symfony\Bundle\FrameworkBundle\Controller\Controller class, not the abstract one that you find on most examples Symfony\Bundle\FrameworkBundle\Controller\AbstractController
  • Dragos
    Dragos over 5 years
    worth to mention @CarlosDelgado that the controller class you mentioned is being deprecated in Symfony 4.2 @deprecated since Symfony 4.2, use {@see AbstractController} instead.
  • tlorens
    tlorens about 5 years
    Moving forward to Symfony 4 and beyond, this is closer to the real answer. $this->get('kernel') is deprecated in 4.2.
  • Aries
    Aries over 4 years
    Why did you downvote this? This is the only one that worked for me?!
  • DevAntoine
    DevAntoine over 4 years
    @godrar because we're talking about Symfony 4 where your controllers should be defined as services thus, not inheriting from AbstractController.
  • godrar
    godrar over 4 years
    @vdavid Yes, it worked in old versions. In the new version you do not have access to the kernel in this way (inject "container" is bad practice)
  • Joe Devine
    Joe Devine about 2 years
    +1 for being the only answer that uses an environment variable instead of hard coding the public directory. Although, I would recommend binding this as an argument for the service in its config instead of injecting the whole parameter bag.