在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/PHP/ Laravel 5.3以上session丟換問題

Laravel 5.3以上session丟換問題

這是一個全局方法,設置session的
clipboard.png
登陸時,用HomeController調用session_member存儲session
然后在首頁IndexController繼承Controller

clipboard.png

然后在HomeController里使用$this->data,數(shù)據(jù)為空,這是為什么?

clipboard.png

回答
編輯回答
懶洋洋

$member = session_member(); //這里輸出一下$member;
session_member();這個沒傳值就調用下面:
function session_member(member){

return $member = session('member', null); //session('member', null)返回什么值?
}
一步步輸出排查吧,另外能貼代碼嗎,貼圖代碼有錯也不想給你改

2018年9月8日 04:50
編輯回答
陌如玉

你確定不是代碼的問題嗎?

2017年10月15日 08:25
編輯回答
病癮

你調用的路由是否在 web 中間件里? (只有 web 中間建立的路由所調用的方法才會啟用 session

2018年5月15日 23:54
編輯回答
葬憶

原來我在 windows 上使用 php artisan serve 開發(fā)的時候也同樣遇到了這個問題,我通過在 Session::put() 之后添加了 Session::save() 方法,成功讀取到了數(shù)據(jù)。

所以,我換成了 Homestead 開發(fā)環(huán)境。

2017年8月27日 08:15
編輯回答
情已空

大家都在想辦法幫你解決問題,但是我覺得實現(xiàn)自身業(yè)務時的思路就是問題的。

從代碼邏輯來看,你是希望用戶信息存儲在 session 中方便存取,同時希望所有的 Controller 通過繼承后,能夠簡單的拿到常用的數(shù)據(jù)。

其實完全沒有必要性。

  1. 在試圖中要拿用戶可以直接 auth()->user()
  2. 要把數(shù)據(jù)共享給試圖,可以使用 View Composer
2017年11月30日 04:45
編輯回答
墨染殤

必須方路由中間里?。。?!他們說什么不符合邏輯,不安全!1年前的事情了

2017年7月20日 01:03
編輯回答
拮據(jù)

沒仔細去考究你的代碼,分析原因應該是版本問題,因為我之前貌似碰到過,后來為了便于管理,我單獨建了一個自定義的Session Facade,不過官方也給出了推薦的做法,我們來看看5.3的升級文檔對該問題的描述:

Session In The Constructor
In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet(你不能在控制器的構造方法中訪問session或auth,因為中間件還沒有運行).

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class ProjectController extends Controller
{
    /**
     * All of the current user's projects.
     */
    protected $projects;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->projects = Auth::user()->projects;

            return $next($request);
        });
    }
}

你也可以參考下面的文章:

https://josephsilber.com/post...

https://stackoverflow.com/que...

2017年3月5日 04:59
編輯回答
舊時光

這樣做真是多此一舉,直接在view模板里用Session或session取值即可.

2017年6月10日 16:28