Static Pages Controller for CodeIgniter

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();
}
}
}

Drupal, where it is appropriate...

I don't know if one can even say using Drupal is appropriate in any situation. The current trend in web technologies is to get a list of keywords, formulate a project around those keywords, and then wind up with some massive headaches as a result. There are a few gems made with Drupal but the majority of sites that are made with the CMS don't scale and are haphazardly put together. There is an argument that a CMS should be usable right out of the box. No agreement is more simpler than install software => use said software. There should be no third party add-ons required.

This is where Drupal fails. To do anything remotely interesting with the CMS you have to install many and not always functioning third party modules. The modules that are built are mostly open source initiatives but they are also open source failures. Some of the modules are actually complete trash and don't scale. Basically if you are going to need a site with a full featured set of options and need scalability Drupal is not your best bet as you will wind up just customizing the third party modules anyway. It takes me approximately three-four hours to implement many of these third party features using my preferred framework CakePHP and then add the features I want on top of it as well (hint build it in CakePHP or an MVC based framework).

To Drupal's success though they have pretty much made making a blog idiot proof. No other tool allows you to setup a blog with some relative ease, download hundreds of third party additions and make your shared hosting provider weep like a little girl who just dropped her ice cream on a hot summer day. Some may say I don't know what I am talking about, but I do! I was the "go to guy" on thrillist.com. That website is 100% Drupal and it doesn't use MySQL it uses PostgreSQL, so I am well versed in the scalability of Drupal, the weaknesses and strengths. In closing, don't jump on the bandwagon, especially if it is going to run off a cliff.

 1

About

User