2012-03-09 94 views
4

編輯: 切斷這個短路:有沒有辦法調用$ kernel-> getRootDir();從一個樹枝延伸?或者也許從DI容器?Symfony2:如何在Symfony CLI命令中設置根路徑

原題:

我嘗試通過想象來縮放服務器上的圖像。 在這種情況下,它看起來像有一種錯誤的路徑SET-我得到一個錯誤:

[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.

我使用,我寫了我自己一個樹枝標籤:

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); 
... 

我也試過這個(這兩部作品時,我通過呈現瀏覽器請求的模板)

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

$路徑設置爲 「文件/ 4f59ef3f76e74_test3.jpg」 「文件/」 是 「網絡」

任何想法的一個子目錄?

回答

10

Ha-找到它了!這將檢索Web文件夾的絕對系統文件路徑:

$webRoot = $this->container->get('kernel')->getRootDir()."/../web"; 
+2

我認爲您應該能夠在服務配置中定義Twig擴展時注入該參數? '%kernel.root_dir%'或類似的。 – richsage 2012-03-10 16:24:29

+1

我認爲它是$ this-> getContainer()而不是$ this-> container – AchrafSoltani 2015-07-27 17:11:33