I was looking into doing some work with CodeIgniter but unlike CakePHP they did not have a default controller installed for pages that are just a static view. Sometimes a controller with a defined function is not always necessary and or needed to display a page to the user. Of course if you wanted you could bloat your controller class with a million functions that map to each individual view or we can have one function that maps to the view dynamically. Here is the raw code for my class in question:
/*
Pages Controller for CI
Anthony Wlodarski
ant92083 at gmail dot com
http://www.anthonyw.net
2/16/2010
This class requires custom configuration of CI for static pages to be
rendered and displayed correctly.
1.) routes.php must be configured to contain the line:
$route['pages/:any'] = "pages";
2.) All static php files for views must be contained in a sub folder of views called "pages".
3.) index.php must be implemented in the "pages" subfolder.
4.) All views must have a *.php extension.
*/
class Pages extends Controller{
private $views;
// constructor
function Pages()
{
parent::Controller();
// build a an array of views
$this->load->helper('file');
$this->views = get_filenames(getcwd() . '/system/application/views/pages/');
}
function index()
{
// get the second segment of the url
$view = $this->uri->segment(2);
// special case if $view == false then we have come to a special case of the index page needing to be displayed
if(!$view)
{
$view = 'pages/index';
$this->load->view($view);
return 0;
}
if(in_array($view.'.php', $this->views))
{
// move onto loading this view or then pass a 404
$this->load->view('pages/'.$view);
}
else
{
// the view as not in the "pages" folder and cannot be displayed
show_404();
}
}
}