Laravel 5.3:会话在刷新页面后销毁值

时间:2017-04-05 05:33:49

标签: php laravel session laravel-5.3

我正在尝试将一些数据存储在Laravel的负载基础控制器上,我使用constructor来执行此操作并且构造函数成功执行。但是,当我在会话中存储值,然后在下次显示null的相同会话值时刷新页面:

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Language;
use Session;
class Controller extends BaseController
{
   use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    function __construct()
    {
        if(Session::get('phrase') == null)
        {
            Session::put('phrase','test');
            dump(Session::get('phrase'));
        }
    }
}

访问:

class DashboardController extends Controller
{
  public function index()
  {
     if(!in_array(Auth::user()->role_id,[1,2])){
         return redirect()->route('orsers.list',['filters'=>'']);
     }
     return view('dashboard.index');
  }

我无法理解我错在哪里?

1 个答案:

答案 0 :(得分:1)

在你的app / Http / Kernel.php重新排列以下middlwaregroups ......

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

如果您的$ middlewareGroups已按此订购,我们可以进一步调试。