php – codeigniter中的动态页面控件

在我的CI项目中,我想使用完整的动态页面控件.所以,我有两个控制器方法,它们加载php文件.模型控件的查询基于url段.视图php文件中自动生成所有页面输出,具体取决于数据库的url和结果,index.php文件除外.

这是正确的方法吗?

调节器

public function index()
 {
  $data['title'] = "Index";
  $data['nav'] = $this->content_model->get_index_nav(); //TODO

  $this->load->view('templates/header', $data);
  $this->load->view('templates/nav', $data);
  $this->load->view('templates/nav_pict', $data);
  $this->load->view('pages/aktualis', $data);
  $this->load->view('templates/footer', $data);
 }
 public function view($page)
 {
  $page = 'content';

  $this->load->helper('text');
  $this->load->helper('url'); 

  $page = lcfirst(convert_accented_characters(urldecode($page)));

  if ( ! file_exists('application/views/pages/'.$page.'.php'))
  {
   show_404();
  }

  $data['title'] = ucfirst($page); // Capitalize the first letter
  $data['nav'] = $this->content_model->get_nav();
  $data['content'] = $this->content_model->get_content();

  if(empty($data['content']))
  {
   show_404();
  }

  $this->load->view('templates/header', $data);
  $this->load->view('templates/nav', $data);
  $this->load->view('pages/'.$page, $data);
  $this->load->view('templates/footer', $data);
 }

模型(每个菜单都有一个数据库中的内容.当创建一个新的子菜单时,你必须添加内容)

public function get_content()
 {
   $this->db->select('content.*, mainmenu.label');
   $this->db->from('content');
   $this->db->join('mainmenu', 'mainmenu.id = content.katId', 'left');
   $this->db->where('mainmenu.label', mysql_escape_string(urldecode(end($this->uri->segments))));

   $query = $this->db->get();

   return $query->result_array();

 }

路由

$route['404_override'] = '';
$route['/:any/(:any)'] = 'pages/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['Index'] = 'pages/index';
$route['default_controller'] = 'pages/index';

最佳答案 您使用CodeIgniter就好了,不需要进行任何结构修改.

点赞