【问题标题】:Laravel 5 laracasts flash messaging not showing error divLaravel 5 laracasts flash 消息没有显示错误 div
【发布时间】:2016-06-14 12:40:42
【问题描述】:

我一直在尝试获取一条闪烁消息,让用户知道他们的联系表单中的消息已成功发送,我发现 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">&times;</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 () {
    //
});

【问题讨论】:

    标签: php laravel laravel-5 flash-message


    【解决方案1】:

    只需将 flash:message 更改为 Session::put 并确保它们位于“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('/');
    
        });
    
    
    });
    

    【讨论】:

    • 很遗憾,这不起作用,它仍然没有显示&lt;div&gt;
    • 如果你的主路由在 web 中间件下?或任何保持会话的中间件?
    • 我不知道,这是 Laravel 5 的全新安装
    • 嗯,“发送邮件”路线怎么样?为了使会话正常工作,需要将它们放置在 Web 中间件中。
    • 我对 laravel 比较陌生,所以我基本上不知道如何让表单提交到中间件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2016-02-15
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2017-08-14
    • 2016-09-29
    相关资源
    最近更新 更多