登录  /  注册
codeigniter我的删除不知道是不能传参还是路由问题
php中文网
发布: 2016-06-23 14:00:10
原创
558人浏览过


路由是这样的
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 会找不到文件的话
就表示你的 web 服务器不支持 PATH_INFO

如果web 服务器不支持 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';的前面,否则会背后截获

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学