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.

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

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?