$ request->会话在Laravel 5.3资源控制器中不起作用 [英] $request->session didn't work in Laravel 5.3 resource controller

查看:93
本文介绍了$ request->会话在Laravel 5.3资源控制器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经检查了其他控制器(而不是资源控制器)中的会话是否运行良好,但是当我在资源控制器中执行该操作时,获取会话的代码无法正常工作.

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.

这是我的资源控制者

  <?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;
    }

还有更多的公共功能,但是它仍然是空的,因此在这里我不展示它以避免混乱.这是我在web.php中的路线:

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:

Route::group(['namespace' => 'Admin'],function(){

    Route::resource('/admin/mahasiswa','MahasiswaController');
    Route::resource('/admin/nilai','NilaiController');

});

推荐答案

5.3中,中间件尚未在constructor中运行,因此您无法收集session数据.但是,使用基于闭包的方法,您应该可以通过以下方式访问它:

In 5.3 the middleware hasn't run yet in the constructor, so you're unable to gather session data. 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);
});

这篇关于$ request-&gt;会话在Laravel 5.3资源控制器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆