error_404()

public function error_404(): void

Description

Displays the 404 error page. This method is automatically invoked by Trongate when a requested URL cannot be matched to any existing route. It loads the error_404 template view, which provides a user-friendly message indicating that the requested page could not be found. The method can be customized by editing the template view file or by changing the ERROR_404 constant in the configuration file to point to a different module and method.

Parameters

Parameter Type Description Default Required
This method takes no parameters.

Return Value

Type Description
void This method does not return a value. It outputs the complete HTML error page directly.

Example #1

The code sample below shows the default ERROR_404 constant definition found in config/config.php. This constant determines which module and method Trongate calls when a 404 error occurs.

PHP
define('ERROR_404', 'templates/error_404');

Example #2

This example demonstrates how to manually trigger a 404 error from within your own controller method when certain conditions are not met.

PHP
public function view(): void {
    $product_id = (int) segment(2);
    $product = $this->db->get_where($product_id, 'products');
    
    if ($product === false) {
        // Product not found - show 404 page
        $this->templates->error_404();
        return;
    }
    
    $data['product'] = $product;
    $this->templates->public($data);
}

Example #3

This example shows how to redirect the ERROR_404 constant to a custom error handling method in a different module.

PHP
<?php
// In config/config.php
define('ERROR_404', 'errors/not_found');

// In modules/errors/Errors.php
class Errors extends Trongate {
    public function not_found(): void {
        $data['page_title'] = 'Page Not Found';
        $data['message'] = 'Sorry, we could not find that page.';
        $this->templates->public($data);
    }
}

Example #4

This example demonstrates creating a custom 404 handler with additional logging functionality before displaying the error page.

PHP
<?php
// In modules/errors/Errors.php
class Errors extends Trongate {
    public function custom_404(): void {
        // Log the 404 error for analysis
        $requested_url = $_SERVER['REQUEST_URI'];
        $this->log_404_error($requested_url);
        
        // Display the 404 page
        $this->templates->error_404();
    }
    
    private function log_404_error(string $url): void {
        $log_data['url'] = $url;
        $log_data['timestamp'] = time();
        $log_data['ip_address'] = $_SERVER['REMOTE_ADDR'];
        $this->db->insert($log_data, 'error_logs');
    }
}