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

标签 symfony twig

编辑:
简短地说:有没有办法调用 $kernel->getRootDir();从 Twig 扩展中?或者可能来自 DI 容器?

原问题:

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

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



我正在使用我自己写的 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”的子目录

有任何想法吗?

最佳答案

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

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

关于Symfony2 : How to set the root path in a Symfony CLI command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9634497/

相关文章:

php - SessionHandler::write():父 session 处理程序未打开

php - 如何使查询和结果缓存适用于 Symfony2 和 Doctrine2 中关联实体的查询?

Twig - 遍历数组的一部分

twig - 如何在 Shopware 6 中创建自定义字段并在前端显示?

php - 访问 Symfony2 请求对象中的 POST 值

php - 如何收集我的 symfony 项目的所有路由来创建站点地图?

php - Symfony2 在测试中访问私有(private)服务

javascript - Symfony 3 从用户那里获取时区,而无需以表单询问他们

templates - 限制字符串 Twig

symfony - Symfony2长文本翻译