升级视图
文档
变更内容
升级指南
首先,将所有视图移动到 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>