[英]$request->session didn't work in Laravel 5.3 resource controller
First of all, I already check that in other controller (not in resource controller) my session work very well, but when I did it in the resource controller my code for get session didn't work. 首先,我已经检查了其他控制器(而不是资源控制器)中的会话是否运行良好,但是当我在资源控制器中执行此操作时,获取会话的代码无法正常工作。
Here's my resource controller 这是我的资源控制器
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//tambahan
use DB;
use Session;
//model
use App\_admins;
use App\Mahasiswas;
class MahasiswaController extends Controller
{
protected $data;
protected $token;
public function __contruct(){
$this->data = array();
$this->middleware(function ($request, $next) {
$this->token = $request->session()->get('_admin_id');
if (!$request->session()->has('_admin_id')) {
abort(404);
}
return $next($request);
});
}
private function user($token){
$this->data['query'] = _admins::find($token);
}
public function index(){
echo $this->token;
}
There is more public function, but it's still empty so I am not showing it here to avoid confusion. 还有更多的公共功能,但是它仍然是空的,因此在这里我不展示它以避免混乱。 And here is my route in web.php: 这是我在web.php中的路线:
Route::group(['namespace' => 'Admin'],function(){
Route::resource('/admin/mahasiswa','MahasiswaController');
Route::resource('/admin/nilai','NilaiController');
});
In 5.3
the middleware hasn't run yet in the constructor
, so you're unable to gather session
data. 在5.3
,中间件尚未在constructor
运行,因此您无法收集session
数据。 But using your closure-based approach, you should be able to access it with something like this: 但是,使用基于闭包的方法,您应该可以通过以下方式访问它:
$this->middleware(function($request, $next) {
// Get the session value (uses global helper)
$this->token = session('_admin_id');
// If the value is null, abort the request
if (null === $this->token) abort(404);
return $next($request);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.