当前位置: 技术文章>> 如何在Magento 2中以编程方式创建目录?

文章标题:如何在Magento 2中以编程方式创建目录?
  • 文章分类: Magento
  • 28860 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中以编程方式创建目录的步骤:

第1步: 首先,在 etc文件夹中创建一个CreateDirectory.php文件。

app\code\Vendor\Extension\Helper

然后添加以下代码

<?php
namespace Vendor\Extension\Helper;
 
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Directory\WriteInterface;
 
class CreateDirectory extends AbstractHelper
{
    protected $file;
 
    protected $newCreateDirectory;
 
    public function __construct(
        Filesystem $file
    ) {
        $this->newCreateDirectory = $file->getDirectoryWrite(DirectoryList::VAR_DIR);
    }
 
    public function createDirectory()
    {
        $directoryPath = "magecomp";
        $newCreateDirectory = false;
        try
        {
            $newCreateDirectory = $this->newCreateDirectory->create($directoryPath);
        } catch (FileSystemException $e) {
            throw new LocalizedException(
                __('you can't create directory', $directoryPath)
            );
        }
        return $newCreateDirectory;
    }
}

结论:

因此,您可以在Magento 2中以编程方式轻松创建目录。您甚至可以以编程方式删除Magento 2中的目录


推荐文章