【问题标题】:Session::flash('success', 'Data has been saved successfully');Session::flash('success', '数据保存成功');
【发布时间】:2016-09-10 03:05:03
【问题描述】:

我正在使用以下代码来显示 Flash 消息,但它不起作用。

后控制器

public function store(Request $request){
$this->validate($request,array(
'title'=>'required|max:255',
'slug'  =>'required|alpha_dash|min:5|max:255|unique:posts,slug',
'body'  =>'required'
 ));

    //store in the database

    $post = new Post;
    $post->title = $request->title;
    $post->slug  = $request->slug;
    $post->body = $request->body;

    $post->save();

    //This code will generate flash message about success or failure about data insert

     Session::flash('success', 'Data has been saved successfully!');

    //Redirect to another page
    return redirect()->route('posts.show', $post->id);

}

然后使用下面的代码来显示它:

message.blade.php

@if(Session::has('success'))
    <div class="alert alert-success" role= "alert">
        <strong>Successful:</strong>
            {{ Session::get('success') }} 
    </div>
@endif
@if(count($errors) > 0)
    <div class="alert alert-danger" role="alert">
        <strong>Errors:</strong>
            <ul>
                @foreach($errors as $error)
                    <li>  {{ $error }} </li>
                @endforeach
            </ul>
    </div>
@endif

上面的代码没有显示任何 Flash 消息。但当 Session::flash('success', '数据保存成功!'); 写成: Session::put('success', '数据保存成功!');

闪烁的信息显示出来,并没有消失。

routes.php 是:

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

Route::get('auth/login', ['as' =>'login', 'uses'=>
     'Auth\AuthController@getLogin']);
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', ['as' => 'logout', 'uses' =>
    'Auth\AuthController@getLogout']);


Route::get('auth/register','Auth\AuthController@getRegister');
Route::post('auth/register','Auth\AuthController@postRegister');


Route::get('password/reset/{token?}',
    'Auth\PasswordController@showResetForm');
Route::post('password/email',
    'Auth\PasswordController@sendResetLinkEmail');
Route::post('password/reset','Auth\PasswordController@reset');

Route::get('contact', 'PagesController@getContact');
Route::get('about', 'PagesController@getAbout');
Route::get('/', 'PagesController@getIndex');


Route::get('reader/{slug}', ['as' => 'reader.single', 'uses' =>
    'ReaderController@getSingle'])
     ->where('slug', '[\w\d\-\_]+');


Route::get('reader', ['as' => 'reader.index', 'uses' =>
    'ReaderController@getIndex' ]);

Route::resource('posts', 'PostController');
});

请帮忙!

【问题讨论】:

  • 如果你使用 Laravel >= 5.2.27,你需要从你的 routes.php 文件中删除 web 中间件组。他们对其进行了更新,因此路由文件会自动包装在 web 中间件组中,因此如果您将其放入您的路由文件中,它会运行网络中间件两次,这会混淆您的会话数据。
  • 这已经成功了。非常感谢。

标签: laravel


【解决方案1】:

1.- 确保你的视图中包含消息模板,在这种情况下是你的 SHOW 视图 2.- 尝试为此替换您的消息模板:

@if(Session::has('success'))
    <div class="alert alert-success" role= "alert">
        <strong>Successful:</strong>
            {!! session('success') !!}
    </div>
@endif
@if(count($errors) > 0)
    <div class="alert alert-danger" role="alert">
        <strong>Errors:</strong>
            <ul>
                @foreach($errors as $error)
                    <li>  {{ $error }} </li>
                @endforeach
            </ul>
    </div>
@endif

看,我正在使用 {!! session('success') !!} 而不是你的

【讨论】:

  • 您的建议已经被证明是成功的。它在web中间件组被移除时有效。非常感谢。
猜你喜欢
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 2011-09-22
相关资源
最近更新 更多