我正在开发laravel 5.2项目并遇到会话问题。当我创建会话时,我只能从刀片模板访问它。如果我想从任何其他类访问它,那是不可能的。我正在使用Laravel 5.2,我的所有路由都在'web'中间件内。我搜索了整个网络,但没有一个提供我的问题的解决方案。
会话创建代码:
<?php namespace App\Lib\Services;
$this->request->session()->put('errors', $this->errors);`
redirect()->back()->send();
当我在下面的视图中测试会话时,它可以工作(提供预期的数据):
@extends('themes.backend.metronic.main')
@section('pageContent')
{{ dd(session ('errors')) }}
@endsection
但是,如果我想使用另一个类,如下所示,则返回null:
namespace App\Lib\Themes\Errors;
function __construct($specs)
{
parent::__construct($specs);
$this->setData();
dd(session('errors')); // this returns null value
$this->setErrorMsg();
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
由于您在类的构造函数中访问会话变量,因此可能是在创建会话变量之前创建了类。首先尝试设置会话变量,然后创建该类的新对象。