I have the following code in my routes.php in a codeigniter project.
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'login/view/$1';
$route['default_controller'] = 'login/view';
The following url takes me to the about page correctly (which is located here: views/pages/about.php)
http://localhost:1234/ciblog/index.php/pages/view/about
Also http://localhost:1234/ciblog/index.php/news takes me to the news page correctly which is located here: views/news/index.php
I would like, however, http://localhost:1234/ciblog/index.php/about to take me to the about page, and instead a 404 error is returned. To clarify, I don't want to have to type in the "pages/view" in the url, to get to the about page. And the same for the login page. I don't want to have to type in login/view ....to get to the login.php page.
I thought that this line code, arranged for this to be routed such that pages/view is bypassed (that is the views/pages directory) so that it goes straight to the about.php page, but clearly, I am missing some logic.
The controller for the Pages.php is as so:
<?php
class Pages extends CI_Controller{
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
Can someone point out what I've done wrong, but also, as an accepted answer, explain how exactly the routing works in simple terms. If I wanted to simply create a new page, and create a route (url) that allowed me to access that page from the localhost/page (i.e directly) what is the best method to do so?
For reference, a screenshot of the directory structure can be seen on the left.