0

我使用 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')
    );
}

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

4

2 回答 2

0

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

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

    Downloadlink created

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

    @endcomponent    
@endcomponent
于 2018-10-16T08:44:00.060 回答
0

如何创建这样的路由:routes.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 

希望这可以帮助你。

于 2018-10-17T00:28:48.687 回答