简体   繁体   English

Laravel 5中闪烁的反馈消息

[英]Flashing feedback message in Laravel 5

I want to flash feedback messages like "An entry has been created successfully" or "You don't have enough permissions to access this item" . 我想刷新诸如“条目已成功创建”“您没有足够的权限来访问此项目”之类的反馈消息。

I want to avoid embedding these messages straight into views, because they can re-appear, for example, when a user navigates back to the previous page with his browser. 我想避免将这些消息直接嵌入视图中,因为例如当用户使用浏览器导航回上一页时,它们可能会重新出现。

To solve this problem, I have a javascript function that acts like following: 为了解决这个问题,我有一个javascript函数 ,其功能如下:

(showFlashMessage(){
    // 1. make an ajax request
    // 2. retrieve a flash message (if any)
    // 3. display the message
})();

This way, the problem of the reappearance of the flash messages is solved. 这样,解决了闪存消息再次出现的问题。 However, by the time the request is made, the flash message has already disappeared. 但是,在发出请求时,Flash消息已经消失。 How should I solve this problem? 我应该如何解决这个问题?

If you want to solve the problem on the server side you can make use of the Session facade like so: 如果要在服务器端解决问题,可以使用Session外观,如下所示:

In your controller: 在您的控制器中:

function create() {
    if(Gate::allows('createAccess')) {
        Session::flash('error', 'You don't have enough permissions to access this item!');

        return redirect()->back();
    }

    // Create the model or perform any other logic

    Session::flash('success', 'An entity has been created successfully!');

    return view('entities.show');
}

Then you can have a partial named message.blade.php which is included in your entities.show view: 然后,您可以有一个名为message.blade.php的部分名称,它包含在您的entities.show视图中:

@if (Session::has('message'))
    <div class="alert alert-info">
        <p>{{ Session::get('message') }}</p>
    </div>
@endif

If you want to solve the problem on the client side you can call your message function (for example sweet alert message) in the success callback when the request has been completed successfully: 如果要在客户端解决问题,可以在成功完成请求后在成功回调中调用消息函数(例如,甜蜜警报消息):

function showMessage() {
    $.ajax({
        url: 'your-url',
        data: {
            // Your data here
        },
        success: function() {
            // Show success message
        },
        error: function() {
            // Show error message
        }
    });
}

I would suggest keeping it simple and call the inbuilt flash() method in your controller: 我建议保持简单,并在控制器中调用内置的flash()方法:

flash()->error('We encountered an error whilst doing something');
return redirect('path') //action(), back(), etc;

By my understanding, Laravel handles these flash messages in the session and they persist only for the next page view before being removed. 据我了解,Laravel在会话中处理这些Flash消息,它们仅在下一个页面视图中保留,然后再被删除。 If you want to reuse a specific message or combination of message type and content, consider storing this in either a DB table or in helper.php 如果要重用特定消息或消息类型和内容的组合,请考虑将其存储在数据库表或helper.php

Another option is to pull in the Laracasts/Flash/Flash package 另一个选择是拉入Laracasts/Flash/Flash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM