本文介绍了在自定义的magento 2模块中使用外部php库的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是magento的新手,我目前正在为magento2开发自定义模块,并且我想在Block文件中使用外部php库(PHPMailer).

I am new on magento I am currently working on a custom module for magento2 and I want to use an external php library (PHPMailer) within a Block file.

我的项目文件结构:

ModuleFolder
---等
.
.
---块
------- Main.php
--- lib
------- PHPMailer
.
.

ModuleFolder
---etc
.
.
---Block
------- Main.php
---lib
------- PHPMailer
.
.

我尝试使用以下方法将PHPMailer类包含在main.php块中:

I tried to include the PHPMailer class within my block main.php using:

require_once(__DIR__."/../lib/PHPMailer/src/PHPMailer.php");

对于我使用的类声明:

$mail = new PHPMailer();

我也试图将PHPMailer库包含在Block文件夹中,但没有任何效果

also i tried to include the PHPMailer library in the Block folder and nothing works

它总是返回:

PHPMailer class is not found in /...../Block/Main.php

当我尝试将PHPMailer.php直接放置在Block文件夹中时,如下所示:

And when i tried to put the PHPMailer.php directly in the Block folder like this:

---块
----- Main.php
----- PHPMailer.php

---Block
-----Main.php
-----PHPMailer.php

并包含

require_once(__DIR__."/PHPMailer.php");

它返回:无法在Main.php中声明PHPMailer类,因为该名称已在PHPMailer.php中使用

it returns: cannot declare PHPMailer class in Main.php because the name is already in use in PHPMailer.php

我从github安装了最新版本的PHPMailer: https://github.com/PHPMailer/PHPMailer

I installed the latest version of PHPMailer from github: https://github.com/PHPMailer/PHPMailer

我决定使用它,因为它是如此简单和直接.

And i decided to use it because it is so easy and straightforward.

那么我该如何使用这个库,什么是最好的方法呢?

So how can i use this library and what is the best way for this ?

谢谢!

推荐答案

Magento 2首先使用 Composer 构建阶级公民.您还应该使用Composer安装PHPMailer: https://github.com/PHPMailer/PHPMailer #installation--加载

Magento 2 is built with Composer as a first class citizen. You should use Composer to install PHPMailer as well: https://github.com/PHPMailer/PHPMailer#installation--loading

composer require phpmailer/phpmailer

这意味着Composer负责处理PHPMailer类的自动加载,您可以在项目代码中立即使用它:

This means the PHPMailer class autoloading is taken care of by Composer, and you can use it immediately in your project code:

$mail = new \PHPMailer\PHPMailer\PHPMailer();

这篇关于在自定义的magento 2模块中使用外部php库的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:42