我一直在尝试获取一条闪存消息,让用户知道他们来自联系表单的消息已成功发送,我找到了这个 Github 存储库,其中有一种"简单"的方式来在 Laravel 5 中闪烁错误消息(我使用的是 Laravel 5.2),所以我继续尝试使用它,但我似乎无法让它工作。
这些类都被找到了,这里的问题是我重定向后它不会闪烁。
在我的master.blade.php
@if (Session::has('flash_notification.message'))
<div class="alert alert-{{ Session::get('flash_notification.level') }}">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('flash_notification.message') }}
</div>
@endif
在我的routes.php
Route::post('sendemail', function () {
$data = array(
'name' => "Learning Laravel",
);
Mail::send('emails.welcome', $data, function ($message) {
$message->from('email@provider', 'Learning Laravel!');
$message->to('email@provider')->subject('Learning Laravel test email');
});
Flash::message('Thank you for contacting us, we will get back to you as soon as possible.');
return Redirect::to('/');
});
我做错了什么/忘记了什么?
编辑完成routes.php
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('index');
});
Route::post('sendemail', function () {
$data = array(
'name' => "Learning Laravel",
);
Mail::send('emails.welcome', $data, function ($message) {
$message->from('email@provider', 'Learning Laravel!');
$message->to('email@provider')->subject('Learning Laravel test email');
});
Session::put('flash_notification.message', 'Thank you for contacting us, we will get back to you as soon as possible.');
return Redirect::to('/');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
只需将 flash:message 更改为 Session::p ut,并确保它们位于"web"中间件组下。
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('index');
});
Route::post('sendemail', function () {
$data = array(
'name' => "Learning Laravel",
);
Mail::send('emails.welcome', $data, function ($message) {
$message->from('email@provider', 'Learning Laravel!');
$message->to('email@provider')->subject('Learning Laravel test email');
});
Session::put('flash_notification.message', 'Thank you for contacting us, we will get back to you as soon as possible.');
return Redirect::to('/');
});
});