7/13/08
Why I’m Building My Own Site
I should preface this post by saying that this will be the first of many very nerdy posts. This site isn’t completely done because I’m building it from scratch using CodeIgniter. By programming the site myself, I can make exactly what I want and hopefully apply it to future projects. I will be documenting some of my bigger A-HA! moments here on the blog. You have been warned.
I want to hone my skills as a programmer, but the truth is: every off-the-shelf CMS out there tends to be overkill for what I want to do or is too blog-centric. In the case of my portfolio, I want a CMS that presents me with a place to put my description and a place to put files, and be done with it. Another CMS would require me to shoehorn how I want my portfolio to appear into a template that I have to fill out, and then that content becomes harder to reuse later. That’s why I have a portfolio controller and a portfolio model in CodeIgniter, as well as separate portfolio and files tables in the database. I could completely redesign the portfolio section a year from now and not worry about having to change any of the content.
This weekend, I found myself needing to bend CodeIgniter to my will. That I was actually able to showed me how much I like its malleability over other frameworks.
I created a new pages database for all of that static content that every site has, like the About ARLOdesign® page. I created a page controller, put the code to pull the appropriate page in the index function, and then made a route:
$route['about'] = ‘page/index/about’;
That worked dandy, but it got me thinking: What if I add a new page? Does that mean I have to add a new route every time? I can’t expect another user who might want to use my application to edit their own routes; that’d be silly. Shouldn’t everything be a page unless I specifically create a controller to do something special? Wouldn’t it make more sense if:
- CodeIgniter checks to see if a controller exists.
- If a controller doesn’t exist, send the request to the page controller.
- If the requested page doesn’t exist, send a 404 message.
It took awhile, but I ultimately came up with the solution.
Obviously, CodeIgniter has to see if a controller exists: it’s in the Router.php library. I found this post. Now I have MY_Router.php sending requests to the page controller with:
return array('page', 'index', $segments[0]);
Problem solved, right? Except for one caveat: I wanted a custom 404 page, and I wanted that page content to also be editable from the pages database table. Now it gets fun. It was easy to have page controller return the 404 page if the database query returned zero results, but what about the portfolio controller? I can’t just redirect the user to a 404 page because the URL in the browser would change, preventing the user from saying, “Oh, there’s a typo” or something.
Ultimately, the answer was to create MY_Exceptions.php and extend the show_404 function. I copied my code from the page controller and loaded the 404 page. Sure, there’s probably a better way than having to copy and paste code like that, but it seemed like a great, quick hack to accomplish what I needed. It also required an even uglier hack to actually display the views, invoking the output class manually. It worked, but I think it can be improved. UPDATE: It became a library.
The point of all this: I learned more about how my site will operate than I did before. I learned some new skills, and I have more to be proud of on this site than just the content. That’s pretty damn satisfying. That’s why I’m building my own site: because when it’s exactly what I want it to be, it means I learned a lot on the way.
FYI: The blog is running in WordPress, but that’s temporary. I seriously plan to replace it, as WordPress really is more than I need.
