【问题标题】:Passing variable when redirecting to a page重定向到页面时传递变量
【发布时间】:2015-03-20 05:48:36
【问题描述】:

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

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

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

    Session::get('sent');
    

    这里是Laravel doc

    医生说,

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


    withErrors 的工作原理。 这是Doc

    它说,

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

    【讨论】:

    • 嗯,好的。我想知道为什么-&gt;withErrors($validator)-&gt;withInput(); 在重定向时会起作用但不能使用自己的变量?
    • 您会在控制器(GET 方法)还是视图中使用 Session::get('sent')?
    • 就个人而言,我在视图中查看了我的Session::get("whatever")
    • blade view 中,我更新了与withErrors 相关的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多