PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 10, 2022

[FIXED] How to make Laravel 5 return 404 status code

 February 10, 2022     http-status-code-404, laravel-5     No comments   

Issue

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.



Answered By - Dan Willett
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing