我有以下基于 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
└── vendor
CatController.php:
<?php
namespace 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(实体):
<?php
namespace 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 无法正确解析。
慕田峪4524236
慕后森
慕运维8079593