Php 重定向到页面时传递变量
我已经设计了一个带有输入验证的联系人页面,如果验证成功通过,则将发送电子邮件。我在发送电子邮件后遇到问题,$sent变量没有传递到blade视图以确认用户 在contact.blade.php文件中Php 重定向到页面时传递变量,php,laravel,laravel-4,Php,Laravel,Laravel 4,我已经设计了一个带有输入验证的联系人页面,如果验证成功通过,则将发送电子邮件。我在发送电子邮件后遇到问题,$sent变量没有传递到blade视图以确认用户 在contact.blade.php文件中 @if (isset($sent) && $sent == true) <p>Your details have been successfully send to us we will contact you shortly.</p> @endif 在控制
@if (isset($sent) && $sent == true)
<p>Your details have been successfully send to us we will contact you shortly.</p>
@endif
在控制器中,当用户按下提交按钮(POST)时
发送电子邮件后,它将重定向回
/contact
页面,但带有('sent',true)代码>未通过?要获取数据,请使用典型的会话::get()代码>
这是你的电话号码
博士说
注意:由于with方法将数据闪烁到会话,因此可以使用典型的session::get方法检索数据
威瑟罗的工作原理。
这是你的电话号码
上面说,
但是,请注意,我们不必显式地将错误消息绑定到GET路由中的视图。这是因为Laravel将始终检查会话数据中的错误,并自动将它们绑定到视图(如果可用)
啊好的。我想知道为什么->withErrors($validator)->withInput()代码>在重定向时可以工作,但不能使用自己的变量?您是在控制器(get方法)中使用Session::get('sent')还是在视图中使用Session::get('sent')?我个人在a视图中检查我的Session::get(“whatever”)
。在blade视图中,我更新了关于withErrors
的答案。
public function showContact()
{
$data = [];
return View::make('contact')->withData($data);
}
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);
}
Session::get('sent');