CodeIgniter 4.4.1 の使い方

Ubuntu 23.04 にインストールしました。

## 参考ページ
[CodeIgniter4: Manual Installation](https://codeigniter.com/user_guide/installation/installing_manual.html)

## ソースのダウンロード
こちらのページからダウンロード
[CodeIgniter: Download](https://codeigniter.com/download)
codeigniter4-framework-v4.4.1-0-g844616e.zip

## 解凍

```bash
unzip codeigniter4-framework-v4.4.1-0-g844616e.zip
```

## 設定

```bash
cd codeigniter4-framework-844616e
mv env .env
```

.env の修正

```text:.env
（省略）
# CI_ENVIRONMENT = production
CI_ENVIRONMENT = development
（省略）
```

## ライブラリーのインストール

```bash
sudo apt install php-intl
sudo apt install php-mbstring
```

サーバーの起動

```bash
php spark serve
```

```text
$ php spark serve

CodeIgniter v4.4.1 Command Line Tool - Server Time: 2023-09-05 08:40:55 UTC+00:00

CodeIgniter development server started on http://localhost:8080
Press Control-C to stop.
[Tue Sep  5 17:40:55 2023] PHP 8.1.12-1ubuntu4.3 Development Server (http://localhost:8080) started
```

## ブラウザーでアクセス
http://localhost:8080/

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/66f96816-0791-26de-fa85-9112d6ebd633.png)

# 静的ページの表示
参考ページ
[Build Your First Application / Static Pages](https://codeigniter4.github.io/userguide/tutorial/static_pages.html#)

## Views の作成

次のページを作成

```text
├── pages
│   ├── about.php
│   └── home.php
└── templates
    ├── footer.php
    └── header.php
```

```php:app/Views/pages/about.php
<p>Views/pages/about.php</p>
```

```php:app/Views/pages/home.php
<p>Views/pages/home.php</p>
```

```php:app/Views/templates/header.php
<!doctype html>
<html  lang="ja">
<head>
    <meta charset="utf-8">
    <title>CodeIgniter Tutorial</title>
</head>
<body>
    <h1><?= esc($title); ?></h1>
```

```php:app/Views/templates/footer.php
<p>フッター</p>
<blockquote>
    <em>&copy; 2023</em>
</blockquote>
</body>
</html>
```

## Controllers の作成

```php:app/Controllers/Pages.php
<?php

namespace App\Controllers;

class Pages extends BaseController
{
    public function index()
    {
        return view('welcome_message');
    }

    public function view($page = 'home')
    {
        if (! is_file(APPPATH . 'Views/pages/' . $page . '.php')) {
            // Whoops, we don't have a page for that!
            throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
        }
    
        $data['title'] = ucfirst($page); // Capitalize the first letter
    
        echo view('templates/header', $data);
        echo view('pages/' . $page, $data);
        echo view('templates/footer', $data);
    }
}
```

## ルーティングの設定

次のように設定

```php:app/Config/Routes.php
<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', 'Home::index');

use App\Controllers\Pages;

$routes->get('pages', [Pages::class, 'index']);
$routes->get('(:segment)', [Pages::class, 'view']);
```

## ブラウザーでアクセス

http://localhost:8080/home/
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/31f7d161-79a1-8180-8cf8-d3baf2372f7b.png)


http://localhost:8080/about/
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/19654313-f678-b65c-9ce7-2852f804ce91.png)

## 確認したバージョン

```bash
$ php -v
PHP 8.1.12-1ubuntu4.3 (cli) (built: Aug 17 2023 17:37:48) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.12-1ubuntu4.3, Copyright (c), by Zend Technologies
```