origins523
origins523

Reputation: 57

symfony fileuploader undefined variable

I have a form with a fileupload and for this implementation I've followed the official documentation of symfony: When I try to save my form, I get the following error:

Notice: Undefined variable: fileUploader

It means that the following code from my controller has a problem in it...

<?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())); 
     }
}

...on the line $filename = $fileUploader->upload($file);. Even though I've linked my Service with a USE-Statement. Below is my service:

<?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;
     }
}

And I've defined the service under services.yml:

AppBundle\Service\FileUploader:
   arguments:
      $targetDir: '%file_directory'

config.yml:

parameters:
    file_directory: '/tmp'

Does anyone know what I'm missing?

Upvotes: 1

Views: 907

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

you need to pass FileUploader to the controller like this:

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

Or get the service into your controller like this:

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

Or instantiate It into your code like this:

$fileUploader = new FileUploader();

If you read the documentation inside the action It pass the FileUploader :

Documentation

Upvotes: 2

Related Questions