Laravel 4 如何在视图中显示 Flash 消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17527182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:32:29  来源:igfitidea点击:

Laravel 4 how to display flash message in view?

sessionlaravellaravel-4

提问by Morne Zeelie

I'm trying to get my flash message to display.

我正在尝试显示我的 Flash 消息。

This is in my routing file

这是在我的路由文件中

Route::post('users/groups/save', function(){

return Redirect::to('users/groups')->withInput()->with('success', 'Group Created Successfully.');

});

This is in my view

这在我看来

{{ $success = Session::get('success') }}
@if($success)
    <div class="alert-box success">
        <h2>{{ $success }}</h2>
    </div>
@endif

But nothing is working.

但没有任何效果。

When I try this, I get an error Variable $success is undefined. But it actually shows the flash message too.

当我尝试这个时,我收到一个错误变量 $success 未定义。但它实际上也显示了 flash 消息。

{{ Session::get('success') }}
@if($success)
    <div class="alert-box success">
        <h2>{{ $success }}</h2>
    </div>
@endif

回答by Andreyco

This works for me

这对我有用

@if(Session::has('success'))
    <div class="alert-box success">
        <h2>{{ Session::get('success') }}</h2>
    </div>
@endif

回答by Muthamizhchelvan. V

if you are using bootstrap-3try the script below for Alert Style

如果您使用的是bootstrap-3,请尝试以下脚本以获取警报样式

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

回答by Milleus88

two methods:

两种方法:

Method 1 - if you're using

方法 1 - 如果您使用

return Redirect::to('users/groups')->withInput()->with('success', 'Group Created Successfully.');

under your controller create(), add in

在您的控制器 create() 下,添加

$success = Session::get('success');
return View::make('viewfile')->with('success', $success);

then on view page,

然后在查看页面,

@if (isset($success))
{{$success }}
@endif

What's happening in method 1 is that you're creating a variable $success that's passed into your create(), but it has no way of display $success. isset will always fail unless you set a variable to get the message and return it.

方法 1 中发生的事情是您正在创建一个变量 $success,该变量已传递到您的 create() 中,但它无法显示 $success。除非您设置一个变量来获取消息并返回它,否则 isset 将始终失败。

Method 2 - use return Redirect withFlashMessage

方法 2 - 使用 return Redirect withFlashMessage

return Redirect::route('users/groups')->withFlashMessage('Group Created Successfully.');

then on your view page,

然后在您的查看页面上,

@if (Session::has('flash_message'))
{{ Session::get('flash_message') }}
@endif

Method 2 is much cleaner and does not require additional code under create().

方法 2 更简洁,不需要在 create() 下添加额外的代码。

回答by Trying Tobemyself

when you set variable or message using ->with()it doesn't set the variable/message in the session flash, rather it creates an variable which is available in your view, so in your case just use $successinstead of Session::get('success')

当您使用->with()它设置变量或消息时,不会在会话闪存中设置变量/消息,而是创建一个在您的视图中可用的变量,因此在您的情况下,只需使用$success而不是Session::get('success')

And in case you want to set the message in the session flash the use this Session::flash('key', 'value');but remember with session flash the data is available only for next request.

如果您想在会话闪存中设置消息,请使用此选项,Session::flash('key', 'value');但请记住,使用会话闪存,数据仅可用于下一个请求。

Otherwise you can use Session::put('key', 'value');to store in session

否则,您可以使用Session::put('key', 'value');存储在会话中

for more info read here

欲了解更多信息,请阅读此处

回答by brainless

{{ Session::get('success') }}

This just echos the session variable 'success'. So when you use

这只是呼应会话变量“成功”。所以当你使用

{{ Session::get('success') }} @if($success) <div class="alert-box success"> <h2>{{ $success }}</h2> </div> @endif

{{ Session::get('success') }} @if($success) <div class="alert-box success"> <h2>{{ $success }}</h2> </div> @endif

you are seeing it's output along with the error of the next statement. Because with() function only sets the value in Session and will not set as a variable. Hence @if($success)will result in undefined variable error.

您会看到它的输出以及下一条语句的错误。因为 with() 函数只设置 Session 中的值,不会设置为变量。因此@if($success)将导致未定义的变量错误。

As @Andreyco said,

正如@Andreyco 所说,

@if(Session::has('success'))
<div class="alert-box success">
    <h2>{{ Session::get('success') }}</h2>
</div>
@endif

This should work.

这应该有效。

The reason you are not seeing it might be because the action you are performing is not success. And this does not require you to either reinstall xampp or modify php.ini.

您没有看到它的原因可能是因为您正在执行的操作不成功。这不需要您重新安装 xampp 或修改 php.ini。

回答by foxfarrier

Laravel 4.2 Personally i use

Laravel 4.2 我个人使用

Session::flash('key', 'value');
return Redirect::to('some/url');

then in the view id first check if there is a session of that key in the view

然后在视图 id 首先检查视图中是否有该键的会话

@if(Session::has('key'))
   {{Session::get('key')}} //this prints out the message or your 'value' in the session::flash method
@endif

it works for me most of the time and i usually have that blade template integrated into my view just so i can push success messages to the view from my codes.

它大部分时间都对我有用,而且我通常将刀片模板集成到我的视图中,这样我就可以将成功消息从我的代码推送到视图。

please do note that it is stated in the documentation that "Sometimes you may wish to store items in the session only for the next request. You may do so using the Session::flash method" so yes it expires after the next page.

请注意,文档中指出“有时您可能希望仅为下一个请求在会话中存储项目。您可以使用 Session::flash 方法这样做”,所以是的,它会在下一页后过期。

hope this helps

希望这可以帮助

回答by Big Tree

i just realized in using the Redirect::to(), when you use the withInput() method, chaining a with() function to pass variables will not work. the only way is either you flash your inputs separately using Input::flash(), and use the with() to pass your variables or you pass your variable via session using Session::flash('key','val') and retrieve in the view via session::get('key').

我刚刚在使用 Redirect::to() 时意识到,当您使用 withInput() 方法时,链接 with() 函数来传递变量将不起作用。唯一的方法是要么使用 Input::flash() 分别刷新输入,并使用 with() 传递变量,要么使用 Session::flash('key','val') 和通过会话传递变量通过 session::get('key') 在视图中检索。

回答by funguy

This link describes how to do this http://vegibit.com/flash-messages-in-laravel/

此链接描述了如何执行此操作http://vegibit.com/flash-messages-in-laravel/

Just tried with laravel 5 - works to me.

刚刚尝试使用 laravel 5 - 对我有用。

回答by Abdullah Ibn Farouk

this should work at localhost and on host and i tried it.

这应该在本地主机和主机上工作,我试过了。

<?php $success = Session::get('success'); ?>
@if($success)
    <div class="alert-box success">
        <h2>{{ $success }}</h2>
    </div>
@endif

回答by Abdul Rehman Ch

Inside of the routes.php file try to create your routes within the

在 routes.php 文件中尝试在

Route::group(['middleware' => ['web']], function () {
    //routes here
}

then use

然后使用

@if(Session::has('success'))
   <div class="alert-box success">
     <h2>{{ Session::get('success') }}</h2>
   </div>
@endif