Using Routes with CodeIgniter

On many web sites, there is a contact page, about us page, and other pages at the root level, ie mydomain.com/about/ or mydomain.com/about.php. With CodeIgniter, the first URI segment, assuming you're using the mode_rewrite, is supposed to be the name of the controller, and the second segment is the name of the function. I didn't want to have the name of my controller in the URL for these root level pages. I think the "main", or whatever controller name I might later choose, in overfood.com/main/about/ makes the URL longer than it needs to be. It's almost as ugly as when I see people have "html" in their URL, which is often the case when people use content management systems or some sort of template

With the help of the people on the CodeIgniter forums, I found the solution to my problem. It's not really a problem, but rather a preference, I suppose. I knew part of the solution was to use the "routes.php" file, but I wasn't using it with all the right parameters. The obvious parameter was the "default_controller", which I set to "main". What I was missing was individual settings for each root level page. Since I have an "about us" page, I needed a setting for that. I also needed a setting for the "contact us" page. With just these two extra lines, I was able to accomplish what I needed.

So, now my routes.php file looks like ...

$route['default_controller'] = "main";
$route['scaffolding_trigger'] = "I_would_be_dumb_to_leave_this_here";

$route['about'] =  'main/about';
$route['contactus'] =  'main/contactus';

Just as a side note, it got a little more complicated when trying to send messages with the contact page. I didn't want to add another line to my routes page for the "page" that handles the emailing of the message. So, I used "/contactus/send_message" as the action of my form. This made my "contactus" function a little longer since it now has to handle displaying a blank form, validating the form data, and whatever happens as a result of that, which would be sending the email or redisplaying the form with a proper error message.

The solution here was just to do some if/then checking of the "$this->uri->segment(2)" variable. I'd rather go this route than have to manage another view file and adding another line to the routes.php file. Not too bad.




CodeIgniter with Simpleviewer and Flickr

Tonight's work focused on redoing my basic Flickr photo search. Rather than just doing a search on tags and slapping the results to the screen, I wanted something nicer looking. I've used SimpleViewer in the past, and I thought it would work well here. Once I found FlickrViewer, which integrates the Flickr API with SimpleViewer, then I knew I was on route that would work.

I didn't use FlickrViewer as it came, which only pulled images from your sets. I wanted to continue using a tag search, so I had to rewrite the call to use the "flickr.photos.Search" method instead of the "flickr.photosets.getPhotos". That wasn't too bad since I had originally did that the other night. The big change here was that my output string had to be in XML format for SimpleViewer to be able to read the data into the Flash object.

The XML was very basic, but what was interesting is how SimpleViewer appends the name of an image to a thumbnail URL base and a larger sized URL base. That made sense, but with FlickrViewer, the images were being pulled directly from Flickr and not from a directory on the same server. To get around that they used "flickrViewer.php?mode=img&size=large&image=", and that would in turn pull from "http://static.flickr.com/". Hmm, pretty neat, huh?

If you've played with CodeIgniter long enough, you'd know that you don't pass variables through the query string. Instead you use "URI segments". So, I had to make mine go to "/photos/load/large/" and "/photos/load/thumb/" and append the "_t.jpg" and ".jpg" for the thumbnail and larger image.

So, in the end, my code is an offshoot of FlickrViewer. One thing that it also does, which I haven't tried yet, is the caching of the XML. My page starts out with the "overfood" tag in the search box, and I should probably cache the XML for that tag. It would also be nice to put in some pagination too. Right now, I can only grab the first 50 photos for the tag, and you can't get any more. That should be easy enough to do, but that's enough for tonight!




Finally online

After much toil and trouble, I've finally uploaded a somewhat working version of the site. This new version of overfood.com is my first foray into using PHP frameworks. It's built with the CodeIgniter framework, which was fun to work with in the beginning.

CI is still nice, but it was tedious to move all my working code into the MVC structure. I was using the Smarty template system before, so I wasn't using a true MVC system. Add to that my sparse OOP background, and this is what you get =) I can do PHP just fine, but OOP just doesn't come naturally.

I've stripped this version down to just a few features. Only the Food Finder is done in CI. The blog and photos are using prior code. Ah, I'm trying to work in a login system too. That's been rough by itself!

Well, this is day one, just in time for April Fool's Day. I hope this site doesn't become a big joke!




Page :  1