3

I have no idea why I keep getting this lately by just simply navigate between pages.

Call to a member function setCookie() on null

This is what I have in my 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);
    }
}

I'm on Laravel 5.8.

3
  • I did a quick reseach I found this answer. : stackoverflow.com/a/48970646/4480164
    – code-8
    Commented Sep 20, 2019 at 17:49
  • I suddenly check my AdminMiddleware since I logged in as admin, but nothing seems wrong in there, and I have no idea, why this is start happening - just lately - only.
    – code-8
    Commented Sep 20, 2019 at 17:50
  • Any suggestions on what should I look into will be much appreciated!
    – code-8
    Commented Sep 20, 2019 at 17:51

2 Answers 2

5

The error occurs when you are logged in as a non Admin because you are returning a View in your AdminMiddleware instead of a Response.

Replace:

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

With:

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

To expand on @Chin Leung's answer and to properly return a 404 not found status code

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.