Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: How to set the root path in a Symfony CLI command

Tags:

twig

symfony

EDIT: to cut this short: is there a way to call $kernel->getRootDir(); from within a twig extension? Or maybe from the DI container?

ORIGINAL QUESTION:

I try to scale images on the server by using Imagine. Everything works fine as log as I don't try to trigger the rendering via command line: In this case it looks like there is a wrong path set- I'm getting an error:

[Twig_Error_Runtime]

An exception has been thrown during the rendering of a template ("File ../web/documents/4f59ef3f76e74_test3.jpg doesn't exist") in "....:detail.html.twig" at line 72.

I'm using a twig tag which I wrote by myself:

 public function thumbnail($path,$width,$maxHeight=0,$alt="",$absolute=false){

        /* @var $imagine \Imagine\Gd\Imagine */
        $imagine = $this->container->get('imagine');

        //$box = new \Imagine\Image\Box($width, $height);

        /* @var $image \Imagine\Image\ImageInterface */
        $image = $imagine->open("../web/".$path);
...

I also tried this (both works when I render the template via browser request)

$image = $imagine->open($path);

$path is set to "documents/4f59ef3f76e74_test3.jpg" "documents/" is a sub directory of "web"

Any ideas?

like image 330
stoefln Avatar asked Dec 27 '22 04:12

stoefln


1 Answers

Ha- found it! This retrieves the absolute system file path of the web folder:

$webRoot = $this->container->get('kernel')->getRootDir()."/../web";
like image 62
stoefln Avatar answered Jan 05 '23 11:01

stoefln