symfony 4 : How to get "/public" from RootDir
Asked Answered
B

7

27

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/';
Beulabeulah answered 2/2, 2018 at 14:54 Comment(2)
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?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.Rhubarb
Possible duplicate of How do I read from parameters.yml in a controller in symfony2?Topnotch
E
31

You can use either

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

Or the parameter %kernel.project_dir%

$container->getParameter('kernel.project_dir') . '/public/';
Endocentric answered 2/2, 2018 at 15:2 Comment(2)
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\AbstractControllerScoria
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.Crowberry
S
36

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.

Shallot answered 7/8, 2019 at 11:56 Comment(0)
E
31

You can use either

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

Or the parameter %kernel.project_dir%

$container->getParameter('kernel.project_dir') . '/public/';
Endocentric answered 2/2, 2018 at 15:2 Comment(2)
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\AbstractControllerScoria
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.Crowberry
D
9

In Controller (also with inheriting AbstractController):

$projectDir = $this->getParameter('kernel.project_dir');
Drury answered 21/3, 2019 at 9:2 Comment(3)
Why did you downvote this? This is the only one that worked for me?!Willard
@Drury because we're talking about Symfony 4 where your controllers should be defined as services thus, not inheriting from AbstractController.Raver
@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)Drury
H
7

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/'
Hardesty answered 1/2, 2019 at 12:54 Comment(2)
Moving forward to Symfony 4 and beyond, this is closer to the real answer. $this->get('kernel') is deprecated in 4.2.Devalue
+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.Gunstock
Q
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;
    }
}
Qualification answered 10/1, 2020 at 19:24 Comment(0)
S
2

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()),
        // ...
    ];
}
Santana answered 8/4, 2021 at 16:8 Comment(0)
E
1

All above answers seems valid, but I think it's simplier if you configure it as parameter in services.yaml

If you need to use it in serveral services, you can bind it like this:

# services.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true
    bind:
      $publicDir: "%kernel.project_dir%/public"
# src/Services/MyService.php

class MyService
{
    public function __construct(
         private string $publicDir,
    ) {
    }

    // …
}

This way, this is configured at one place only, and if later you decide to change /public to something else, you will have to change it only in .yaml file.

If you don't need the root directory but a subdirectory, it might be better to define the final target path: This way you will be more flexible if you need later to move only that directory, like $imageDir or $imagePath (depends if you will use the full directory or only the public path).

Note also the default public path is defined in composer.json file, in the extra.public-dir key

Ephram answered 24/11, 2021 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.