Laravel 5.2 重定向成功消息

Laravel 5.2 redirect back with success message(Laravel 5.2 重定向成功消息)
本文介绍了Laravel 5.2 重定向成功消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

Route::group(['middleware' => 'web'], function () {路线::认证();路由::get('/', function () {返回视图('家');});Route::post('/newsletter/subscribe','NewsletterController@subscribe');});

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

解决方案

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

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

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

如果存在则显示消息:

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

@万一

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

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

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

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:

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?

解决方案

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

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

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

这篇关于Laravel 5.2 重定向成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)