重定向到页面时传递变量


Passing variable when redirecting to a page

我设计了一个带有输入验证的联系页面,如果验证成功通过,则将发送电子邮件。发送电子邮件后我遇到问题,$sent变量未传递到边栏选项卡视图以确认用户?

在 contact.blade.php 文件中,

@if (isset($sent) && $sent == true)
<p>Your details have been successfully send to us we will contact you shortly.</p>
@endif

在控制器中,显示/contact页 (GET)

   public function showContact()
    {
        $data = [];
        return View::make('contact')->withData($data);
    }

在控制器中,当用户按下提交按钮 (POST) 时

public function postContact()
{
    $contactRules = [
        'contact_name'    => 'required',
        'contact_email'   => 'required|email',
    ];
    $validator = Validator::make(Input::all(), $contactRules);
    if ($validator->fails()) {
        return Redirect::to('/contact')->withErrors($validator)->withInput();
    }
    $emailData['contact_name'] = Input::input('contact_name');
    $emailData['contact_email'] = Input::input('contact_email');
    Mail::send('emails.contact', $emailData, function ($message) {
        $message->to('email@domain.net', 'Name Name')->subject('Subject Here');
    });

    // Problem Here - $sent varible not passing through to view to confirm user?    
    return Redirect::to('/contact')->with('sent', true);
}

发送电子邮件后,它将重定向回/contact页面,但with('sent', true);没有通过?

获取数据使用典型的Session::get();

Session::get('sent');

这是拉拉维尔文档

医生 说,

注意:由于 with 方法将数据闪烁到会话,因此您可以使用典型的 Session::get 方法检索数据。


withErrors如何运作。这是文档

它说,

但是,请注意,我们不必将错误消息显式绑定到 GET 路由中的视图。这是因为Laravel将始终检查会话数据中的错误,并自动将它们绑定到视图(如果可用)。