简体   繁体   English

codeigniter管理面板自动页面创建

[英]codeigniter admin panel automatic page creation

I want to create one controller file which is creating automatically a function if i create a menu dynamically and also want to create view page which is connencted to this main controller.. how to do that? 我想创建一个控制器文件,如果我动态创建菜单,并且还想创建与该主控制器连接的视图页面,该文件将自动创建一个功能。该怎么做?

Current code: 当前代码:

public function our_history() 
      { 
          $data['category']= $this->menu_model->getCategory('$lang'); 
          $data['subcategory']= $this->menu_model->getSubCategory('$lang'); 
          $this->load->view('vwMain',$data);//Left Menu } 
      }

Follow below steps Hope that makes sense. 请按照以下步骤操作希望有道理。 -- Admin section -- -管理部分-

/*content.php -- controller starts here */ /*content.php-控制器从这里开始* /

class Content extends VCI_Controller {

# Class constructor
function __construct()
{
    parent::__construct();
    $this->load->model('content_model');
}

/*
Add page logic
*/
function edit_page($id = null)
{

    $this->_vci_layout('your_layoutname');
    $this->load->library('form_validation');
    $view_data = array();

    //Set the view caption
    $view_data['caption'] = "Edit Content";

    //Set the validation rules for server side validation
    // rule name editcontent should be defined

    if($this->form_validation->run('editcontent')) {

        //Everything is ok lets update the page data
        if($this->content_model->update(trim($id))) {
            $this->session->set_flashdata('success', "<li>Page has been edited successfully.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        } else {
            $this->session->set_flashdata('error', "<li>Unknown Error: Unable to edit page.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        }

    } else {

        $page = $this->content_model->get_content_page(trim($id)); 
        $view_data["id"] = $page->id;
        $view_data["page_title"] = $page->page_title;
        $view_data["page_menu_slug"] = $page->page_menu_slug;
        $view_data["page_name"] = $page->page_name;
        $view_data["page_content"] = $page->page_content;
        $view_data["status"] = $page->status;
        $this->_vci_view('content_editpage', $view_data);
    }
}

/*
Edit page logic
*/
function add_page()
{

    $this->_vci_layout('your_layoutname');
    $this->load->library('form_validation');

    $view_data = array();

    $view_data['caption'] = "Edit Content";

    if($this->form_validation->run('editcontent')) { 
        // after passing validation rule data to be saved
        // editcontent rule must be defined in formvalidations file

        //Everything is ok lets update the page data
        if($this->content_model->add()) {
            $this->session->set_flashdata('success', "<li>Page has been edited successfully.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        } else {
            $this->session->set_flashdata('error', "<li>Unknown Error: Unable to edit page.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        }

    } else {

        $page = $this->content_model->get_content_page(trim($id)); 
        $view_data["id"] = $page->id;
        $view_data["page_title"] = $page->page_title;
        $view_data["page_menu_slug"] = $page->page_menu_slug;
        $view_data["page_name"] = $page->page_name;
        $view_data["page_content"] = $page->page_content;
        $view_data["status"] = $page->status;
        $this->_vci_view('content_editpage', $view_data);
    }
}

} /** * content.php -- controller ends here */ } / ** * content.php-控制器到此为止* /

/* Content_model starts here */ / * Content_model从这里开始* /

class Content_model extends CI_Model {

// update logic goes here
function update($id = null) {
    if(is_null($id)) {
        return false;
    }

    $data = array(
        'page_title' => htmlspecialchars($this->input->post('page_title',true)),
        'page_name' => htmlspecialchars($this->input->post('page_name',true)),
        'page_content' => $this->input->post('page_content',true),
        'page_menu_slug' => htmlspecialchars($this->input->post('page_menu_slug',true)),
        'status' => htmlspecialchars($this->input->post('status',true))
    );

    $this->db->where('id', $id);
    $this->db->update('content', $data);
    return true;

}

// Add logic goes here
function add() {
    $data = array(
        'page_title' => htmlspecialchars($this->input->post('page_title',true)),
        'page_name' => htmlspecialchars($this->input->post('page_name',true)),
        'page_content' => $this->input->post('page_content',true),
        'page_menu_slug' => htmlspecialchars($this->input->post('page_menu_slug',true)),
        'status' => htmlspecialchars($this->input->post('status',true))
    );

    $this->db->where('id', $id);
    $this->db->insert('content', $data);
    return true ;
}

} /* Content_model ends here # Admin section changes ends here */ -- Add view files also to admin section content_editpage.php } / * Content_model在这里结束#管理部分更改在这里结束* /-也将视图文件添加到管理部分content_editpage.php

Now go to your routes.php file for front section -- add below line at last -- 现在转到您的routes.php文件的前半部分-最后在下面添加一行-

$route['(:any)'] = 'page/view_usingslug/$1';

This will be for all urls like --- http://yourdomainname/your_slug_name 这将适用于所有urls like --- http://yourdomainname/your_slug_name

// create again a controller in front section page.php -- //在前一节page.php中再次创建一个控制器

class page extends VCI_Controller {

    function __construct()
    {
        parent::__construct();

    }

    function view_usingslug($slug='')
    {
        // retrieve the data by slug from content table using any model class and assign result to $view_dat
        $this->_vci_view('page',$view_data);
        //page.php will be your view file
    }

}

Suppose Your URL is 假设您的网址是

www.example.com/controllername/methodname/menutitle1 www.example.com/controllername/methodname/menutitle1

or 要么

www.example.com/controllername/methodname/menutitle2 www.example.com/controllername/methodname/menutitle2

So this is how you handle these pages. 这就是您处理这些页面的方式。

public function method()
{
   $menutitle = $this->uri->segment(3);
   $query = $this->db->get_where('TableName',array('Menutitle'=>$menutitle))
   $data['content'] = $query->row()->page_content;
   $this->load->view('common_page',$data);

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM