ビューのアップグレード
ドキュメント
変更点
ビューは以前とよく似ていますが、呼び出し方が異なります... CI3の
$this->load->view('x');
の代わりに、return view('x');
を使用できます。CI4は、レスポンスをピースで構築するためのビューセルと、ページレイアウトのためのビューレイアウトをサポートしています。
テンプレートパーサーはそのまま残り、大幅に機能強化されています。
アップグレードガイド
まず、すべてのビューを app/Views フォルダに移動します。
- ビューをロードするすべてのスクリプトで、ビューのロード構文を変更します。
$this->load->view('directory_name/file_name')
からreturn view('directory_name/file_name');
へ$content = $this->load->view('file', $data, TRUE);
から$content = view('file', $data);
へ
(オプション) ビューでのecho構文を
<?php echo $title; ?>
から<?= $title ?>
に変更できます。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>