22

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/';
goto
  • 7,020
  • 10
  • 40
  • 50
Mouna Ben Hmida
  • 316
  • 1
  • 4
  • 15
  • 1
    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Clijsters Feb 02 '18 at 15:10
  • Possible duplicate of [How do I read from parameters.yml in a controller in symfony2?](https://stackoverflow.com/questions/13901256/how-do-i-read-from-parameters-yml-in-a-controller-in-symfony2) – Tomas Votruba Dec 21 '18 at 16:59

6 Answers6

30

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.

Anjana Silva
  • 5,175
  • 3
  • 40
  • 45
27

You can use either

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

Or the parameter %kernel.project_dir%

$container->getParameter('kernel.project_dir') . '/public/';
goto
  • 7,020
  • 10
  • 40
  • 50
  • 5
    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 – Carlos Delgado Aug 17 '18 at 18:30
  • 7
    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.` – Dragos Dec 29 '18 at 17:04
8

In Controller (also with inheriting AbstractController):

$projectDir = $this->getParameter('kernel.project_dir');
godrar
  • 91
  • 1
  • 2
  • Why did you downvote this? This is the only one that worked for me?! – Aries VII Aug 07 '19 at 12:01
  • @godrar because we're talking about Symfony 4 where your controllers should be defined as services thus, not inheriting from AbstractController. – DevAntoine Oct 01 '19 at 15:20
  • @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) – godrar Oct 03 '19 at 10:58
5
parameters:
    webDir: '%env(DOCUMENT_ROOT)%'
Nexen
  • 94
  • 1
  • 3
  • Moving forward to Symfony 4 and beyond, this is closer to the real answer. $this->get('kernel') is deprecated in 4.2. – tlorens Apr 10 '19 at 12:16
4

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;
    }
}
Manolo
  • 16,729
  • 16
  • 67
  • 115
0

Starting from Symfony 4.3 we can generate absolute (and relative) URLs for a given path by using the two methods getAbsoluteUrl() and getRelativePath() of the new Symfony\Component\HttpFoundation\UrlHelper class.

New in Symfony 4.3: URL Helper

public function someControllerAction(UrlHelper $urlHelper)
{
    // ...
    return [
        'avatar' => $urlHelper->getAbsoluteUrl($user->avatar()->path()),
        // ...
    ];
}
Az.Youness
  • 1,362
  • 1
  • 13
  • 28