1.新建一个文件 application/controllers/Pages.php ,然后添加如下代码。
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
2.新建页头文件 application/views/templates/header.php 并添加以下代码:
<html>
<head>
<title>CodeIgniter Tutorial</title>
</head>
<body>
<h1>我是页眉</h1>
<h1><?php echo $title; ?></h1>
3.新建页脚文件 application/views/templates/footer.php ,然后添加以下代码:
<em>我是页脚</em>
</body>
</html>
4.新建application/views/pages/ home.php,
<?php echo “hom页”;?>
5.新建application/views/pages/ about.php,
<?php echo “about页”;?>
6.访问 index.php/pages/view/about 时你将看到 about 页面, 包括页头和页脚。
7.打开文件 application/config/routes.php 然后添加如下两行代码, 并删除掉其他对 $route 数组赋值的代码。
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
8.现在访问 index.php/about 。路由规则是不是正确的将你带到了控制器中的 view() 方法。