0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CodeIgniter 4.4.1 の使い方

Last updated at Posted at 2023-09-05

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

参考ページ

CodeIgniter4: Manual Installation

ソースのダウンロード

こちらのページからダウンロード
CodeIgniter: Download
codeigniter4-framework-v4.4.1-0-g844616e.zip

解凍

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

設定

cd codeigniter4-framework-844616e
mv env .env

.env の修正

.env
(省略)
# CI_ENVIRONMENT = production
CI_ENVIRONMENT = development
(省略)

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

sudo apt install php-intl
sudo apt install php-mbstring

サーバーの起動

php spark serve
$ 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

ブラウザーでアクセス

image.png

静的ページの表示

参考ページ
Build Your First Application / Static Pages

Views の作成

次のページを作成

├── pages
│   ├── about.php
│   └── home.php
└── templates
    ├── footer.php
    └── header.php
app/Views/pages/about.php
<p>Views/pages/about.php</p>
app/Views/pages/home.php
<p>Views/pages/home.php</p>
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>
app/Views/templates/footer.php
<p>フッター</p>
<blockquote>
    <em>&copy; 2023</em>
</blockquote>
</body>
</html>

Controllers の作成

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);
    }
}

ルーティングの設定

次のように設定

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

http://localhost:8080/about/
image.png

確認したバージョン

$ 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?