Table of Contents

CodeIgniter 4

Simple demo of CI4 framework.

Environment

Devuan Chimaera 4.0

Install Apache & PHP

Update

apt update
apt full-upgrade

Install

apt install apache2 php php-cli php-mbstring php-intl php-curl php-json php-mysqlnd php-xml

PHP version

php --version
PHP 7.4.28 (cli) (built: Feb 17 2022 16:17:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies

Install MySQL

Install MariaDB fork of MySQL.

apt-get install default-mysql-server default-mysql-client

MySQL version

mysql --version
mysql  Ver 15.1 Distrib 10.5.15-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

Install CI4

Download package and unzip into htdocs.

mkdir -p /var/www/htdocs
cd /var/www/htdocs
wget https://api.github.com/repos/codeigniter4/CodeIgniter4/zipball/v4.2.4 -O ci4.2.4.zip
unzip ci4.2.4.zip
mv codeigniter4-CodeIgniter4-* hostname.kewl.org
cd hostname.kewl.org
chown www-data:www-data writable

Warning: 4.1 would auto-detect routes but this is not the case with 4.2.

Configure apache

<VirtualHost *:80>
    DocumentRoot "/var/www/htdocs/hostname.kewl.org/public"
    ServerName hostname.kewl.org
    ServerAdmin root

    Header append X-FRAME-OPTIONS "SAMEORIGIN"

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* /var/www/htdocs/hostname.kewl.org/public/index.php [L]

    <Directory "/var/www/htdocs/hostname.kewl.org/public">
        Options None
        AllowOverride None
    </Directory>

    CustomLog /var/log/apache2/hostname.kewl.org/access.log combined
    ErrorLog  /var/log/apache2/hostname.kewl.org/error.log
</VirtualHost>

Configure CI4

Change baseURL and indexPage in app config.

vi app/Config/App.php

Change logging threshold if required.

vi app/config/Logger.php

Hello world

Controller
vi app/Controllers/Hello.php
<?php

namespace App\Controllers;

class Hello extends BaseController
{
    public function index()
    {
        echo view('Hello');
    }
}
?>
Index view
vi app/Views/Index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Index</title>
</head>
<body>

<!--Content start-->
<?=$this->renderSection("Content")?>
<!--Content end-->

</body>
</html>
Controller view
vi app/Views/Hello.php
<?php
    $this->extend("Index");
    $this->section("Content");
    phpinfo();
    $this->endSection();
?>
Route

vi app/Config/Routes.php

$routes->get('/Hello', 'Hello::index');
URL
http://hostname.kewl.org/Hello

Resources

View layouts

Routing

Basic overview