codeigniter我的刪除不知道是不能傳參還是路由問題

來源:互聯網
上載者:User
路由是這樣的
PHP複製代碼
$route['default_controller'] = 'pages/view';

$route['content/(:any)'] = 'content/view/$1';
$route['content'] = 'content';
$route['content/del/(:any)'] = 'content/del/$1';
$route['content/add'] = 'content/add';
$route['(:any)'] = 'pages/view/$1';

$route['404_override'] = '';
複製代碼




控制器是這樣的
PHP複製代碼

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

public function index()
{
$data['title'] = 'Content archive';
$data['content'] = $this->content_model->get_content();

$this->load->view('templates/header', $data);
$this->load->view('content/index', $data);
$this->load->view('templates/footer');
}

public function view($slug)
{
$data['content_item'] = $this->content_model->get_content($slug);

if (empty($data['content_item']))
{
show_404();
}

$data['title'] = $data['content_item']['title'];

$this->load->view('templates/header', $data);
$this->load->view('content/view', $data);
$this->load->view('templates/footer');
}

public function add()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Add a content item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');

if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('content/add');
$this->load->view('templates/footer');

}
else
{
$this->content_model->set_content();
$this->load->view('templates/header', $data);
$this->load->view('content/add_success');
$this->load->view('templates/footer');
}
}

public function del($slug)
{
$this->content_model->del_content($slug);
}

}
複製代碼


資料模型是這樣的
PHP複製代碼

public function __construct()
{
$this->load->database();
}

public function get_content($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('content');
return $query->result_array();
}

$query = $this->db->get_where('content', array('id' => $slug));
return $query->row_array();
}

public function set_content()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);

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

public function del_content($slug)
{
$this->db->where('id',$slug);
$this->db->delete('content');
}
}
複製代碼


我http://localhost/ci//index.php/content/del/1
後就是404 Page Not Found
不知道是不能傳參還是路由問題


回複討論(解決方案)

如果象 http://localhost/ci/index.php/content/del/1 這樣的 url 會找不到檔案的話
就表示你的 網頁伺服器不支援 PATH_INFO

如果網頁伺服器不支援 PATH_INFO,
為什麼
http://localhost/ci//index.php/content/1
確能顯示文章

$route['content/(:any)'] = 'content/view/$1';

寫路由的一個原則:會被包含的規則要寫在包含他的規則上面,否則就應用不到了。

5樓正解,
$route['content/del/(:any)'] = 'content/del/$1';一定要放在
$route['content/(:any)'] = 'content/view/$1';的前面,否則會背後截獲

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.