按照教程,可以通过application/controllers/pages.php处理所有静态页面的请求
class Pages extends CI_Controller {
public function view($page = 'home')
{
if( ! file_exists('application/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('templates/nav', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/aside_right', $data);
$this->load->view('templates/bottom', $data);
}
}
这适用于“主页”页面,但我似乎无法调用 view/pages/about 例如。
我试图为关于页面制作一个单独的控制器。它有效,但感觉有些不对劲。
应用程序/控制器/about.php
class About extends CI_Controller {
public function index()
{
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/about');
$this->load->view('templates/aside_right');
$this->load->view('templates/bottom');
}
}
我的 htaccess 文件或路由文件也有问题。使用上面编写的 About 控制器,我只能通过键入 domain.com/index.php/about 来访问该页面。我希望它是 domain.com/about 等。
这就是我的 routes.php 的样子:
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
我的 Htaccess:
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]