Trongate Way Docs

Show and Delete

The show method displays a single record in a read-only detail view. The delete flow uses a two-step confirmation pattern to prevent accidental data loss.

The Show Method

PHP
/**
 * Display detailed view of a single country.
 *
 * Shows all details with edit/delete options.
 * Automatically handles missing records with 404 page.
 *
 * @return void
 */
public function show(): void {
    $this->trongate_security->make_sure_allowed();

    $update_id = segment(3, 'int');

    if ($update_id === 0) {
        redirect('countries/manage');
    }

    // Fetch record and prepare for display.
    $data = $this->model->get_data_from_db($update_id, true);

    if ($data === false) {
        $this->not_found();
        return;
    }

    // Add additional view data
    $data['update_id'] = $update_id;
    $data['headline'] = 'Country Details';
    $data['back_url'] = $this->get_back_url();
    $data['view_module'] = 'countries';
    $data['view_file'] = 'show';
    $this->templates->admin($data);
}

The method checks authentication, validates that an ID was provided via segment(3, 'int'), and then fetches the record from the model with true for the prepare_for_display parameter. If the record is not found (false is returned), it calls the private $this->not_found() method to show a clean error page.

Note the use of $this->get_back_url() - this private helper returns the previous URL if it was the manage page, otherwise defaults to /countries/manage:

PHP
/**
 * Determine appropriate back URL for navigation.
 *
 * Uses previous URL if it was the manage page, otherwise defaults to manage.
 *
 * @return string URL for back button
 */
private function get_back_url(): string {
    $previous_url = previous_url();
    if ($previous_url !== '' && strpos($previous_url, BASE_URL . 'countries/manage') === 0) {
        return $previous_url;
    }
    return BASE_URL . 'countries/manage';
}

The Show View

View File
<h1><?= $headline ?></h1>
<?= flashdata() ?>
<div class="card">
    <div class="card-heading">
        Country Details
    </div>
    <div class="card-body">
        <div class="text-right mb-3">
            <?= anchor($back_url, 'Back', array('class' => 'button alt')) ?>
            <?= anchor(BASE_URL.'countries/create/'.$update_id, 'Edit', array('class' => 'button')) ?>
            <?= anchor('countries/delete_conf/'.$update_id, 'Delete',  array('class' => 'button danger')) ?>
        </div>
        <div class="detail-grid">
            <div class="detail-row">
                <div class="detail-label">Country Title</div>
                <div class="detail-value"><?= out($country_title) ?></div>
            </div>
            <div class="detail-row">
                <div class="detail-label">Country Code</div>
                <div class="detail-value"><?= out($country_code) ?></div>
            </div>
        </div>
    </div>
</div>

The .detail-grid class arranges label-value pairs in a two-column layout. Each value is wrapped in out() to escape HTML entities. The top-right button bar provides three actions:

  • Back - returns to the previous page (or the manage page).
  • Edit - opens the create form with this record's data.
  • Delete - proceeds to the confirmation step.

The .detail-grid CSS class is significant - the manage page preview modal uses mx-select=".detail-grid" to extract this section when showing a quick preview. This is why the view uses a semantic class name rather than inline layout.

The Delete Flow

Deleting a record follows a two-step pattern to prevent accidents. The first step shows a confirmation page. The second step actually deletes the record.

Delete Confirmation

PHP
/**
 * Display confirmation page before deleting a country.
 *
 * Shows confirmation dialog with details to prevent accidental deletion.
 *
 * @return void
 */
public function delete_conf(): void {
    $this->trongate_security->make_sure_allowed();

    $update_id = segment(3, 'int');

    if ($update_id === 0) {
        $this->not_found();
        return;
    }

    $data = $this->model->get_data_for_edit($update_id);

    if ($data === false) {
        $this->not_found();
        return;
    }

    $data['update_id'] = $update_id;
    $data['headline'] = 'Delete Country Record';
    $data['cancel_url'] = 'countries/show/'.$update_id;
    $data['form_location'] = 'countries/submit_delete/'.$update_id;
    $data['view_module'] = 'countries';
    $data['view_file'] = 'delete_conf';
    $this->templates->admin($data);
}

It fetches the record via get_data_for_edit() (which returns raw data without display transformations) to confirm it exists, and renders the confirmation view:

View File
<h1><?= $headline ?></h1>
<div class="card">
    <div class="card-heading">
        Confirmation Required
    </div>
    <div class="card-body">
        <p>Are you sure?</p>
        <p>You are about to delete a country record. This cannot be undone. Do you really want to do this?</p>
        
        <?php
        echo form_open($form_location);
        echo '<div class="text-center">';
        echo anchor($cancel_url, 'Cancel', array('class' => 'button alt'));
        echo form_submit('submit', 'Yes - Delete Now', array('class' => 'danger'));
        echo form_close();
        echo '</div>';
        ?>
    </div>
</div>

The form posts to /countries/submit_delete/{id}. The Cancel button returns the user to the show page for that record.

Execute Deletion

PHP
/**
 * Handle country deletion after confirmation.
 *
 * Verifies confirmation and deletes record from database.
 * Includes safety checks to prevent unauthorized deletion.
 *
 * @return void
 */
public function submit_delete(): void {
    $this->trongate_security->make_sure_allowed();

    $submit = post('submit', true);

    if ($submit === 'Yes - Delete Now') {
        $update_id = segment(3, 'int');

        if ($update_id === 0) {
            redirect('countries/manage');
            return;
        }

        $record = $this->model->find_by_id($update_id);

        if ($record === false) {
            redirect('countries/manage');
            return;
        }

        $this->model->delete_record($update_id);

        set_flashdata('The record was successfully deleted');
        redirect('countries/manage');
    } else {
        redirect('countries/manage');
    }
}

The method:

  1. Verifies the form was actually submitted by checking that the submit button value is exactly 'Yes - Delete Now'. Any other value (or a direct URL hit) redirects safely.
  2. Checks that the record still exists using find_by_id(). If not, redirects to manage (the record may have been deleted by another user).
  3. Deletes the record via the model.
  4. Sets a flashdata success message and redirects back to the manage page.

The Not Found Page

When a requested record does not exist, the controller calls the private not_found() method instead of showing a raw error or a blank view:

PHP
/**
 * Display 404-style not found page for missing countries.
 *
 * Shows user-friendly error message with navigation back.
 *
 * @return void
 */
private function not_found(): void {
    $data = [
        'headline' => 'Country Not Found',
        'message' => 'The country you\'re looking for doesn\'t exist or has been deleted.',
        'back_url' => $this->get_back_url(),
        'back_label' => 'Go Back',
        'view_module' => 'countries',
        'view_file' => 'not_found'
    ];
    $this->templates->admin($data);
}

The not_found.php view provides a clean error card with a link back to safety:

View File
<h1><?= $headline ?></h1>
<div class="card">
    <div class="card-heading">
        <?= $headline ?>
    </div>
    <div class="card-body">
        <p><?= $message ?></p>
        <div class="text-center">
            <?= anchor($back_url, $back_label, array('class' => 'button alt')) ?>
        </div>
    </div>
</div>

This is a private helper method because it is an internal implementation detail - it is not intended to be called as a URL endpoint.

We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.

Share your thoughts in the Documentation Feedback.

Leave Feedback About This Page