在 null 上调用成员函数 setCookie() - Laravel 5.8

Call to a member function setCookie() on null - Laravel 5.8

我不知道为什么我最近总是通过简单地在页面之间导航来得到这个。

Call to a member function setCookie() on null

这是我的 AdminMiddleware

<?php

namespace App\Http\Middleware;

use App\Article;
use Closure, View, Auth ;
use Illuminate\Contracts\Auth\Guard;

class AdminMiddleware
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ( Auth::user()->type !== "Admin") {
            return View::make('layouts.share.errors.404');
        }

        return $next($request);
    }
}

我在 Laravel 5.8。

当您以非 Admin 身份登录时会发生错误,因为您在 AdminMiddleware 中返回 View 而不是 Response

替换:

if ( Auth::user()->type !== "Admin") {
    return View::make('layouts.share.errors.404');
}

与:

if ( Auth::user()->type !== "Admin") {
    return response()->view('layouts.share.errors.404', [], 404);
}

扩展@Chin Leung 的回答并正确return 404 未找到状态代码

if ( Auth::user()->type !== "Admin") {
    return response()->view('layouts.share.errors.404', [], 404);
}