flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.63k stars 407 forks source link

Custom 404 page return 200 #453

Closed lk-KEVIN closed 9 months ago

lk-KEVIN commented 2 years ago

Hi, I was trying to use a custom html file to replace the default error page, but the body returns 200 OK instead of 404 Not Found. Tried the code for issue #343 and everything was fine, return 404

Flight::route('/status', function() {
    Flight::response()->status(404);
    echo 'The status is 404.';
});

then I try to Override using the example from the docs is when return 200 OK

Flight::map('notFound', function(){
    // Display custom 404 page
    include 'errors/404.html';
});

What should I do? neither http_response_code(404);, header("HTTP/1.1 404 Not Found"); or Apache ErrorDocument 404 errors/404.html return 404 Not Found

My only solution is replace the original flight\Engine.php error message

severak commented 2 years ago

Hi, this works for me:

Flight::map('notFound', function(){
    Flight::response(false)
            ->status(404)
            ->write(
                file_get_contents(__DIR__ . '/lib/views/404.htm')
            )
            ->send();
});
sah-comp commented 2 years ago

You may also use:

Flight::stop(404);
krmu commented 9 months ago

Or you can use Flight::halt(404, 'Not found');

@n0nag0n also can be closed.

n0nag0n commented 9 months ago

Thanks @krmu