我有以下基于 Symfony 5 的 PHP Web 应用程序结构:.├── LICENSE├── bin├── composer.json├── composer.lock├── config├── migrations├── phpunit.xml.dist├── public│ ├── images│ │ ├── 3e28ae7b9f72cb1958e532161709f559.jpg│ │ ├── f16b8e3277415e2783d076177aee8f7e.jpg│ │ └── fe0920ac3d2b89f1cd4d787cedc3daf4.jpg│ └── index.php├── src│ ├── Controller│ │ └── CatController.php├── symfony.lock├── templates├── tests├── translations├── var└── vendorCatController.php:<?phpnamespace App\Controller;use App\Entity\Cat;use App\Repository\CatRepository;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;/** * @Route("/cat") */class CatController extends AbstractController{ /** * @Route("/{id}", name="cat_show", methods={"GET"}) */ public function show(Cat $cat): Response { return $this->render('cat/show.html.twig', [ 'cat' => $cat, ]); }}Cat.php(实体):<?phpnamespace App\Entity;use App\Repository\CatRepository;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass=CatRepository::class) */class Cat{ /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $image; public function getId(): ?int { return $this->id; } public function getImage(): ?string { return $this->image; } public function setImage(string $image): self { $this->image = $image; return $this; }}cat/show.html.twig:{{ cat.image }}<img src="{{ cat.image }}"/ >该模板生成 HTML,看起来正确:images/3e28ae7b9f72cb1958e532161709f559.jpg <img src="images/3e28ae7b9f72cb1958e532161709f559.jpg">但图像实际上并未渲染,当我右键单击图像应在的位置并选择“在新选项卡中打开”时。将打开一个新选项卡,并将 URL 设置为:http://localhost:8000/cat/images/3e28ae7b9f72cb1958e532161709f559.jpg有没有办法阻止/cat渲染 URL 上的前缀?这导致图像 url 无法正确解析。
3 回答
慕田峪4524236
TA贡献1875条经验 获得超5个赞
<img src="{{ absolute_url(asset(cat.image)) }}"/ >
https://symfony.com/doc/current/templates.html#linking-to-css-javascript-and-image-assets
- 3 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消