我删除了coneigniter2中的一行,但它需要404页面 (找不到您请求的页面。)
这是我的代码
Controler Post
class Posts extends CI_Controller{
public function index(){
$data['title'] = 'Ultimos Post';
$data['posts'] = $this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
...
public function delete($id){
//apagar post pelo id
$this->Post_Model->delete_post($id);
redirect('posts');
}}
发布模型
class Post_model extends Ci_Model{
...
public function delete_post($id) {
$this->db->where('id',$id);
$this->db->delete('posts');
}}
在View.php中
<?php echo form_open('/posts/delete/'.$post['id']);?>
<input type="submit" value="Delete" class="btn btn-danger" name="delete">
routes.php文件
$route['posts/create'] = 'posts/create';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
答案 0 :(得分:0)
删除重定向帖子后, 哪个调用Posts控制器的索引功能。 你的问题帖子控制器索引方法,请 分享该方法或亲自检查。
答案 1 :(得分:0)
根据您的配置,posts/delete/2
路由到posts/view/delete/2
。将您的routes
配置更改为以下代码:
$route['posts/create'] = 'posts/create';
$route['posts/delete/(:num)'] = 'posts/delete/$1';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
答案 2 :(得分:0)
替换此代码
<强>控制器强>
class Posts extends CI_Controller{
...
public function delete(){
$id = $this->input->post('id');
$this->Post_Model->delete_post($id);
redirect('posts');
}
}
<强>模型强>
class Post_model extends Ci_Model{
...
public function delete_post($id) {
$this->db->where('id',$id);
$this->db->delete('posts');
}
}
查看强>
<?php echo form_open('/posts/delete'); ?>
<input type="hidden" name="id" value="<?php echo $post['id']; ?>" />
<input type="submit" value="Delete" class="btn btn-danger" name="delete">
</form>
答案 3 :(得分:0)
通过在控制器和路径
上执行此操作来解决控制器
$route['posts/delete/(:num)'] = 'posts/delete/$1'
Route.php
{{1}}