Hewwo rashidkhan~
Symfony 文档在上传过程中相当完整,您阅读了吗?
http://symfony.com/doc/current/controller/upload_file.html
经过一些修改,我选择将其用作服务。
流程如下:
1) 给app/config/config.yml添加几个参数:
parameters下:
parameters:
locale: en
profile_directory: '%kernel.root_dir%/../web/upload/profile'
another_directory: '%kernel.root_dir%/../web/upload/another'
twig下
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
profile_directory: '/upload/profile/'
another_directory: '/upload/another/'
刚才添加的两个profile_directory将用作您的上传服务和twig中的变量以指向目标目录。
我添加了another_directory 以稍后解释更多内容。
2) 创建服务:
在src/YourBundle/Services/FileUploader.php下创建一个新文件
从这里开始,我的代码与您在 Symfony 网站上找到的代码有些不同。
FileUploader.php内容:
<?php
namespace YourBundle\Services;
use YourBundle\Entity\ProfileModel;
use YourBundle\Entity\Another;
class FileUploader {
private $profileDir;
private $anotherDir;
public function __construct($profileDir) {
$this->profileDir=$profileDir;
$this->anotherDir=$anotherDir;
}
public function upload($class) {
if($class instanceof ProfileModel) {
$file=$class->getPicture();
$fileName='picture-'.uniqid().'.'.$file->guessExtension();
$file->move($this->profileDir, $fileName);
$class->setPicture($fileName);
}
if($class instanceof Another) {
$file=$class->getPicture();
$fileName='picture-'.uniqid().'.'.$file->guessExtension();
$file->move($this->anotherDir, $fileName);
$class->setPicture($fileName);
}
return $class;
}
}
3) 将服务注册到app/config/services.yml:
services下:
services:
app.file_uploader:
class: YourBundle\Services\FileUploader
arguments:
- '%profile_directory%'
- '%another_directory%'
每个参数的顺序必须与private 在FileUploader.php 文件中的顺序相同。
这些参数是我们在app/config/config.yml 下parameters 下设置的参数。
4) 编辑您的控制器:
控制器部分很简单。
在导入部分添加use Symfony\Component\HttpFoundation\File\File;
newAction下
public function newAction(Request $request)
{
$profileModel = new ProfileModel();
$form = $this->createForm('YourBundle\Form\ProfileModelType', $profileModel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// We upload the file with this line
$profileModel=$this->get('app.file_uploader')->upload($profileModel);
$em = $this->getDoctrine()->getManager();
$em->persist($profileModel);
$em->flush();
return $this->redirectToRoute('profile_model_show', array('id' => $profileModel->getId()));
}
return $this->render('YourBundle:Default:new.html.twig', array(
'profileModel' => $profileModel,
'form' => $form->createView(),
));
}
editAction下
public function editAction(Request $request, ProfileModel $profileModel)
{
// Add this live above everything else in the code.
$profileModel->setPicture(new File($this->getParameter('profile_directory').'/'.$profileModel->getPicture()));
[...]
}
我还没有走得更远,所以我只能解释之后要修改什么......
在您的editAction 中,您还必须检查 $_FILES 是否为空。
如果不是,那么您执行上传过程。
如果是,请确保不要编辑 SQL 查询中的 picture 列(您必须执行自定义查询)
5) 您的树枝视图:
show.html.twig下
改变
<tr>
<th>Picture</th>
<td>{{ profileModel.picture) }}</td>
</tr>
到
<tr>
<th>Picture</th>
<td><img src="{{ asset(profile_directory~profileModel.picture) }}"></td>
</tr>
index.html.twig 也是如此。
您可以将其添加(而不是替换)到edit.html.twig 以预览实际图片。
6) 说明:
在app/config/config.yml 中,我们添加了一些目录用作文件中的参数。
如果需要,以后可以更轻松地更改这些目录。 (不必编辑大量文件...耶!)
Twig 目录总是从/web 文件夹开始。
当我们将服务注册为arguments 时会使用这些目录。
他们会在服务文件FileUploader.php中设置我们的变量。
与 Symfony 站点示例不同,我们将整个对象传递给上传服务。
然后,我们检查这个对象是从哪个类创建的,并基于它执行我们的上传过程。
然后,您在控制器中的上传过程将缩短为一行。
在 twig 中,我们还将使用在 app/config/config.yml 中设置的目录变量取消删除 twigproperty。
如上所述,如果我们的上传目录发生变化,我们将只需要编辑app/config/config.yml 文件。
希望这能帮助您解决上传问题。
诚挚的,
Preciel。