laravel - 无法在控制器构造函数中获取会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41542802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel - Can't get session in controller constructor
提问by gogagubi
In new laravel I can't get session in constructor. Why?
在新的 Laravel 中,我无法在构造函数中获取会话。为什么?
public function __construct()
{
dd(Session::all()); //this is empty array
}
and then below
然后在下面
public function index()
{
dd(Session::all()); //here works
}
In old laravel i remember there was not this problem. something changed?
在旧的 Laravel 中,我记得没有这个问题。有什么改变吗?
回答by Robin Dirksen
You can't do it by default with Laravel 5.3. But when you edit you Kernel.php
and change protected $middleware = [];
to the following it wil work.
默认情况下,Laravel 5.3 无法执行此操作。但是当您编辑您Kernel.php
并更改protected $middleware = [];
为以下内容时,它将起作用。
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
Hope this works!
希望这有效!
回答by Kamlesh
Laravel 5.7 solution
Laravel 5.7 解决方案
public function __construct()
{
$this->middleware(function ($request, $next) {
// fetch session and use it in entire class with constructor
$this->cart_info = session()->get('custom_cart_information');
return $next($request);
});
}
If you want to use constructor for any other functionality or query or data then do all the work in $this->middleware function, NOT outside of this. If you do so it will not work in all the functions of entire class.
如果您想将构造函数用于任何其他功能或查询或数据,则在 $this->middleware 函数中完成所有工作,而不是在此之外。如果您这样做,它将无法在整个班级的所有功能中发挥作用。
回答by Samy Massoud
As of other answers no out of the box solution for it. But you still can access it using Middleware in constructor.
至于其他答案,没有开箱即用的解决方案。但是您仍然可以在构造函数中使用中间件访问它。
So here is another hack
所以这是另一个黑客
public function __construct(){
//No session access from constructor work arround
$this->middleware(function ($request, $next){
$user_id = session('user_id');
return $next($request);
});
}
回答by Alexey Mezenin
In Laravel 5.3 sessions related functionality will not work in the a controller constructor, so you should move all sessions related logic to methods.
在Laravel 5.3 中,与会话相关的功能在控制器构造函数中不起作用,因此您应该将所有与会话相关的逻辑移动到方法中。
回答by Muhammad Luqman
*This one solved mine problem to use session in constructor*
$this->middleware(function ($request, $next) {
if (!session('records_per_page')) {
session(['records_per_page' => 20]);
}
// update rows per page
if (!empty(\Request::get('records_per_page')) && in_array(\Request::get('records_per_page'), [20, 50, 80, 100])) {
session(['records_per_page' => \Request::get('records_per_page')]);
}
return $next($request);
});
回答by Bablu Mazumder
I used Session in the construct using middleware, You can try, It would be helpful
我在使用中间件的构造中使用了 Session,您可以尝试,它会有所帮助
public function __construct()
{
$this->middleware('PM');
$this->middleware(function ($request, $next){
$school_id = Session::get('school_id');
return $next($request);
});
}
回答by Arvind K.
As of Laravel 6.18.1, add these to $middleware
array in the kernel.php
从 Laravel 6.18.1 开始,将这些添加到$middleware
kernel.php 中的数组
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
And it should work.
它应该工作。
回答by Olamide
If anyone is coming across this question in now, instead of accessing session values in controller constructors, I will suggest creating a middleware and placing it in the Web middleware section in the App\Http\Kernel
.
如果现在有人遇到这个问题,我建议不要在控制器构造函数中访问会话值,而是建议创建一个中间件并将其放在App\Http\Kernel
.
This is because sessions are not part of the requests that are passed to Controller constructors. To then do any session value checks or manipulation in every controller, it is safer to create a middleware.
这是因为会话不是传递给控制器构造函数的请求的一部分。然后在每个控制器中进行任何会话值检查或操作,创建中间件更安全。
This is where session values can be accessed as part of the HTTP request going into the route.
这是可以作为进入路由的 HTTP 请求的一部分访问会话值的地方。
`/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
...,
...,
...,
...,
...,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\MySpecialMiddleWareToAccessSessionVariables::class
],
'api' => [
'throttle:60,1',
'bindings',
],
];`
This is because \Illuminate\Session\Middleware\StartSession
has started the session before it hits your routes
这是因为\Illuminate\Session\Middleware\StartSession
在到达您的路线之前已开始会话