CI4 Playground v4.7.3
한국어문서
1
기본 뷰 렌더링

컨트롤러에서 view() 헬퍼로 뷰를 렌더링하고 데이터를 전달합니다.

컨트롤러
public function index(): string
{
    // 뷰 파일: app/Views/examples/views/index.php
    return view('examples/views/index', [
        'title' => '뷰',         // $title 로 사용 가능
        'items' => [1, 2, 3],   // $items 로 사용 가능
    ]);
}
뷰 파일
<?php // app/Views/examples/views/index.php ?>

<!-- XSS 방지: esc() 필수 -->
<h1><?= esc($title) ?></h1>

<?php foreach ($items as $item): ?>
    <li><?= esc($item) ?></li>
<?php endforeach; ?>

<!-- 뷰 내에서 다른 뷰 포함 -->
<?= view('components/alert', ['message' => '성공!']) ?>
보안: 사용자 입력은 항상 esc($value)로 출력하여 XSS를 방지합니다.
2
레이아웃 시스템

공통 레이아웃을 정의하고 각 페이지에서 extend()로 상속합니다. 이 페이지 자체도 레이아웃을 사용하고 있습니다.

레이아웃 파일 (app/Views/layouts/main.php)
<!DOCTYPE html>
<html>
<head>
    <title><?= esc($title ?? 'CI4') ?></title>
</head>
<body>
    <!-- 각 페이지의 콘텐츠가 여기에 삽입됩니다 -->
    <?= $this->renderSection('content') ?>
</body>
</html>
페이지 파일 (app/Views/examples/views/layout.php)
// 레이아웃 상속
<?= $this->extend('layouts/main') ?>

// 'content' 섹션 정의
<?= $this->section('content') ?>

<h1>페이지 내용</h1>
<p>여기에 콘텐츠를 작성합니다.</p>

// 섹션 종료
<?= $this->endSection() ?>
3
파셜 & 컴포넌트 include

반복 사용되는 UI 조각을 별도 파일로 분리하고 view()로 포함합니다.

뷰에서 파셜 포함
// 단순 포함
<?= view('components/footer') ?>

// 데이터 전달
<?= view('components/alert', ['type' => 'success', 'message' => '저장 완료!']) ?>

// 배열 데이터 전달
<?= view('components/item_list', ['items' => $items]) ?>
4
View Cell — 재사용 컴포넌트

View Cell은 독립적인 로직을 가진 재사용 가능한 UI 컴포넌트입니다. 자체 DB 조회, 데이터 처리가 가능합니다.

Cell 클래스 (app/Cells/RecentPostsCell.php)
namespace App\Cells;

use CodeIgniter\View\Cells\Cell;

class RecentPostsCell extends Cell
{
    public int $limit = 5; // 기본값, 외부에서 주입 가능

    public function mount(): void
    {
        // DB 조회 등 독립 로직 처리
    }
}
뷰에서 사용
<?= view_cell('RecentPostsCell') ?>
<?= view_cell('RecentPostsCell', ['limit' => 3]) ?>
<?= view_cell('App\Cells\RecentPostsCell::render', ['limit' => 10]) ?>