'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 1:[1]

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.

Solution 2:[2]

you may use the abort helper:

abort(404);

Solution 3:[3]

Very simple, I assumed you use Laravel v5++ just go to

app > Exceptions > Handler.php

See the picture as below:

enter image description here

And modify the codes from:

public function render($request, Exception $e)
{
    return parent::render($request, $e);
}

to

public function render($request, Exception $e)
{
    if ($e instanceof MethodNotAllowedHttpException)
    {
        abort(404);
    }
    return parent::render($request, $e);
}

Then, do not forget to add 404.blade.php page in errors folder:

enter image description here

Well, you can customise by yourself the 404 page in 404.blade.php.


Note

This case only when you run the URL were not listed as in the routes. You may find in web.php file.

In case you need to call by custom, just call in the controller like below:

public function show_me()
{
   abort(404);  //404 page
}

Hope it helps!

Solution 4:[4]

If you want to return JSON instead of a view, you can call this in your controller:

return response(['error'=>true,'error-msg'=>$msg],404);

Solution 5:[5]

If you have a 404 page under resources/views/errors/ then just do a

return view('errors.404');

or like ???? suggests

abort(404);

Solution 6:[6]

This is what I did because I needed to use my custom view and a 404 header:

return response()
            ->view('my_view', $data, 404);

I found it on https://laravel.com/docs/8.x/responses#view-responses

It worked in Laravel 8, but it seems that it works in older versions.

Solution 7:[7]

You can also do this

return response(view('view_name'), 404);

You can also add headers to the response like this

return response(view('view_name'), 404, [
    'header_1' => 'header 1 value',
    'header_2' => 'header 2 value' ...
]);

Extra info

The thing is that it works both on Laravel and Lumen (micro framework by Laravel). However,

response()->view() 

doesn't work on Lumen.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sebastian Viereck
Solution 2 ????
Solution 3 Nere
Solution 4 nicomonjelat
Solution 5 Tiago Martins Peres
Solution 6 Luis Rodriguez
Solution 7 Koushik Das