【问题标题】:Laravel - Redirect with variable to twig pageLaravel - 使用变量重定向到树枝页面
【发布时间】:2017-11-09 20:54:10
【问题描述】:

我制作了一个带有 try/catch 情况的邮件功能。它检查邮件模板是否存在,如果不存在则需要将重定向/返回到带有消息的页面(return redirect('dashboard')->with('status', "Mail doesn't exists");

是否可以使用树枝页面通过变量进行重定向?还是我做错了什么?

这是我的邮件功能(DashboardController.php)

 public function MailContact($date_id, $contact_id)
{
    try
    {
        $contact = Contact::find($contact_id);
        $date = Date::find($date_id);
        $mail = Mails::where('datetype_id', $date->datetype_id)->first();
        $html = $mail->html;

        Mail::to($contact->email)->send(new Anniversary($html));

        return redirect('dashboard');
        }
    catch(\Exception $ex )
    {
        return redirect('dashboard')->with("Mail doesn't exists");
    }
}

这是树枝页面('dashboard.twig')

 <div class="box-body no-padding" style="width: 100%; padding-top:20px;">
  <ul class="telephoneKingList" style=" display: inline-block;
    width: 100%;">
        {% for dates in datestodayUser %}
           {% if dates is not empty %}
              <li data-id="{{ dates.id }}">
                 {{ dates.contact.fullname }}, {{ dates.contact.relation.company }} <br>
                 {{ dates.description }}<br>
                 {# Place where Error needs to be #}
                 <a role="button" id="edit-button" href="/dashboard/mail/{{ dates.id }}/{{ dates.contact.id }}" class="btn btn-primary btn-sm"><i class="fa fa-envelope"></i></a>
              </li>
              <hr>
           {% endif %}
           {% else %}
              <li>
                 Geen data beschikbaar.
              </li>
           {% endfor %}                         
  </ul>

【问题讨论】:

标签: php symfony redirect laravel-5 twig


【解决方案1】:

你可以的

return redirect('dashboard')->with(['message'=>"Mail doesn't exists"]);

然后在你的树枝上

{{message}}会给你留言

【讨论】:

  • 工作,谢谢。{{ $message }} 应该是 {{ message }} tho
  • 哎呀错过了,如果工作也接受相同
【解决方案2】:

使用

redirect('dashboard')->withErrors(["Mail doesn't exists"])

并且在您的树枝中,您需要使用

访问错误
{{ errors.first() }}

Laravel 有一个开箱即用的中间件,名为 ShareErrorsFromSession,它与所有视图共享会话错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2016-10-08
    • 2019-05-04
    • 2015-05-26
    • 2015-03-20
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多