重定向到页面时传递变量
Passing variable when redirecting to a page
我设计了一个带有输入验证的联系页面,如果验证成功通过,则会发送电子邮件。我在发送电子邮件后遇到问题,$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
在控制器中,显示一个 /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');
这里是 Laravel doc
医生说,
Note: Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.
withErrors
的工作原理。
这是 Doc
它说,
However, notice that we do not have to explicitly bind the error messages to the view in our GET route. This is because Laravel will always check for errors in the session data, and automatically bind them to the view if they are available.
我设计了一个带有输入验证的联系页面,如果验证成功通过,则会发送电子邮件。我在发送电子邮件后遇到问题,$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
在控制器中,显示一个 /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');
这里是 Laravel doc
医生说,
Note: Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.
withErrors
的工作原理。
这是 Doc
它说,
However, notice that we do not have to explicitly bind the error messages to the view in our GET route. This is because Laravel will always check for errors in the session data, and automatically bind them to the view if they are available.