CodeIgniter Forums
How to create custom Error 404 page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to create custom Error 404 page (/showthread.php?tid=75405)



How to create custom Error 404 page - falko - 02-02-2020

Hello, guys!

I'm creating a RESTful resource and I marked the page "new" as except.

OK. Everything working nice, BUT, how to handle a custom Error 404 page in JSON format (the default is html) when someone access the /new path?


RE: How to create custom Error 404 page - littlej - 02-02-2020

Hello Falko !

You can set a custom 404 page in the "app/Config/Routes.php" file: https://codeigniter4.github.io/userguide/incoming/routing.html#override

The part that will interest you is:
PHP Code:
// Would execute the show404 method of the App\Errors class
$routes->set404Override('App\Errors::show404'); 

So you just have to redirect to a Class::method of your choice, that will return the json response you are looking for :-)


> Maybe the RESTful madman will have something to add ? :-D


RE: How to create custom Error 404 page - falko - 02-19-2020

(02-02-2020, 05:24 PM)littlej Wrote: Hello Falko !

You can set a custom 404 page in the "app/Config/Routes.php" file: https://codeigniter4.github.io/userguide/incoming/routing.html#override

The part that will interest you is:
PHP Code:
// Would execute the show404 method of the App\Errors class
$routes->set404Override('App\Errors::show404'); 

So you just have to redirect to a Class::method of your choice, that will return the json response you are looking for :-)


> Maybe the RESTful madman will have something to add ? :-D

Thank you! It solved my question.


RE: How to create custom Error 404 page - littlej - 02-19-2020

Perfect ! :-) If you have time, don't forget to rate my answer with the "Rate" button at the bottom ;-)


RE: How to create custom Error 404 page - falko - 02-19-2020

(02-19-2020, 01:27 PM)littlej Wrote: Perfect ! :-) If you have time, don't forget to rate my answer with the "Rate" button at the bottom ;-)

The button is not working. It just freeze the screen.


RE: How to create custom Error 404 page - littlej - 02-19-2020

No problem. The most important thing is that you have got a solution ;-)