BaseController 中的会话变量问题
Session variable Issue in BaseController
问题
Session::get
无法在 Base Controller 中工作
以下情况未显示正确的会话值
登录控制器
class LoginController extends \App\Http\Controllers\Web\BaseController
{
public function Login() {
return View("UserManagement.Auth.Login.login");
}
}
基地控制器
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");
}
}
}
以下情况显示正确的会话值
基地控制器
class BaseController extends 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");
}
}
这里的问题是,我必须在许多控制器中使用Base Controller。有什么方法可以使会话在 Base Controller 中工作?
根据以下 URL,您无法再在 Laravel 5.3 中的控制器的 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);
});
}
}
这是可行的,因为您的控制器只是在稍后为 运行 定义一个中间件,之后会话可用。
它在您的第二个示例中起作用的原因是您在控制器方法中访问会话。届时会话可用,因为中间件将具有 运行.
问题
Session::get
无法在 Base Controller 中工作
以下情况未显示正确的会话值
登录控制器
class LoginController extends \App\Http\Controllers\Web\BaseController
{
public function Login() {
return View("UserManagement.Auth.Login.login");
}
}
基地控制器
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");
}
}
}
以下情况显示正确的会话值
基地控制器
class BaseController extends 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");
}
}
这里的问题是,我必须在许多控制器中使用Base Controller。有什么方法可以使会话在 Base Controller 中工作?
根据以下 URL,您无法再在 Laravel 5.3 中的控制器的 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);
});
}
}
这是可行的,因为您的控制器只是在稍后为 运行 定义一个中间件,之后会话可用。
它在您的第二个示例中起作用的原因是您在控制器方法中访问会话。届时会话可用,因为中间件将具有 运行.