MorshedSaif
MorshedSaif

Reputation: 399

Laravel middleware returning (Trying to get property 'headers' of non-object) error

I'm getting error when I wrap a resource route to my custom middleware

My middleware:

<?php

 namespace App\Http\Middleware;

 use Closure;
 use Auth;

class Officer
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->role == 'title_officer') {
            return $next($request);
        }
        // elseif (Auth::check() && Auth::user()->role == 'agent') {
        //     return redirect('/agent');
        // }
        // else {
        //     return redirect('/customer');
        // }
    }
}

The resource route using the middleware:

Route::resource('new-order', 'BackendController')->middleware('officer');

I'm getting error:

(Trying to get property 'headers' of non-object).

How to fix it ?

Upvotes: 20

Views: 65364

Answers (6)

Limon
Limon

Reputation: 31

Check middleware function and you have missed below one_

return $next($request);

so add this line.

Upvotes: 0

kevlon galloway
kevlon galloway

Reputation: 21

I had this error. In my controller I had

return route('dashboard')

instead of

return redirect()->route('dashboard')

That should fix it

Upvotes: 2

sam00vun0c
sam00vun0c

Reputation: 27

Late but.. This happens to me when column in DB was null.

Upvotes: -2

Android user
Android user

Reputation: 63

just use some thing like this in middleware : RedirectIfAuthenticated

public function handle($request, Closure $next, $guard = null)
{
     if (Auth::guard($guard)->check()) {
         // your condition
         return redirect('the route name');
     }
}

Upvotes: 3

Mihir Bhende
Mihir Bhende

Reputation: 9055

In middleware, it is important to handle all cases and return the redirects accordingly or abort.

You do return $next($request); when you want to allow system to continue processing the request ahead.

However, if in case if (Auth::check() && Auth::user()->role == 'title_officer') condition fails, you have not mentioned what system should do.

You can may be abort(404) if you do not want to show the page as available or maybe abort(403) for access forbidden.

public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->role == 'title_officer') {
            return $next($request);
        }
        abort(403);
    }

But make sure you do not add a case which will make an infinite loop. Please check documentation for more options.

Upvotes: 43

Kapitan Teemo
Kapitan Teemo

Reputation: 2164

try adding an else block which returns when your if condition isn't met:

public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->role == 'title_officer') {
            return $next($request);
        }else{
           return back();
        }
    }

Upvotes: 0

Related Questions