我想在我的文件 twig 中显示来自 bdd 的图像的 url 我收到图像错误,例如:
这是我的 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
));
}
最佳答案
你的图片 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/