【问题标题】:Symfony2: How to set the root path in a Symfony CLI commandSymfony2:如何在 Symfony CLI 命令中设置根路径
【发布时间】:2012-03-09 13:02:41
【问题描述】:

编辑: 简而言之:有没有办法调用 $kernel->getRootDir();从树枝扩展中?或者可能来自 DI 容器?

原始问题:

我尝试使用 Imagine 来缩放服务器上的图像。一切正常,因为我不尝试通过命令行触发渲染: 在这种情况下,看起来路径设置错误 - 我收到错误:

[Twig_Error_Runtime]

在渲染模板期间引发了异常 (“文件 ../web/documents/4f59ef3f76e74_test3.jpg 不存在”) “....:detail.html.twig”在第 72 行。

我使用的是我自己写的 twig 标签:

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

$path 设置为“documents/4f59ef3f76e74_test3.jpg” “documents/”是“web”的子目录

有什么想法吗?

【问题讨论】:

    标签: symfony twig


    【解决方案1】:

    哈- 找到了!这将检索 web 文件夹的绝对系统文件路径:

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

    【讨论】:

    • 我认为您应该能够在服务配置中定义 Twig 扩展时注入该参数? <argument>%kernel.root_dir%</argument> 或类似名称。
    • 我认为是 $this->getContainer() 而不是 $this->container
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2020-08-18
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多