routes.php
$route['admin/news'] = 'admin_news/index'; //working
$route['admin/news/(:any)'] = 'admin_news/view/$1'; //working
$route['admin/news/create'] = 'admin_news/create'; //working
$route['admin/news/edit/(:any)'] = 'admin_news/edit/$1'; //not-working
$route['admin/news/delete/(:any)'] = 'admin_news/delete/$1'; //not-working
controllers: admin_news.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_news extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
if(!$this->session->userdata('is_logged_in')){
redirect('admin/login');
}
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('admin/includes/header', $data);
$this->load->view('admin/news/index', $data);
$this->load->view('admin/includes/footer');
}
public function view($slug = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
//$this->load->view('templates/header', $data);
$this->load->view('admin/news/view', $data);
//$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news 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('admin/includes/header', $data);
$this->load->view('admin/news/create', $data);
$this->load->view('admin/includes/footer');
}
else
{
$this->news_model->set_news();
$this->load->helper('url');
$this->index();
}
}
public function edit($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = 'Edit: '.$data['news_item']['title'];
$this->load->helper('form');
$this->load->library('form_validation');
$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('admin/includes/header', $data);
$this->load->view('admin/news/edit', $data);
$this->load->view('admin/includes/footer');
}
else
{
$this->news_model->update_news( $this->input->post('id'),
$this->input->post('title'),
$this->input->post('text'));
$data['news_item'] = $this->news_model->get_news($slug);
$this->load->view('admin/includes/header', $data);
$this->load->view('admin/news/success');
$this->load->view('admin/news/edit', $data);
$this->load->view('admin/includes/footer');
}
}
public function delete($id = NULL) {
$this->news_model->delete_news($id);
$this->load->helper('url');
$this->index();
}
}
models: News_model.php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$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('news', $data);
}
/*public function update_news($slug = FALSE)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'),'dash',TRUE);
$data = array(
'slug' => $slug,
'title' => $this->input->post('title'),
'text' => $this->input->post('text')
);
$this->db->where('slug', $slug);
return $this->db->update('news', $data);
}*/
public function update_news($id, $title, $text) {
$data = array(
'title' => $title,
'text' => $text
);
$this->db->where('id', $id);
$this->db->update('news', $data);
}
public function delete_news($id = FALSE)
{
$this->db->delete('news', array('id' => $id));
}
}
views: admin/news/edit.php
Edit a news item
Title
Text
When I am access
`http://localhost/ciadmin/admin/news/edit/news-slug`showing 404 Page Not Found message!!
Answer
Put it this way:
$route['admin/news/delete/(:any)'] = 'admin_news/delete/$1';
$route['admin/news/edit/(:any)'] = 'admin_news/edit/$1';
$route['admin/news/create'] = 'admin_news/create';
$route['admin/news/(:any)'] = 'admin_news/view/$1';
$route['admin/news'] = 'admin_news/index';
Remember:
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
Docs.
No comments:
Post a Comment