Hi Everyone,
i'm trying to write a custom module for Magento 2.2. It have to write a .csv file. I read on different sites somebody that use class in \Magento\Framework\App\Response\Http\FileFactory but if I open \Magento\Framework\App\Response I don't have \Http\FileFactory.
Someone can help me? Maybe it a stupid problem but I'm new in development of Magento custom module.
Thank you guys
Solved! Go to Solution.
You can find your magento framework module under lib folder in root.
your path will be,
lib/internal/Magento/Framework/File/Csv.php
Hi @Magenz,
Maybe you'll find useful to check how the Export works on Magento to get an idea of which classes are being used.
You can write csv file using below way,
<?php public function __construct( \Magento\Framework\File\Csv $csvProcessor, \Magento\Framework\App\Filesystem\DirectoryList $directoryList, \Magento\Framework\Filesystem $filesystem ) { $this->filesystem = $filesystem; $this->directoryList = $directoryList; $this->csvProcessor = $csvProcessor; } function writeToCsv(){ $fileDirectoryPath = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR); if(!is_dir($fileDirectoryPath)) mkdir($fileDirectoryPath, 0777, true); $fileName = 'export.csv'; $filePath = $fileDirectoryPath . '/' . $fileName; $data = []; /* pass data array to write in csv file */ $data[] = ['orderid' => '100001']; $this->csvProcessor ->setEnclosure('"') ->setDelimiter(',') ->saveData($filePath, $data); return true; }
You can check your generated CSV file inside var folder
Hi @Rakesh Jesadiya,
thanks for the code, but the problem is that I don't have these path: \Magento\Framework\File\Csv ; \Magento\Framework\App\Filesystem\DirectoryList ;
\Magento\Framework\Filesystem
(I have \Magento\Framework and \Magento\Framework\App of course)
Hello @Damian Culotta,
thanks for the answer. But I can't find these necessary classes in my Magento\Framework
Have you installed Magento 2 using the command line or using a downloaded zip file?
no, I used an installation tool provided by the server host.
UPDATE: I have just installed again Magento using .zip file and I still can't find those classes in \Magento\Framework
You can find your magento framework module under lib folder in root.
your path will be,
lib/internal/Magento/Framework/File/Csv.php
you're right. I have checked on my Magento Installation and there isn't the path
lib/internal/Magento/
But, checking on another Magento Installation (local) I founf it. Maybe there were some problem with installation. Thank you so much