php Laravel 5.2 重定向成功消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37376168/
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-08-26 01:12:34  来源:igfitidea点击:

Laravel 5.2 redirect back with success message

phplaravellaravel-5.2

提问by Stefan

I'm trying to get a success message back to my home page on laravel.

我正在尝试将成功消息返回到我在 laravel 上的主页。

return redirect()->back()->withSuccess('IT WORKS!');

For some reason the variable $success doesn't get any value after running this code.

出于某种原因,运行此代码后变量 $success 没有得到任何值。

The code I'm using to display the succes message:

我用来显示成功消息的代码:

@if (!empty($success))
    <h1>{{$success}}</h1>
@endif

I have added the home and newsletter page to the web middleware group in routes.php like this:

我已将主页和时事通讯页面添加到 routes.php 中的 Web 中间件组,如下所示:

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/', function () {
        return view('home');
    });

    Route::post('/newsletter/subscribe','NewsletterController@subscribe');
});

Does anyone have any idea why this doesn't seem to work?

有谁知道为什么这似乎不起作用?

回答by Alexey Mezenin

You should remove webmiddleware from routes.php. Adding webmiddleware manually causes session and request related problems in Laravel 5.2.27 and higher.

您应该webroutes.php. 在 Laravel 5.2.27 及更高版本中web手动添加中间件会导致会话和请求相关问题

If it didn't help (still, keep routes.phpwithout web middleware), you can try little bit different approach:

如果它没有帮助(仍然,保持routes.php没有网络中间件),您可以尝试一些不同的方法:

return redirect()->back()->with('message', 'IT WORKS!');

Displaying message if it exists:

如果存在则显示消息:

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

回答by Mohammad Rajabloo

you can use this :

你可以使用这个:

return redirect()->back()->withSuccess('IT WORKS!');

and use this in your view :

并在您看来使用它:

@if(session('success'))
    <h1>{{session('success')}}</h1>
@endif

回答by Alex Sholom

Controller:

控制器:

return redirect()->route('subscriptions.index')->withSuccess(['Success Message here!']);

Blade

刀刃

@if (session()->has('success'))
    <div class="alert alert-success">
        @if(is_array(session()->get('success')))
        <ul>
            @foreach (session()->get('success') as $message)
                <li>{{ $message }}</li>
            @endforeach
        </ul>
        @else
            {{ session()->get('success') }}
        @endif
    </div>
@endif

You can always save this part as separate blade file and include it easily. fore example:

您始终可以将此部分另存为单独的刀片文件并轻松包含在内。例如:

<div class="row">
        <div class="col-md-6">
            @include('admin.system.success')
            <div class="box box-widget">

回答by joy

You can simply use back()function to redirect no need to use redirect()->back()make sure you are using 5.2 or greater than 5.2 version.

您可以简单地使用back()函数进行重定向,无需使用redirect()->back()确保您使用的是 5.2 或高于 5.2 的版本。

You can replace your code to below code.

您可以将您的代码替换为以下代码。

return back()->with('message', 'WORKS!');

In the view file replace below code.

在视图文件中替换下面的代码。

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

For more detail, you can read here

有关更多详细信息,您可以在这里阅读

back()is just a helper function. It's doing the same thing as redirect()->back()

back()只是一个辅助函数。它在做同样的事情redirect()->back()

回答by Laravellous Martins

All of the above are correct, but try this straight one-liner:

以上所有都是正确的,但试试这个直线单行:

{{session()->has('message') ? session()->get('message') : ''}}

{{session()->has('message') ? session()->get('message') : ''}}

回答by Radames E. Hernandez

One way to do that is sending the message in the session like this:

一种方法是在会话中发送消息,如下所示:

Controller:

控制器:

return redirect()->back()->with('success', 'IT WORKS!');

View:

查看

@if (session()->has('success'))
    <h1>{{ session('success') }}</h1>
@endif

And other way to do that is just creating the session and put the text in the view directly:

另一种方法是创建会话并将文本直接放在视图中:

Controller:

控制器

return redirect()->back()->with('success', true);

View:

查看

@if (session()->has('success'))
    <h1>IT WORKS!</h1>
@endif

You can check the full documentation here: Redirecting With Flashed Session Data

您可以在此处查看完整文档:Redirecting With Flashed Session Data

I hope it is very helpful, regards.

我希望它非常有帮助,问候。

回答by Genius in trouble

In Controller

在控制器中

return redirect()->route('company')->with('update', 'Content has been updated successfully!');

In view

在视图中

@if (session('update'))
  <div class="alert alert-success alert-dismissable custom-success-box" style="margin: 15px;">
     <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
     <strong> {{ session('update') }} </strong>
  </div>
@endif

回答by Adiyya Tadikamalla

You can use laravel MessageBag to add our own messages to existing messages.

您可以使用 laravel MessageBag 将我们自己的消息添加到现有消息中。

To use MessageBag you need to use:

要使用 MessageBag,您需要使用:

use Illuminate\Support\MessageBag;

In the controller:

在控制器中:

MessageBag $message_bag

$message_bag->add('message', trans('auth.confirmation-success'));

return redirect('login')->withSuccess($message_bag);

Hope it will help some one.

希望它会帮助某人。

  • Adi
  • 阿迪