According to the tutorial, it is possible to handle all the requests for static pages through application/controllers/pages.php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if( ! file_exists('application/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('templates/nav', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/aside_right', $data);
$this->load->view('templates/bottom', $data);
}
}
This works for the “home”-page, but I don’t seem to be able to call views/pages/about for example.
I tried to make a seperate controller for the about-page. It works, but it feels somewhat wrong.
application/controllers/about.php
class About extends CI_Controller {
public function index()
{
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/about');
$this->load->view('templates/aside_right');
$this->load->view('templates/bottom');
}
}
I also have issues with my htaccess file or routes file. With the About-controller written above, I can only get to the page by typing domain.com/index.php/about. I’d like it to be domain.com/about etc.
This is what my routes.php’s like:
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
My Htaccess:
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
As you already said, you don’t need to use another controller for the
aboutpages. Your problem is with yourroutes.php.In this way, it will search for a controller named “about” and it doesn’t find the controller. If you erase the first line:
It should work. In this case, as you request any page, for example
aboutit will callpages/view/about, wherepagesis the controller,viewis the function andaboutis the argument that is passed to the function (replacing the default$page = home).I also spotted another error in your logic. You wrote
You don’t have to call
view/pages/about. You’ve to callpages/view/about. Remember that the syntax is always the sameController/Function/Variable1/Variable2/Variable3. So, you should be able to see theaboutpage withhttp://yourdomain.com/index.php/pages/view/aboutor only withhttp://yourdomain.com/aboutif you have the$route['(:any)'] = pages/view/$1rules in yourroutes.php.