symfony fileuploader undefined变量

时间:2017-10-10 09:22:50

标签: php symfony file-upload service symfony-3.3

我有一个带有fileupload的表单,对于这个实现,我遵循了symfony的官方文档: 当我尝试保存表单时,出现以下错误:

  

注意:未定义的变量:fileUploader

这意味着我的控制器中的以下代码存在问题......

<?php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Users;
use AppBundle\Service\FileUploader;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class DefaultController extends Controller {
/**
     * @Route("/bearbeiten/{id}", name="edit")
     */
     public function editAction($id, Request $request){
         //Daten aus der Datenbank mit $id
          $listen = $this->getDoctrine()
                ->getRepository('AppBundle:Users')
                ->find($id);
        //Variabeln vom passende Eintrag werden geholt und gesetzt
        $listen->setVorname($listen->getVorname());
        $listen->setNachname($listen->getNachname());
        $listen->setStrasse($listen->getStrasse());
        $listen->setOrt($listen->getOrt());
        $listen->setPLZ($listen->getPLZ());
        $listen->setBeschreibung($listen->getBeschreibung());
        $listen->setBild($listen->getBild());

        //Formular wird erstellt
        $form = $this->createFormBuilder($listen)
                ->add('vorname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('nachname', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('strasse', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('ort', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('plz', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('beschreibung', TextType::class, array('attr'=>array('class'=>'form-control', 'style'=>'margin-bottom:0.5cm; width:50%;')))
                ->add('bild', FileType::class, array('label'=>'Bild (JPEG-Datei)', 'data_class'=>null))
                ->add('save', SubmitType::class, array('label'=>'Speichern', 'attr'=>array('class'=>'btn btn-primary')))
                ->add('home', SubmitType::class, array('label'=>'zurück', 'attr'=>array('class'=>'btn btn-default')))
                ->getForm();

        $form->handleRequest($request);
        //Falls die Form valid ist....
        if($form->isSubmitted()){
            //Daten aus der Form in Variabeln sichern
             $vorname = $form['vorname']->getData();
             $nachname = $form['nachname']->getData();
             $strasse = $form['strasse']->getData();
             $ort = $form['ort']->getData();
             $plz = $form['plz']->getData();
             $beschreibung = $form['beschreibung']->getData();
             $file = $form['bild']->getData();

             **$filename = $fileUploader->upload($file);**

             $listen->setBild($filename);
             //Doctrine aktivieren
             $em=$this->getDoctrine()->getManager();
             $listen = $em->getRepository('AppBundle:Users');
             //Führt den Befehl in der DB aus
             $em->flush();

              if ($formsearch->get('home')->isClicked()){
                $textsa = 'Zurück geklickt';
                dump($textsa);
                return $this->redirectToRoute('homepage');
            }
             return $this->redirectToRoute('homepage');
         }

        return $this->render('main/edit.html.twig', array('listen'=>$listen, 'form'=>$form->createView())); 
     }
}

...行 $ filename = $ fileUploader-&gt; upload($ file); 。 即使我已将我的服务与USE-Statement相关联。 以下是我的服务:

<?php
namespace AppBundle\Service;

use Symfony\Component\HttpFoundation\File\UploadedFile;


class FileUploader
{
     private $targetDir;
     public function __construct($targetDir) {
         $this->targetDir = $targetDir;
     }
     public function upload(UploadedFile $file) {
         $fileName = md5(uniqid()).'.'.$file->guessExtension();
         $file->move($this->getTargetDir(), $fileName);
         return $fileName;
     }
     public function getTargetDir() {
         return $this->targetDir;
     }
}

我已经在services.yml:

下定义了服务
AppBundle\Service\FileUploader:
   arguments:
      $targetDir: '%file_directory'

config.yml:

parameters:
    file_directory: '/tmp'

有谁知道我错过了什么?

1 个答案:

答案 0 :(得分:2)

你需要将FileUploader传递给控制器​​,如下所示:

Observable<Boolean>

或者像下面这样将服务引入您的控制器:

public function editAction($id, Request $request, FileUploader $fileUploader){

或者将它实例化到你的代码中:

$fileUploader = $this->get('your_service');

如果您阅读了操作中的文档,则会传递FileUploader:

Documentation