如果你有几个页面,你想检查,我会建议有一个页面控制器,管理这些页面。事情是这样的
class Pages extends CI_Controller {
public function view($page_name)
{
$this->load->Pages_model();
if($this->Pages_model->does_exist($page_name))
{
// Does exist. Do things.
} else
{
show_404();
}
}
}
在你routes.php
,您将自己的about
和contact
页面(你可能拥有的任何其他人)的页面控制器。
$route['about'] = "pages/view/about";
$route['contact'] = "pages/view/contact";
你Pages_model
需要一个简单的函数来检查,如果页面名称存在于数据库中。
function does_exist($page_name) {
$this->db->where('name', $page_name); // assuming you have a table with a `name` field
$query = $this->db->get('pages'); // select from the `pages` table
return $query->num_rows() > 0; // returns bool
}
看看[hooks](http://codeigniter.com/user_guide/general/hooks.html)。 –