Laravel中间件将用户信息返回为null [英] Laravel Middleware return user info as null

查看:326
本文介绍了Laravel中间件将用户信息返回为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个中间控件,以防止用户不要插入或更新任何内容.我在Laravel 5.6上

I created a middle to prevent a user to not to insert or update anything. I am on Laravel 5.6

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
class LimitDemoUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $userId = Auth::id();
        if(request()->method() != "GET" && request()->method() != "HEAD" && $userId == 6) {
            abort(403);
        }
        return $next($request);
    }
}

我像这样(LimitDemoUser)将其注册到内核中

I registered it to kernel like this ( LimitDemoUser )

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
    \App\Http\Middleware\LimitDemoUser::class,

];

但是当我dd(Auth :: id());我得到的是null而不是gettin登录的用户ID

But when I dd(Auth::id()); I got null rather than gettin loggedin user id

我在这里想念什么?

我想我没有给路由中间件添加任何东西

I suppose I don't have o add anything to routemiddleware

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

];

我的LoginController在下面.也许有帮助

My LoginController is below. Maybe it may help

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/login';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function logout()
    {
        $this->guard()->logout();

        return redirect()->route('login');
    }

}

推荐答案

我在此页面中找到了解决方案. https://laracasts.com/discuss/channels/laravel/current-中间件用户

I found the solution in this page. https://laracasts.com/discuss/channels/laravel/current-user-in-middleware

OP已声明

i have added the following code in the global middleware and nou is het working 
\App\Http\Middleware\EncryptCookies::class, \Illuminate\Session\Middleware\StartSession::class,

它很脏,但是可以用.我认为这不是最佳做法,但是它解决了问题.既然我们发现了问题,那么有人可以在这里提出一个体面的解决方案吗?

It is dirty but it worked. I don't think that this is the best practice but it solved the problem. Since we found the problem, from here can someone suggest a decent solution?

这篇关于Laravel中间件将用户信息返回为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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