重定向到页面时传递变量

时间:2015-01-21 16:10:36

标签: php laravel laravel-4

我设计了一个带有输入验证的联系页面,如果验证成功通过,那么将发送电子邮件。电子邮件发送后我遇到问题,$ 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);未通过?

1 个答案:

答案 0 :(得分:1)

使用典型的Session::get();

来获取数据
Session::get('sent');

这是Laravel doc

doc说,

  

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


withErrors如何运作。 这是Doc

它说,

  

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

相关问题