我使用vich uploader包来显示添加到我的数据库的图像
这是我的代码
config.yml:
vich_uploader:
db_driver: orm
mappings:
cours_image:
uri_prefix: /images/cours
upload_destination: '%kernel.root_dir%/../web/images/cours'
inject_on_load: true
delete_on_update: false
delete_on_remove: true
namer: vich_uploader.namer_origname
实体
<?php
namespace DataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
/**
* CoursCode
*
* @ORM\Table(name="cours_code")
* @ORM\Entity
* @Vich\Uploadable
*/
class CoursCode
{
/**
* @var integer
*
* @ORM\Column(name="id_cours", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idCours;
/**
* @var string
*
* @ORM\Column(name="titre_cours", type="string", length=50, nullable=false)
*/
private $titreCours;
/**
* @var string
*
* @ORM\Column(name="contenu_cours", type="text", length=65535, nullable=false)
*/
private $contenuCours;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="cours_image", fileNameProperty="imageName", size="imageSize")
* @Assert\File(maxSize="1200k",mimeTypes={"image/png", "image/jpeg", "image/pjpeg"})
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @var integer
*/
private $imageSize;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*
* @return CoursCode
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* @return File|null
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*
* @return CoursCode
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* @return string|null
*/
public function getImageName()
{
return $this->imageName;
}
/**
* @param integer $imageSize
*
* @return CoursCode
*/
public function setImageSize($imageSize)
{
$this->imagesize = $imageSize;
return $this;
}
/**
* @return integer|null
*/
public function getImageSize()
{
return $this->imageSize;
}
/**
* @return int
*/
public function getIdCours()
{
return $this->idCours;
}
/**
* @param int $idCours
*/
public function setIdCours($idCours)
{
$this->idCours = $idCours;
}
/**
* @return string
*/
public function getTitreCours()
{
return $this->titreCours;
}
/**
* @param string $titreCours
*/
public function setTitreCours($titreCours)
{
$this->titreCours = $titreCours;
}
/**
* @return string
*/
public function getContenuCours()
{
return $this->contenuCours;
}
/**
* @param string $contenuCours
*/
public function setContenuCours($contenuCours)
{
$this->contenuCours = $contenuCours;
}
}
树枝:
{% for c in cours %}
<div class="col-md-6 col-sm-12">
<article class="cp-taxi-holder cp-deals-holder">
<figure class="cp-thumb">
<img src=" {{ vich_uploader_asset(c, 'imageFile') }}" />
</figure>
<div class="cp-text">
<h3> {{ c.titreCours}}</h3>
<ul class="cp-meta-listed">
<li>Niveau de difficulté <span>moyen</span></li>
</ul>
<a href="#" class="cp-btn-style1">Voir les lessons</a>
</div>
</article>
</div>
{% endfor %} }}
控制器:
public function AfficheCoursFrontAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$cours = $em->getRepository('DataBundle:CoursCode')->findAll();
/**
* @var $paginator \knp\Component\Pager\paginator
*/
$paginator = $this->get('knp_paginator');
$result = $paginator->paginate(
$cours,
$request->query->getInt('page',1) /*page number*/,
$request->query->getInt('limit',4) /*limit per page*/
);
形式:
类AjoutCoursType扩展AbstractType { public function buildForm(FormBuilderInterface $ builder,array $ options) { $助洗剂
->add('titreCours')
->add('contenuCours')
->add('imageFile', VichImageType::class, [
'required' => false,])
->add('Ajout',SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DataBundle\Entity\CoursCode'
));
}
symfony正在读取文件,但没有显示 我找不到问题
答案 0 :(得分:0)
您应该做的第一件事是检查您的html渲染并搜索图像,然后检查src
是否正确。如果链接正确,则可能是权限错误,您应该查看应用于上传文件夹web/images/cours
的权限。