How to get the latest Codeigniter release?

post Main Image

Fetch the latest stable release from GitHub and display it on your app. Useful as information on the admin or superadmin pages to remind you it may be time to upgrade.

Easy peasy snippet:

Place this on your controller

// Get current version
        $currentVersion = \CodeIgniter\CodeIgniter::CI_VERSION;

        // Get latest version from GitHub API
        $latestVersion = null;
        $context = stream_context_create(['http' => ['user_agent' => 'CI-Version-Checker']]);
        $json = @file_get_contents('https://api.github.com/repos/codeigniter4/CodeIgniter4/releases/latest', false, $context);
        if ($json) {
            $data = json_decode($json, true);
            if (isset($data['tag_name'])) {
                $latestVersion = ltrim($data['tag_name'], 'v');
            }
        }

Place this on your view

<div class="container">
    <div class="row">
        <div class="col-md-12 text-center">
            CI version in use: <strong><?= esc($ciVersion) ?></strong>
            <?php if ($latestVersion): ?>
                | Latest CI release: <strong><?= esc($latestVersion) ?></strong>
            <?php endif; ?>
        </div>
    </div>
</div>

Are you concerned that a request to GH on each user request is not the best? You are smart. Modify this to run a command with cron every day/ week/ whatever to check this on the background and update a file or db entry with the updated value.

Amazing. Beautiful. Meaningful. Rejoice.