Php 图像未显示-Symfony twig
我是Symfony的新手,正在尝试了解如何显示数据库中的图像。我遵循Symfony文档,当我检查它时,它向我显示了正确的路径,但是我不确定我做错了什么 控制器代码:Php 图像未显示-Symfony twig,php,symfony,Php,Symfony,我是Symfony的新手,正在尝试了解如何显示数据库中的图像。我遵循Symfony文档,当我检查它时,它向我显示了正确的路径,但是我不确定我做错了什么 控制器代码: public function new(Request $request): Response { $repa = new Repas(); $form = $this->createForm(RepasType::class, $repa); $form->handleRequest($requ
public function new(Request $request): Response
{
$repa = new Repas();
$form = $this->createForm(RepasType::class, $repa);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$img = $request->files->get('repas')['img'];
$uploads_directory = $this->getParameter('uploads_directory');
$filename = md5(uniqid()) . '.' . $img->guessExtension();
$img->move(
$uploads_directory,
$filename
);
$entityManager = $this->getDoctrine()->getManager();
$repa->setimg($filename);
$entityManager->persist($repa);
$entityManager->flush();
return $this->redirectToRoute('repas_index');
}
return $this->render('repas/new.html.twig', [
'repa' => $repa,
'form' => $form->createView(),
]);
}
细枝代码:
{%for repa in repas%}
请让我知道我做错了什么,并提前向您表示感谢x尝试以下方法:
{{ asset('uploads/' ~ repa.getImg()) }}
您的所有请求都被重定向到
public
文件夹,因此无需将public/
添加到资产中。嘿,只是一个空白,没有显示任何内容,让我感到困惑的是,当我检查元素时,我得到了完整路径和图像名称。(src=“/public/uploads/e9cd9b708af1d706add7e529332fc665.png”)您是否检查过该文件是否存在?如果复制该路径,将其放在主机之后并通过浏览器访问,是否看到该图像?是的,似乎需要从路径中删除“public/”,因为资产意味着文件夹public非常感谢:)这就是问题x