symfony 4 : How to get "/public" from RootDir
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;
}
}
Related videos on Youtube
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, 2021Comments
-
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/';
-
Clijsters about 6 yearsHello 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.
-
Tomas Votruba over 5 yearsPossible duplicate of How do I read from parameters.yml in a controller in symfony2?
-
-
Carlos Delgado over 5 yearsWorth 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 over 5 yearsworth 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 about 5 yearsMoving forward to Symfony 4 and beyond, this is closer to the real answer. $this->get('kernel') is deprecated in 4.2.
-
Aries over 4 yearsWhy did you downvote this? This is the only one that worked for me?!
-
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 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 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.