我开始在一个工作项目中使用 Codeigniter,但我仍然掌握它的窍门......
浏览 Codeigniter 教程,我开始构建网站,只有一个控制器来处理视图,但现在我无法处理该控制器上的表单提交。我在这里失去了理智......:P
这是控制器的唯一功能(pages.php)
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
// handle ACTIVE class
$current_page = uri_string();
$this->Mp->isactiveset($this->session->userdata('active'),$current_page);
$this->Mp->newactive($this->session->userdata('active'),$current_page);
// testing database query and passing variables
$passar = $this->Mp->testedb();
// get ap types
$aptype = $this->Mp->getaptype();
// passing data to view
$data['estado'] = $passar; // Testing db connection
$data['title'] = ucfirst($page); // Homepage page
$data['aptypes'] = $aptype; // Ap types
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
我有处理准备好的表格的功能
public function index()
{
$this->form_validation->set_rules('nome', 'Username', 'required|min_length[3]|max_length[25]|');
$this->form_validation->set_rules('telemovel', 'Telephone', 'numeric|exact_length[9]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('mensagem', 'MessageBody', 'required|min_length[10]');
if ($this->form_validation->run() == FALSE)
{
// handle ACTIVE class
$current_page = uri_string();
$this->Mp->isactiveset($this->session->userdata('active'),$current_page);
$this->Mp->newactive($this->session->userdata('active'),$current_page);
// get ap types
$aptype = $this->Mp->getaptype();
// testing database query and passing variables
$passar = $this->Mp->testedb();
// passing data to view
$data['estado'] = $passar; // Testing db connection
$data['aptypes'] = $aptype; // Ap types
$this->load->view('templates/header', $data);
$this->load->view('pages/home', $data);
$this->load->view('templates/footer', $data);
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('pages/submit', $data);
$this->load->view('templates/footer', $data);
}
}
这就是 route.php 的样子(在第一个答案之后,我现在知道这是相关的)
$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
$route['404_override'] = '';
在视图上,当我执行 form_open('submit', $attributes) 时,它不会进入控制器上的任何函数,除了视图。显然我已经在pages.php上放了表单处理功能,重命名了它,尝试了不同的表单提交页面(form_open('我试过的页面',$attributes))。如何使其转到特定功能来处理表单提交?