用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1541|回复: 5
收起左侧

[HELP] 我的删除不知道是不能传参还是路由问题

[复制链接]
发表于 2014-3-27 10:01:47 | 显示全部楼层 |阅读模式
路由是这样的
PHP复制代码
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$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复制代码
<?phpclass Content extends CI_Controller {
 
        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复制代码
<?phpclass Content_model extends CI_Model {
 
        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
不知道是不能传参还是路由问题
 楼主| 发表于 2014-3-27 10:02:47 | 显示全部楼层
而且数据确实没被删除
发表于 2014-3-27 10:10:44 | 显示全部楼层
你试着不用model   直接echo看有值传进去没有
 楼主| 发表于 2014-3-27 10:55:56 | 显示全部楼层
本帖最后由 goyuquan 于 2014-3-27 11:07 编辑

还是404,好像是路由问题
发表于 2014-3-27 11:21:45 | 显示全部楼层
路由有优先级
看看你的操作是不是被第一个路由截获了
 楼主| 发表于 2014-3-27 11:46:56 | 显示全部楼层
果然啊,
$route['content/del/(:any)'] = 'content/del/$1';
真的被
$route['content/(:any)'] = 'content/view/$1';截获了

昨天晚上弄到半夜

本版积分规则