src/Controller/ImportController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Upload;
  4. use App\Form\UploadType;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Console\Input\ArrayInput;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpKernel\KernelInterface;
  10. use Symfony\Component\Console\Output\BufferedOutput;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. class ImportController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/user/upload", name="app_upload")
  17.      */
  18.     public function Upload(Request $request)
  19.     {
  20.         $upload = new Upload();
  21.         $form $this->createForm(UploadType::class, $upload);
  22.         $form->handleRequest($request);
  23.         if($form->isSubmitted() && $form->isValid()){
  24.             $file $form['filename']->getData();
  25.             $file->move($this->getParameter('upload_directory'), 'datas.csv');
  26.             return $this->redirectToRoute('app_import');
  27.         }
  28.         return $this->render('upload/form.html.twig', array(
  29.             'form'=>$form->createView()
  30.         ));
  31.     }
  32.     /**
  33.      * @Route("/user/import", name="app_import")
  34.      */
  35.     public function Import(KernelInterface $kernel): Response
  36.     {
  37.         $application = new Application($kernel);
  38.         $application->setAutoExit(false);
  39.         $input = new ArrayInput([
  40.             'command' => 'csv:import',
  41.             // (optional) define the value of command arguments
  42.             //'fooArgument' => 'barValue',
  43.             // (optional) pass options to the command
  44.             //'--bar' => 'fooValue',
  45.         ]);
  46.         // You can use NullOutput() if you don't need the output
  47.         $output = new BufferedOutput();
  48.         $application->run($input$output);
  49.         
  50.         // return the output, don't use if you used NullOutput()
  51.         $content $output->fetch();
  52.         // return new Response(""), if you used NullOutput()
  53.         return new Response($content);
  54.     }
  55. }