Reputation: 1373
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]
Upvotes: 0
Views: 869
Reputation: 11900
As you already said, you don't need to use another controller for the about
pages. Your problem is with your routes.php
.
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
In this way, it will search for a controller named "about" and it doesn't find the controller. If you erase the first line:
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
It should work. In this case, as you request any page, for example about
it will call pages/view/about
, where pages
is the controller, view
is the function and about
is the argument that is passed to the function (replacing the default $page = home
).
I also spotted another error in your logic. You wrote
This works for the "home"-page, but I don't seem to be able to call views/pages/about for example.
You don't have to call view/pages/about
. You've to call pages/view/about
. Remember that the syntax is always the same Controller/Function/Variable1/Variable2/Variable3
. So, you should be able to see the about
page with http://yourdomain.com/index.php/pages/view/about
or only with http://yourdomain.com/about
if you have the $route['(:any)'] = pages/view/$1
rules in your routes.php
.
Upvotes: 2