【问题标题】:Laravel Email template route with parameter带参数的 Laravel 电子邮件模板路由
【发布时间】:2019-03-20 17:40:25
【问题描述】:

我使用 Laravel 创建了一个简单的电子邮件功能,用户收到一封电子邮件,我想在该电子邮件中调用一个下载文件的路由。

我的可邮寄:

class AppSent extends Mailable
{
    use Queueable, SerializesModels;

    public $name = '';

    public function __construct(String $name)
    {
        $this->name = $name;
    }

    public function build()
    {
        return $this->from(env('MAIL_USERNAME'))
                    ->markdown('emails.download.android_app');
    }
}

点击按钮时,我在我的 DownloadController 中调用此函数,它会向用户发送电子邮件:

return Mail::to($request->email)->queue(new AppSent($request->name));

这是用户获得的降价视图(电子邮件视图):

@component('mail::message')
    Hi,

    Downloadlink created

    @component('mail::button', ['url' => 'https://myapp.com/download'])
    Download!

    @endcomponent    
@endcomponent

这是我下载 zip 文件需要调用的路径:

public function download(Request $request)
{
    return response()->download(
        storage_path('/app/uploaded_apps/' . $request->name . '.zip')
    );
}

如何在我的下载刀片视图中添加参数以下载具有给定名称的特定文件?

【问题讨论】:

    标签: php laravel laravel-routing laravel-mail


    【解决方案1】:

    您可以在url数组中添加参数:

    @component('mail::message')
        Hi,
    
        Downloadlink created
    
        @component('mail::button', ['url' => 'https://myapp.com/download', 'name' => $name])
        Download!
    
        @endcomponent    
    @endcomponent
    

    【讨论】:

    • 没有用,它只是将我重定向到 /download 页面并且没有下载
    • 你能发布生成的链接吗?
    【解决方案2】:

    如何创建这样的路线: 路由.php

    Route::get('download/{name}', function($name){
       return response()->download(
            storage_path('/app/uploaded_apps/' . $name . '.zip')
        );
    });
    

    你也可以把下载函数放到一个控制器中……我们把它叫做DownloadController然后你就可以调用它了:

    Route::get('download/{name}', 'DownloadController@download');
    

    您需要让下载函数接受 $name 作为参数而不是 Request 请求。

    然后在邮件刀片中:

     @component('mail::button', ['url' => 'https://myapp.com/download/'.$name])
        Download!
    @endcomponent 
    

    希望这对你有帮助。

    【讨论】:

      猜你喜欢
      • 2021-06-22
      • 2021-09-21
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 2021-08-31
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多