2017-06-08 126 views
0

IM刪除在coneigniter2一排,但它需要我的404頁 (您請求未找到頁。)錯誤404笨2

這是我的代碼

的Controler郵政

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

刪除'$ id'爭論就這樣做解決來自c ontroller函數並使用'$ id = $ this-> uri-> segment(3);' – ImBS

+0

我得到了同樣的錯誤,它找到了id,但是像這樣[http:// localhost/blog/posts/delete/2 ] –

+0

請將您的'routes'配置添加到問題中。 –

回答

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> 
+0

上使用此代碼它不會收回該ID –

+0

如果有可能檢查隱藏的輸入,你可以得到ID在隱藏輸入? – ImBS

+0

我壞你在哪裏可惜它工作nows –

0

刪除後您重定向其上的帖子, 哪個調用Posts控制器的索引功能。 在您的帖子控制器索引方法的問題,請分享該方法或自己檢查它 。

+0

我在問題 –

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; 
0

控制器和路線

控制器

class Posts extends CI_Controller{ 
... 
    public function delete(){ 
    $id = $this->uri->segment(3); 
     $this->Post_model->delete_post($id); 
      redirect('posts'); 
    } 
} 

Route.php

$route['posts/delete/(:num)'] = 'posts/delete/$1'