gpt4 book ai didi

php - codeigniter 管理面板自动创建页面

转载 作者:行者123 更新时间:2023-12-02 23:52:48 25 4
gpt4 key购买 nike

我想创建一个 Controller 文件,如果我动态创建一个菜单,它会自动创建一个函数,并且还想创建连接到该主 Controller 的 View 页面。如何做到这一点?

当前代码:

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 }
}

最佳答案

按照以下步骤操作希望这是有道理的。-- 管理部分 --

/*content.php -- Controller 从这里开始 */

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 到此结束*/

/*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 在此结束 # 管理部分更改在此结束*/-- 将 View 文件也添加到管理部分 content_editpage.php

现在转到你的routes.php文件的前面部分--最后添加以下行--

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

这将适用于所有网址,例如 --- http://yourdomainname/your_slug_name

//在前面的 page.php 中再次创建一个 Controller --

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
}

}

关于php - codeigniter 管理面板自动创建页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com