如何在 Laravel 包开发中设置和获取会话数据

How to Set & Get Session Data in Laravel Package Development

我正在 Laravel 中开发一个带有联系表的程序包。我正在尝试设置和获取会话数据,但它不适用于以下内容。

查看

@if(session('success'))
    <div class="alert alert-success alert-dismissible">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
        {{session('success')}}
    </div>
@endif

控制器

public function store(Request $request)
{
    //Validation
    $request->validate([
        'email'=>'required|max:50|unique:contact_forms,email'
    ]);

    //Data
    $contact_form = new ContactForm();
    $contact_form->full_name = $request->full_name;
    $contact_form->mobile = $request->mobile;
    $contact_form->email = $request->email;

    //Save
    $contact_form->save();

    //Return back
    return back()->with('success','Record inserted successfully');
}

路线

Route::group(['namespace' => 'W3public\ContactForm\Http\Controllers'], function () {
    Route::get('contact-us', 'ContactFormController@index');
    Route::post('contact-us', 'ContactFormController@store')->name('contact-us');
});

如何在 Laravel 的包开发中 set/get 会话数据?在此先感谢。

执行此操作的最佳方法是 Flash 会话消息

重定向为

$request->session()->flash('alert-success', 'Record inserted successfully!');

并在您的视图文件中

<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
  @if(Session::has('alert-' . $msg))

  <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></p>
  @endif
@endforeach