I have a module that primarily works with the admin-side of Magento 2.
At the minute, I'm just trying to parse a CSV file and log the contents, here's a skimmed version of my Block class:
app/code/Vendor/Module/Block/Adminhtml/Import.php
<?php
namespace Vendor\Module\Block\Adminhtml;
class Import extends \Magento\Framework\View\Element\Template
{
public function parseFile($file)
{
foreach (array_map('str_getcsv', file($file)) as $num => $row)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/bob.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(print_r($row, 1). PHP_EOL);
}
}
}
Which I attempt to call in my (skimmed) Controller class:
app/code/Vendor/Module/Controller/Adminhtml/Import/Submit.php
class Submit extends \Magento\Framework\App\Action\Action
{
protected $_resultLayoutFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
)
{
parent::__construct($context);
$this->_resultLayoutFactory = $resultLayoutFactory;
}
public function execute()
{
$files = (array) $this->getRequest()->getFiles();
if (!empty($files)) {
$importFile = $files['import_file'];
if (!$importFile['error']) {
$resultLayout = $this->_resultLayoutFactory->create();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/another.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($resultLayout->getLayout()->getBlock('Vendor\Module\Block\Adminhtml\Import'));
}
}
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
Going to another.log show's:
2020-02-28T14:18:16+00:00 INFO (6):
I've ran:
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush
But to no avail. I turned on the With Block Hints option in debugging and it matches, so I'm not quite sure why my Controller can't see it.
How do I use my Block class in my Controller class (adminhtml side)?