Welcome Guest, Not a member yet? Register   Sign In
Setting 404 Status Code
#1

Recently discovered that my 404 override was throwing errors silently into the logs in production. Trying to troubleshoot it this morning. 

This is my code in the app/Config/Routes.php file:

PHP Code:
$routes->set404Override(static function() {
   $response->setStatusCode(404);
   echo view("error_404.php");
 }); 

This produces an error message in the log (in production) (Line 23 being the $response->setStatusCode(404) line):

Code:
CRITICAL - 2023-06-21 21:30:31 --> Undefined property: CodeIgniter\Router\RouteCollection::$response
in APPPATH/Config/Routes.php on line 23.
1 APPPATH/Config/Routes.php(23): CodeIgniter\Debug\Exceptions->errorHandler()
2 SYSTEMPATH/CodeIgniter.php(954): CodeIgniter\Router\RouteCollection->Config\{closure}()
3 SYSTEMPATH/CodeIgniter.php(387): CodeIgniter\CodeIgniter->display404errors()
4 FCPATH/index.php(67): CodeIgniter\CodeIgniter->run()
If I force it to try and load a bad page in development environment, it throws:

Code:
ErrorException
Undefined variable $response
APPPATH/Config/Routes.php at line 23

22    $routes->set404Override(static function() {
23        $response->setStatusCode(404);
24        echo view("error_404.php");
25    });

I thought I was following the manual, but maybe I am missing something. Any help/suggestions would be appreciated.
Marc
Reply
#2

(This post was last modified: 06-22-2023, 02:42 PM by Mni.day.)

$this->response->
or
$response = Services::response();
Reply
#3

(This post was last modified: 06-22-2023, 04:49 PM by kenjis.)

In the closure the $response is not initialize, and there is not $this->response.
Reply
#4

sorry
Reply
#5

THANK YOU! I finally got a correct 404 response (it used to be 200 and my SEO guy was complaining about it)
Here is the code in app\Config\Routes.php (CodeIgniter version 4.4.1):

$routes->set404Override(function () {
$response = CodeIgniter\Config\Services::response();
$response->setStatusCode(404);
return view('errors/html/error_404');
});
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#6

(10-05-2023, 12:03 AM)Leo Wrote: THANK YOU! I finally got a correct 404 response (it used to be 200 and my SEO guy was complaining about it)
Here is the code in app\Config\Routes.php (CodeIgniter version 4.4.1):

$routes->set404Override(function () {
    $response = CodeIgniter\Config\Services::response();
    $response->setStatusCode(404);
    return view('errors/html/error_404');
});

Thanks for posting this!!  Finally working correctly!
Reply
#7

Hi there! In codeigniter 4.6.1 iyou can use $this->response directly.
https://codeigniter4.github.io/userguide...ponse.html

$statusCode = $this->pageTitle==='404' ? 404 : 200;
$this->response->setStatusCode($statusCode);

I was getting always a 404 status, except at the base_url, even on all correct urls, I still don't know why, so i resorted -by now- to check the page title, which i set to 404 when i know the uri doesn't exist in the database. Cheers!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB