使用 laravel 在电子邮件中显示动态视图

show dynamic view in email with laravel

我得到用户在项目中上传的模板,项目将它们存储在 public/templates。

每个模板都有一个文件夹,文件夹里有两个文件。第一个preview.png,第二个template.blade.php

当我显示模板预览时,它显示为必须显示,但是当我尝试使用此模板发送电子邮件时,出现此错误 View [default1.template] not found

这是config/view.php :

'paths' => [
    template_path(),
    resource_path('views'),
],

这是我的助手(template_path() 函数):

if(!function_exists('template_path')){
    function template_path(){
        return public_path('templates');
    }
}

在 AppServiceProvider 中我使用了这段代码:

$this->loadViewsFrom(template_path(), 'template');

这是我的可邮寄文件 (SendMail.php) :

$view = $this->notification->template->path . '.template';

if (!is_null($this->notification->replyToEmail))
    return $this->view('template::'.$view)->subject($this->notification->subject)->replyTo($this->notification->replyToEmail, $this->notification->replyToName);
else
    return $this->view('template::'.$view)->subject($this->notification->subject);

最后这是我的发送电子邮件代码:

Mail::to($this->subscriber->email)->send(
    new SendMail(
        $this->subscriber,
        $this->notification,
        $this->products,
        $this->group,
    )
);

编辑: 当我在 AppServiceProvider 中获得 dd(View::exists('template::'.$view)) 时,此视图存在,但是当我在 Mailable 文件 (SendMail.php) 中获得 dd(View::exists('template::'.$view)) 时,它什么也没抛出。

编辑 2: 此模板存在于 /public/templates 目录中。

.template 是一个 blade 文件,它的名字是 template.blade.php


注意:在本地运行就可以了。但它在服务器中抛出错误

在服务器中,我将此代码放入 index.php 文件

$app->bind('path.public', function() {
    return __DIR__;
});

并将除 public 目录 之外的所有 我的文件和目录放入主机 root。并将 public 目录内容 放入 /public_html

我将我的视图存储在存储中并将存储视图连接到控制器。这是一个解决方案,这个错误 View [default1.template] not found 已解决