我一直试图收到一条Flash消息,让用户知道他们的联系表单中的消息已经成功发送,我发现This Github Repo在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 () {
//
});
答案 0 :(得分:1)
只需将flash:消息更改为Session :: put,并确保它们位于&#34; web&#34;中间件组。
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('/');
});
});