doupin5667 2017-02-08 08:32
浏览 195
已采纳

会话变量BaseController中的问题

Problem

Session::get not working in Base Controller

Below case does not shows correct session value

Login Controller

class LoginController extends \App\Http\Controllers\Web\BaseController
{
    public function Login() {
        return View("UserManagement.Auth.Login.login");
    }
}

Base Controller

class BaseController extends Controller
{
    public function __construct() {
        if(\Session::get("CurrentLanguage") != null) {
            dd('here');
            \App::setLocale(\Session::get("CurrentLanguage"));
        }
        else {
            dd('here1');
            \Session::put("CurrentLanguage", "en");
            \App::setLocale("en");
        }
    }
}

Below case shows correct session value

Base Controller

class BaseController extends Controller
{

}

Login Controller

class LoginController extends \App\Http\Controllers\Web\BaseController
{
    public function Login() {
        if(\Session::get("CurrentLanguage") != null) {
            dd('here');
            \App::setLocale(\Session::get("CurrentLanguage"));
        }
        else {
            dd('here1');
            \Session::put("CurrentLanguage", "en");
            \App::setLocale("en");
        }
        return View("UserManagement.Auth.Login.login");
    }
}

Here the problem is, I have to use Base Controller in many controllers. Is there any way to make the session work in Base Controller?

  • 写回答

1条回答 默认 最新

  • duanmuyao0463 2017-02-08 09:15
    关注

    According to the following URL, you are no longer able to use the session in the constructor of a controller in Laravel 5.3. This is because at the point in time that your controller is constructed the middleware that deals with the session has not yet run. Apparently, it was never an intended feature to be able to access the session in the controller. Since this affected sessions, you will not be able to access the authenticated user in the controller's constructor either.

    A way around this however is to use a closure based middleware in your constructor.

    class BaseController extends Controller
    {
        public function __construct()
        {
            $this->middleware(function ($request, $next) {
                if(\Session::get("CurrentLanguage") != null) {
                    dd('here');
                    \App::setLocale(\Session::get("CurrentLanguage"));
                }
                else {
                    dd('here1');
                    \Session::put("CurrentLanguage", "en");
                    \App::setLocale("en");
                }
    
                return $next($request);
            });
        }
    }
    

    This works because your controller is simply defining a middleware to run at a later time after which the session is available.

    The reason it works in your second example is that you're accessing the session in a controller method. At that point in time the session is available because the middleware will have run.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)