【问题标题】:How to include Laravel blade markdown within a component for mail?如何在邮件组件中包含 Laravel 刀片降价?
【发布时间】:2018-12-26 15:10:27
【问题描述】:

我正在尝试创建一封欢迎电子邮件,其中包含一个按钮,该按钮可以链接回我的 Laravel 项目中的用户个人资料,但似乎无法正常工作。这是我的代码:

@component('mail::message')
Thanks for signing up {{ $user->fn }}

@component('mail::button', ['url' => 'https://site.dev/users/{{ $user->id }}'])
Check out your profile!
@endcomponent

<br>

{{ config('app.name') }}
@endcomponent

当我尝试这个时,电子邮件会发送,但它链接到

https://site.dev/users/%3C?php%20echo%20e(%user-%3Eid);%20?%3E

我怎样才能让它正常工作,通过像这样的 url 反映

https://site.dev/users/1

我可以访问用户模型特征,但在这种情况下无法使用刀片指令,我还能如何使其工作?

【问题讨论】:

    标签: laravel routing components laravel-blade


    【解决方案1】:

    .blade.php 文件由 Laravel 渲染,在此过程中各种 Blade 标签被转换为 PHP 执行。例如,{{ $variable }} 转换为 &lt;?php e($variable); ?&gt;

    向组件传递参数时不需要使用 Blade 标签,参数的传递方式与在控制器或模型中调用方法时传递参数的方式相同。组件参数使用纯PHP定义,不涉及Blade。

    你可以像这样连接字符串:

    @component('mail::button', ['url' => 'https://site.dev/users/' . $user->id])
        Check out your profile!
    @endcomponent
    

    或者您可以在双引号字符串中使用花括号:

    @component('mail::button', ['url' => "https://site.dev/users/{$user->id}"])
        Check out your profile!
    @endcomponent
    

    或者你可以使用路由助手(推荐):

    @component('mail::button', ['url' => route('users.show', ['id' => $user->id])])
        Check out your profile!
    @endcomponent
    

    【讨论】:

    • 感谢您提供快速有效的解决方案!还有一点背景:)
    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 2018-06-15
    • 2013-11-22
    • 2018-06-04
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多