뷰 업그레이드

문서

변경된 사항

  • 뷰는 이전과 매우 유사하지만 호출 방식이 다릅니다. CI3의 $this->load->view('x'); 대신 return view('x');를 사용할 수 있습니다.

  • CI4는 응답을 조각으로 구성하기 위한 셀 보기와 페이지 레이아웃을 위한 레이아웃 보기를 지원합니다.

  • 템플릿 파서는 여전히 존재하며 크게 향상되었습니다.

업그레이드 가이드

  1. 먼저 모든 뷰를 app/Views 폴더로 이동하십시오.

  2. 뷰를 로드하는 모든 스크립트에서 뷰 로드 구문을 변경하십시오:
    • $this->load->view('directory_name/file_name')에서 return view('directory_name/file_name');으로

    • $content = $this->load->view('file', $data, TRUE);에서 $content = view('file', $data);으로

  3. (선택 사항) 뷰의 echo 구문을 <?php echo $title; ?>에서 <?= $title ?>으로 변경할 수 있습니다.

  4. defined('BASEPATH') OR exit('No direct script access allowed'); 줄이 있으면 제거하십시오.

코드 예제

CodeIgniter 버전 3.x

경로: application/views:

<html>
<head>
    <title><?php echo html_escape($title); ?></title>
</head>
<body>
    <h1><?php echo html_escape($heading); ?></h1>

    <h3>My Todo List</h3>

    <ul>
    <?php foreach ($todo_list as $item): ?>
        <li><?php echo html_escape($item); ?></li>
    <?php endforeach; ?>
    </ul>

</body>
</html>

CodeIgniter 버전 4.x

경로: app/Views:

<html>
<head>
    <title><?= esc($title) ?></title>
</head>
<body>
    <h1><?= esc($heading) ?></h1>

    <h3>My Todo List</h3>

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

</body>
</html>