CodeIgniter Forums
set 404 custom error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: set 404 custom error (/showthread.php?tid=76223)



set 404 custom error - eleumas - 04-23-2020

Hi, if i set 404 error with this route (where Errors is the name the controller and show404 the method), i have a php error: Class 'Errors' not found.

Code:
$routes->set404Override(Errors::show404);

if i set error with this route the page 404 work.

Code:
$routes->set404Override(function(){echo view('show404.php');});

Why? Where i wrong?

Thanks for the support.


RE: set 404 custom error - ajayamagar - 07-04-2020

This line of code worked for me.
$routes->set404Override('App\Controllers\Errors::show404');


RE: set 404 custom error - InsiteFX - 07-05-2020

Because you need to pass along the namespace also.


RE: set 404 custom error - wdeda - 07-05-2020

(07-04-2020, 08:41 PM)ajayamagar Wrote: This line of code worked for me.
$routes->set404Override('App\Controllers\Errors:Confusedhow404');

I did a little bit different ...

Route:
PHP Code:
<?php namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH 'Config/Routes.php'))
{
 require 
SYSTEMPATH 'Config/Routes.php';
}

/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override(function()
{
    $header = array (
    'icon' => 'favicon',
    'css' => 'nodata',
    'title' => 'Page not found - wdeda',
    'action' => '/search/allmedia/',
    'placeholder' => 'Search'
    );
    
    
echo view('templates/headerr'$header);
    echo view('content/error_404');
    echo view('templates/footerfb');
    });
$routes->setAutoRoute(true);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/''Home::index');

/**
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need to it be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (file_exists(APPPATH 'Config/' ENVIRONMENT '/Routes.php'))
{
 require 
APPPATH 'Config/' ENVIRONMENT '/Routes.php';


The view:
PHP Code:
<!-- 20200524 -->

 <
div class="content-container">
 <
div class="message">
 <
img alt="Oops" src="/public/img/common/wd-20200416-oops.png" />
 <
span class="msgerror">ERRO 404página não encontrada.</span>
 <
h2>
 - 
What the f***!<br />
 - 
Houstonwe have a problem here.
 </
h2>
 <
h4>Às vezesisso pode acontecerNos desculpe pelo inconveniente.</h4>
 </
div>
 <
div class="links">
 <
p><a href="http://localhost">Retornar à página inicial</a></p>
 <
p><a href="mailto://[email protected]">Entrar em contato conosco para reportar um problema.</a></p>
 </
div>
 </
div