Following along with the CI News Tutorial
I'm only doing the news section so I changed the default controller to 'news'
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['default_controller'] = 'news';
Now a 404 error is generated from the 'View article' anchors. Changing the default to:
$route['default_controller'] = 'welcome';
creates the correct path. How should I change the router to use news?
No custom config, or .htaccess used.
From config.php:
$config['base_url'] = 'http://frameworks:8888/ci_site_tut/';
$config['index_page'] = 'index.php';
News Controller:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News Archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$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('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('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
} else {
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
SOLUTION:
Autoload URL Helper:
$autoload['helper'] = array('url');
Updated routes:
$route['default_controller'] = 'news';
$route['404_override'] = 'errors/page_missing';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
Updated URL in views/news/index.php:
<p><a href="<?php echo site_url('news/' . $news_item['slug']); ?>">View Article</a></p>
<a href="news/to<a href="view/and everything worked fine.