Laravel8如何引入第三方类

比如我们有个发送邮件的类Email.php位于/app/Http/lib/Email目录下

比如我们有个发送邮件的类Email.php位于/app/Http/lib/Email目录下

<?php
namespace App\Http\lib\Email;
use App\Http\Response\ApiErrDesc;
use App\Http\Response\ResponseJson;
use Illuminate\Support\Facades\Mail;
class Email
{
    use ResponseJson;
//通用发送邮件
    public function sendemail($email,$type_desc){
        $email=$email; //用户邮箱
        $type_desc=$type_desc; //邮件描述
        try {
            Mail::raw(
                $email. PHP_EOL .$type_desc,
                function ($msg) use ($email,$type_desc) {
                    $msg->from('123456@163.com', '织梦云博客');
                    $msg->subject($type_desc);
                    $msg->to($email);
                }
            );
            return $this->jsonData(ApiErrDesc::SUCCESS_EMAIL[0],ApiErrDesc::SUCCESS_EMAIL[1]);


        } catch (\Exception $e) {
            return $this->jsonData(ApiErrDesc::SUCCESS_EMAIL[0],$e->getMessage());
        }
    }
}

1、添加代码到项目根目录下的composer.json文件

"classmap": [
            "app/Http/lib/Email"
        ],

2、如下图所示:

3、执行更新

composer dump-autoload

4、使用方法

<?php
use App\Http\lib\Email\Email;
class Index extend Controller{
 public function index(){
    $newEmail= new Email();
    $resMsg=$newEmail->sendemail();
  } 
}

本文标题为:Laravel8如何引入第三方类

基础教程推荐