Search code examples
laravel-5http-status-code-404

How to make Laravel 5 return 404 status code


I am trying to get Laravel 5 (5.1.31) to return a http status response of 404 when a page is not found. I have tried several things and it always returns 200.

In my controller I have this:

else
   {
   header('HTTP/1.0 404 Not Found');
   return view('errors.404);
   }

I have also tried:

else
   {
   http_response_code(404);
   return view('errors.404);
   }

and

else
   {
   abort(404, 'Page not found');
   }

I also tried putting this in the 404.blade

@inject( 'response', 'Illuminate\Http\Response' )
{{ $response->status(404) }}

No success. No matter what I try, Laravel returns 200. How do I get it to return 404?


Solution

  • While I don't know why abort is not returning the 404 status as it is suppose to, I did find a solution that will make Laravel return a 404 status:

    Here is what I did:

    else {
        $data['title'] = '404';
        $data['name'] = 'Page not found';
        return response()->view('errors.404',$data,404);
    }
    

    This actually works better for my purposes because it doesn't mess with the contents of my 404.blade like the abort does.