【问题标题】:Laravel session flash messages are not showing on production (Laravel Forge)Laravel 会话闪存消息未在生产中显示(Laravel Forge)
【发布时间】:2019-07-14 06:40:00
【问题描述】:

我的 production site 上没有显示 Flash 会话错误消息的 Laravel Blade 表单。它之前给出了 419 错误响应“抱歉,您的会话已过期。请刷新并重试。在生产 laravel 上”。我能够通过清除缓存和 composer dump-autoload 来清除表单提交。

这是显示会话的表单。在本地工作,我在 L5.7.9。

<form method="POST" action="{{ route('candidates.store') }}" class="form">
    @csrf

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

    @if (session('login_error'))
        <div class="alert alert-danger" role="alert">
            {{ session('login_error') }}
        </div>
    @endif

    @if ($errors->any())
        <div class="alert alert-danger" role="alert">
            Woops had some problems saving
        </div>
    @endif

在我的 .env 文件中,我有:

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_DOMAIN=employbl.com
QUEUE_DRIVER=sync

然后在我的控制器存储方法中:

public function store(Request $request)
{
    $validator = $request->validate([
        'first_name'         => 'required',
        'last_name'          => 'required',
        'email'              => 'email|required|unique:users',
        'linkedin_url'       => 'required|url',
        'phone_number'       => 'required',
        'work_authorization' => 'required',
        'city'               => 'required',
        'state'              => 'required'
    ]);

    $candidate         = new User($request->all());
    $candidate->active = false;
    $candidate->save();

    return redirect()->route('candidates.landing')->with('message', 'Successfully applied to join Employbl network!');
}

对于我没有路线组的路线:

Route::get('/candidates', 'CandidateController@landing')->name('candidates.landing');
Route::post('/candidates', 'CandidateController@store')->name('candidates.store');

php artisan route:list 显示我只使用了一次 web 中间件:

|        | POST     | candidates                                                                                 | candidates.store            | App\Http\Controllers\CandidateController@store                             | web                                                                                                                                                                               |

Flash 消息在本地工作,但是当我在生产环境(使用 Laravel Forge 部署)提交表单时,表单提交但没有显示 Flash 消息。我该怎么做才能使会话消息出现在生产环境中?为什么会这样?

【问题讨论】:

  • 请查看this帮助
  • 您是否还做了php artisan migrate 以在生产中拥有sessions如果您的会话驱动程序是database
  • 已更新以显示我的路线信息,而不是在那里发布。我在 prod 上的会话驱动程序是 file,所以没有会话表。使用会话表可能对此有所帮助吗?
  • 只是检查正常登录是否有效?喜欢不是闪存消息而是正常会话?可能是storage/framework/sessions 的文件权限。主要检查storage文件夹的权限?
  • 好的,我更新了数据库以使用会话表。当我提交表单时,会话消息仍然没有显示。不过,我可以很好地从应用程序登录和注销。使用 session db table 文件权限仍然是问题吗?

标签: php laravel session laravel-5


【解决方案1】:

事实证明,我需要运行 php artisan cache:clear 来删除现有的会话信息。更多信息在这里:https://laracasts.com/discuss/channels/forge/419-error-when-submitting-form-in-production-sorry-your-session-has-expired-please-refresh-and-try-again#reply=494157

【讨论】:

    【解决方案2】:

    这是问题吗?

         return redirect()
                ->route('candidates.landing')
                ->with('message', 'Successfully applied to join Employbl network!');
        // I think with takes [](array) so, 
         return redirect()
                ->route('candidates.landing')
                ->with(['message' => 'Successfully applied to join Employbl network!']);
      //
      //
      // But why are't you doing like this 
      //
      //
      return back()->with([
              'message' => 'Successfully applied to join Employbl network!'
            ]);
      //
      //
      //
      // And yes Session::has('message') or \Session::has('message') is a good way to check in the blade
    

    【讨论】:

      【解决方案3】:

      您缺少一些方法 (has) 来检查您是否有具有该名称的会话和 (get) 来获取值。

      
      @if(session::has('message'))
              <div class="alert alert-success">
                  {{ session::get('message') }}
              </div>
          @endif
      
          @if (session::has('login_error'))
              <div class="alert alert-danger" role="alert">
                  {{ session::get('login_error') }}
              </div>
          @endif
      
      
      
          @if ($errors->any())
              <div class="alert alert-danger" role="alert">
                  Woops had some problems saving
              </div>
          @endif
      

      【讨论】:

      • 嘿维达尔,这不是问题所在。使用我上面的会话助手很好:laravel.com/docs/5.7/session#using-the-session。它也可以在本地工作,所以我认为这与我服务器上的会话配置有关。谢谢你的意见
      • 知道了,我会在刀片上做一个 dd,看看我们在刀片模板上的会话是否有值。
      猜你喜欢
      • 2019-10-19
      • 2018-04-22
      • 2019-07-09
      • 2018-03-25
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多