Reputation: 624
Symfony2
. In a controller
, I would like to define a path to the Resources/public/pdf
directory of a bundle of mine.
If I use __DIR__
, I get the path to the Controller directory of my bundle; if $this->get('kernel')->getRootDir()
, the path to the App directory. Either way I am not able to move back to the upper directory.
How do I do?
ps. something like $this->get('kernel')->getRootDir().'/../src/
renders .../app/../src/
.
EDIT I got what i need by a very simple minded and ugly fix:
$path001 = $this->get('kernel')->getRootDir().'/';
$path002 = explode('/', $path001);
$path003 = array_pop($path002);
$path003bis = array_pop($path002);
$path004 = implode("/", $path002);
$path_pdf = $path004.'/src/Mario/<MyBundle>/Bundle/Resources/public/pdf';
Is there a better way?
Upvotes: 0
Views: 1498
Reputation: 48865
The realpath function (http://php.net/manual/en/function.realpath.php) can clean up all the dot dot stuff if it bothers you.
// From a controller
$resourceDir = realpath(__DIR__ . '/../Resources');
Of course this only works if the controller is in a fixed directory and never moves.
I like to set a parameter using the dependency injection extension.
class CeradProjectExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter(
'sportacus_project_resources_dir',
realpath(__DIR__ . '/../Resources')
);
The path can be injected or retrieved from the container.
Upvotes: 2
Reputation: 1985
yes, there is:
$kernel = $this->container->getService('kernel');
$path = $kernel->locateResource('@MarioMyBundle/Resources/public/pdf/file.pdf');
as per Accessing Files Relative to Bundle in Symfony2
Upvotes: 0