Symfony 上载文件不工作-vich_上载程序
我正在尝试使用vich_upload上传文件,但我不明白为什么图像没有更新:/ 表单未返回任何错误,但图像未保存。我不明白为什么,虽然它没有返回任何错误 能告诉我我的代码哪里错了吗 我先谢谢你 config.yml:Symfony 上载文件不工作-vich_上载程序,symfony,vichuploaderbundle,Symfony,Vichuploaderbundle,我正在尝试使用vich_upload上传文件,但我不明白为什么图像没有更新:/ 表单未返回任何错误,但图像未保存。我不明白为什么,虽然它没有返回任何错误 能告诉我我的代码哪里错了吗 我先谢谢你 config.yml: imports: - { resource: parameters.yml } - { resource: security.yml } - { resource: services.y
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: fr
app.path.partenaire_dir : /images/partenaires
framework:
#esi: ~
translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form_themes:
- 'SonataFormatterBundle:Form:formatter.html.twig'
- 'VichUploaderBundle:Form:fields.html.twig'
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
encryption: ssl
auth_mode: login
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
sonata_block:
default_contexts: [cms]
blocks:
# enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
sonata.admin.block.search_result:
contexts: [admin]
sonata.block.service.text:
sonata.block.service.rss:
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
gregwar_captcha:
width: 200
height: 50
length: 6
quality: 90
distortion: true
background_color: [41, 145, 214]
max_front_lines: 0
reload: true
as_url: true
interpolation: true
ivory_ck_editor:
default_config: default
configs:
default:
toolbar: standard
sonata_formatter:
default_formatter: text
formatters:
markdown:
service: sonata.formatter.text.markdown
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig #keep this commented unless you are using media bundle.
text:
service: sonata.formatter.text.text
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
rawhtml:
service: sonata.formatter.text.raw
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
richhtml:
service: sonata.formatter.text.raw
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
twig:
service: sonata.formatter.text.twigengine
extensions: [] # Twig formatter cannot have extensions
sonata_admin:
security:
handler: sonata.admin.security.handler.noop
acl_user_manager: null
title: 'Futurs'
title_logo: images/favicon.ico
templates:
# default global templates
layout: SonataAdminBundle::standard_layout.html.twig
ajax: SonataAdminBundle::ajax_layout.html.twig
dashboard: SonataAdminBundle:Core:dashboard.html.twig
dashboard:
blocks:
-
position: left
type: sonata.admin.block.admin_list
-
position: right
type: sonata.block.service.text
settings:
content: >
<h2 style="text-align:center;">Administration du site FUTURS</h2>
# CONFIGURATION DE VICH UPLOADER
vich_uploader:
db_driver: orm
mappings:
partenaire_dir:
uri_prefix: %app.path.partenaire_dir%
upload_destination: %kernel.root_dir%/../web/images/partenaires
namer: vich_uploader.namer_uniqid
inject_on_load: true
delete_on_update: true
delete_on_remove: true
导入:
-{resource:parameters.yml}
-{资源:security.yml}
-{资源:services.yml}
#在部署应用程序的每台计算机上,将不需要更改的参数放在此处
# http://symfony.com/doc/current/best_practices/configuration.html#application-相关配置
参数:
地点:fr
app.path.partenaire_dir:/images/partenaires
框架:
#esi:~
转换器:{回退:[%locale%]”
秘密:“%secret%”
路由器:
资源:“%kernel.root\u dir%/config/routing.yml”
严格要求:~
表格:~
csrf_保护:~
验证:{enable_annotations:true}
#序列化程序:{enable_annotations:true}
模板:
引擎:['twig']
默认语言环境:“%locale%”
受信任的\u主机:~
受信任的_代理:~
会议:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-身份证
handler\u id:session.handler.native\u文件
保存路径:“%kernel.root\u dir%/../var/sessions/%kernel.environment%”
片段:~
http_方法_覆盖:true
资产:~
#细枝形态
细枝:
调试:“%kernel.debug%”
严格的_变量:“%kernel.debug%”
表格主题:
-'SonataFormatterBundle:Form:formatter.html.twig'
-'VichUploaderBundle:Form:fields.html.twig'
#条令结构
教条:
dbal:
驱动程序:pdo_mysql
主机:“%数据库\主机%”
端口:“%数据库\端口%”
dbname:“%database_name%”
用户:%数据库用户%“
密码:“%database\u password%”
字符集:UTF8
#如果使用pdo_sqlite作为数据库驱动程序:
# 1. 在parameters.yml中添加路径
#例如,数据库路径:“%kernel.root\u dir%/data/data.db3”
# 2. 在parameters.yml.dist中取消对数据库路径的注释
# 3. 取消注释下一行:
#路径:“%database\u path%”
orm:
自动生成代理类:“%kernel.debug%”
命名策略:doctrine.orm.naming\u strategy.下划线
自动映射:true
#Swiftmailer配置
速递员:
传输:“%mailer\u传输%”
加密:ssl
身份验证模式:登录
主机:“%mailer\u主机%”
用户名:“%mailer\u用户%”
密码:“%mailer\u password%”
假脱机:{type:memory}
奏鸣曲组:
默认上下文:[cms]
阻碍:
#启用SonataAdminBundle块
sonata.admin.block.admin\u列表:
上下文:[管理员]
sonata.admin.block.search\u结果:
上下文:[管理员]
sonata.block.service.text:
sonata.block.service.rss:
fos_用户:
db_驱动程序:orm#其他有效值为'mongodb'和'couchdb'
防火墙名称:main
用户\u类:AppBundle\Entity\user
gregwar_验证码:
宽度:200
身高:50
长度:6
品质:90
失真:对
背景颜色:[41145214]
最大前端线:0
重新加载:正确
as_url:true
插值:正确
象牙色编辑:
默认配置:默认
配置:
违约:
工具栏:标准
奏鸣曲格式化器:
默认格式化程序:文本
格式化程序:
降价:
服务:sonata.formatter.text.markdown
扩展:
-奏鸣曲。格式化程序。细枝。控制流
-奏鸣曲
#-sonata.media.formatter.twig#除非您正在使用媒体包,否则请对此进行评论。
正文:
服务:sonata.formatter.text.text
扩展:
-奏鸣曲。格式化程序。细枝。控制流
-奏鸣曲
#-sonata.media.formatter.twig
原始HTML:
服务:sonata.formatter.text.raw
扩展:
-奏鸣曲。格式化程序。细枝。控制流
-奏鸣曲
#-sonata.media.formatter.twig
richhtml:
服务:sonata.formatter.text.raw
扩展:
-奏鸣曲。格式化程序。细枝。控制流
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Partenaire
*
* @ORM\Table(name="partenaire")
* @ORM\Entity(repositoryClass="AppBundle\Repository\PartenaireRepository")
* @Vich\Uploadable
*/
class Partenaire
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="logo", type="string", length=255)
*/
private $logo;
/**
* @Vich\UploadableField(mapping="partenaire_dir", fileNameProperty="logo")
*
* @var File
*/
private $logoFichier;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set logo
*
* @param string $logo
*
* @return Partenaire
*/
public function setLogo($logo)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* @return string
*/
public function getLogo()
{
return $this->logo;
}
public function setLogoFichier(File $logo = null)
{
$this->logoFichier = $logo;
if ($logo) {
$this->updatedAt = new \DateTime('now');
}
return $this;
}
public function getWebPath()
{
$val = $this->logo;
return $val;
}
/**
* @return File
*/
public function getLogoFichier()
{
return $this->logoFichier;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return Partenaire
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
class PartenaireAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$image = $this->getSubject();
$fileFieldOptions = array('required' => false);
if ($image && ($webPath = $image->getWebPath())) {
$fullPath = '/images/partenaires/'.$webPath;
// add a 'help' option containing the preview's img tag
$fileFieldOptions['help'] = '<img src="'.$fullPath.'" class="admin-preview" />';
}
else
{
$fileFieldOptions['help'] = 'Ajouter une Image (4Mo max) (1100x700)';
}
// $container = $this->getConfigurationPool()->getContainer();
$formMapper
->add('nom', 'text')
->add('description', CKEditorType::class, array(
'config' => array('toolbar' => 'full'),
))
->add('logoFichier', 'file', $fileFieldOptions);
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('nom');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('nom')
->add('description', 'text')
->add('logo','text');
}
}
use Vich\UploaderBundle\Form\Type\VichImageType;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper as VichUploaderManager;
class PartenaireAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
// ...
->add('logoFichier', VichImageType::class, $fileFieldOptions)
'help' => '<img src="'.$this->getVichUploaderManager()->asset($image, 'logoFichier').'" class="admin-preview" />'
->add('imageFile', VichImageType::class, [
'template' => 'AppBundle::Admin\..here_your_path..\name_of_your_template.html.twig',
'required' => false,
'download_link' => false,
])
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
<img src="{{ vich_uploader_asset(object, 'logoFichier') }}" alt="" style=""/>
</div>
{% endblock %}