Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 624
Dan Willett Avatar asked Mar 05 '17 21:03

Dan Willett


People also ask

How do I create a 404 page in laravel?

Default 404 Page: Now you have to just create "errors" folder in your resources directory and then after you need to create 404. blade. file inside that folder. So, basically laravel will stetted default design, but if you created 404 file into "errors" directory then it will take from there.

When should you return 404 Not Found?

If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead.


1 Answers

you may use the abort helper:

abort(404); 
like image 195
风声猎猎 Avatar answered Sep 28 '22 00:09

风声猎猎