php - symfony 3 - 来自数据库的 url 图片

标签 php database image symfony

我想在我的文件 twig 中显示来自 bdd 的图像的 url 我收到图像错误,例如: bdd

这是我的 bdd: bdd

还有我对 Twig 的看法:

{% extends '@App/layout.html.twig' %}

{% block title %} Illustration {% endblock %}

{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
    <h2> {{ imageFormation.image.nom }}</h2>
    <hr>
    <img src=" {{ imageFormation.image.url |raw }} "/><br><br>
</div>
{% endblock %}

你能帮我解决我的问题吗? 我不知道为什么我不能得到真实的图像。

编辑:

/**
     * @Route("/formation/{formation}" , name="image")
     * @param $image
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function imageFormationAction($formation){


        $doctrine = $this->container->get('doctrine');
        $em = $doctrine->getManager();

        $imageRepository = $em->getRepository('AppBundle:Formation');

        $imageFormation = $imageRepository->findOneByid($formation);

        return $this->render('@App/formation/formationImage.html.twig', array(
            'imageFormation' => $imageFormation
        ));
    }

bdd

bdd

最佳答案

你的图片 url 必须是相对于 web 目录的,比如 fichier\Mention.jpg,而不是图片在你的文件系统中的路径 C:\xampp\htdocs\Symfony\MonProjetCv\web\fichier\Mention.jpg.

在数据库中保存图像文件的相对路径(url 等于 fichier\Mention.jpg),并在 Twig 文件中使用 asset 函数:

{% extends '@App/layout.html.twig' %}

{% block title %} Illustration {% endblock %}

{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
    <h2> {{ imageFormation.image.nom }}</h2>
    <hr>
    <img src=" {{ asset(imageFormation.image.url); }} "/><br><br>
</div>
{% endblock %}

关于php - symfony 3 - 来自数据库的 url 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44120325/

相关文章:

mysql - 如何编写SQLITE触发器来计算数据库更新或插入时的总和?

python - 诱变剂 : how to extract album art properties?

html - 如何让一个 <div> 元素正好位于另一个 <div> 元素之下

PHP - 删除具有相同引用的重复值

php - Paypal IPN 发送 'pending',原因是 'multi-currency'?

php - 在 php 中显示日期 dd/mm/yy 以及从日期/时间开始的时间

database - 如何写sql语句恢复11g oracle数据库用户密码

MySQL查询删除日期接近另一行的行

wpf - WPF MVVM 列表中的图像

php - 实体上的 MySQL Double 多对多