Scaffolding in the real world
October 02nd, 2007
I’ve always told people that scaffolding should NOT be used “in the wild”. Here’s proof (note the strategic cropping of beer from the frame).
October 02nd, 2007
I’ve always told people that scaffolding should NOT be used “in the wild”. Here’s proof (note the strategic cropping of beer from the frame).
September 24th, 2007
OK, these are the laziest of possible posts, but there have been a few things I’ve found in the last few days that I wanted to bring up.
September 23rd, 2007
One of the signs of an application’s popularity is when a community and books start to spring up around it. The CodeIgniter community has never been questioned, and now it also has a book to accompany it. CodeIgniter for Rapid PHP Application Development. The book is written by David Upton, who has a clean, friendly style. His writing is clear, and examples are plentiful.
I’ve got a copy, and in fact, during the early stages of the books development, I had an opportunity to do some reviewing (with Rick Ellis). So let’s get this out of the way now shall we. Should you buy this book?
September 12th, 2007
CodeIgniter developer Michael Wales started an interesting kind of tutorial today, as he live blogged it.
I think we’re all familar with Live Blogging, the act of updating a specific blog posts continuously through an Expo or something of that nature. Today I’m going to try something a little bit new here - a live tutorial. We’ll be creating a very basic blog using the CodeIgniter framework - I’m not going to run through Installation of a web server or anything like that. Just make sure you have a fresh install of CodeIgniter up and ready to go.
Interesting work Michael, and I hope we see more of this!
August 29th, 2007
Yes, I am on vacation still, this post was pre-written the same day that I wrote Error Handling in CodeIgniter, so I thought I’d schedule it for release mid-week (even though its not much of a post), sorry.
I just wanted to clarify the name of the framework. Its CodeIgniter, all one word, capital “c” and “i”. Its true that we used to refer to ourselves as codeigniter, Code Igniter, CodeIgniter and lots of other variations, but ever since the rebranding of the EllisLab familiy of sites we’ve decided to make it consistently “CodeIgniter”.
I’ll tell you one thing, its a heck of a lot easier to search on ;)
August 22nd, 2007
This entry gets a little nerdy, and into the underlying depths and behaviour of CodeIgniter. If you aren’t interested in the explanations, and just want to know what a good best-practice might be for application development with CodeIgniter, then just skip to the bottom paragraph now. If however you are a glutton for convoluted writing and technical details, then read on!
Recently, I dealt with the idea of PHP error messages and logging in CodeIgniter. Since CodeIgniter subverts PHP errors for its own purposes, I thought I’d write a bit about how it works.
August 19th, 2007
Some interesting items in the past week.
Ed Finkler, the man responsible for the great CodeIgniter podcast PHP Abstract, is at it again. This time, he’s down in Atlanta offering Intro to CodeIgniter for PHP works ‘07. Go Ed! If you’re in Atlanta, find that man and buy him a beer.
I’ve also seen a rise of interest in BambooInvoice recently. Eric Davis wrote Simple and easy to use invoices - Bamboo Invoice and Customizing BambooInvoice. Great work thanks. My favourite quote?
The thing I like most about it is that it provides just what I need to invoice my customers; it does not try to provide accounting, supply chain management, or any other “total business solution”.
And that my friends, is why Bamboo is around. Thanks Eric. Nice writeup. If anyone else has written about Bamboo, please do let me know, I’m always interested in reading those posts.
In ExpressionEngine land, there are a few neat things happening also. Smashing Magazine, in a writable of the RubyOnRails content management system Mephisto, wrote that ExpressionEngine is the first-class engine for professionals; if you’d like to achieve the highest level of flexibility and have the full control over the outer appearance and structure of your weblogs, EE is the first option you should probably consider
.
Also, Les Camacho (our fearless VP, and all around cool guy) started a weekly blog entry called “last week on the forums” where he highlights interesting and notable posts from the ExpressionEngine forums. One of the ones that really stuck out at me was a plugin by silenz called trunchtml. Nice work silenz.
August 16th, 2007
A week ago (August 9th) we quietly added some nice enhancements to the CodeIgniter Session library that I wanted to mention. You can see these for yourself in the subversion repository (here’s the Session.php library and the userguide page). The upgrades will be part of the next CodeIgniter release. Three notables include (in descending order of sexiness):
$time_to_update was a variable hardcoded to 300 (5 minutes in seconds). After the time to live expired, the CI session class runs sess_update, which does some general maintenance and session handiwork. While 5 minutes is probably a very good choice for 99% of us, there may be times when you want the update to run faster or slower. So the update time is now configurable by adding $config[‘sess_time_to_update’] to your config file with the rest of the session preferences. If you choose not to add it, CI just assumes 5 minutes for you.
When a session is created, CodeIgniter (well, all PHP applications really) creates a “session id” and assigns it uniquely to you. In this manner, data can be exchanged with you without also giving away your session data to other visitors. CodeIgniter now regenerates your id every time sess_update runs (this is of course what makes the configurable time noteworthy). This provides an additional layer of protection against session fixation.
OK, now on to the most impressive addition - Flashdata. Flashdata are variables that only exist for the next request. They are mostly useful for “flashing” messages like The person $person was successfully edited
, but can extraordinarily useful in the general development of a web application. In Bamboo I use them to indicate success or failure messages to the user, store whom was edited for dynamic dropdowns, store a client name for assigning of invoices, and other various purposes. Again… very useful.
Using them is almost the same as using the current session library.
$this->session->set_flashdata('foo', 'bar'); // a variable that only exists for 1 request
$this->session->flashdata('foo'); // reading a flashdata variable
There is also a keep_flashdata() function, should you need to preserve from one request to another, perhaps a redirect.
If you’ve used any of the wonderful third party CodeIgniter session libraries, you’ve probably already enjoyed some of these features, and I suspect they will be a welcome addition to your toolkit. On that note though, I’d like to personally take a moment to thank some of the people who have already done great work in this area. Thanks Dariusz Debowczyk (native session), Oscar Bajner (OBsession), Monte Ohrt ( PHP Session), and Dready (DB Session) for their fine work in this area.
This was not an attempt to re-write the session library, but rather to add in a few commonly requested and useful features. Enjoy!, and I hope it takes your programming to even higher heights!
August 14th, 2007
A recent CodeIgniter bug report had got me looking into the depths of the database results functions of the framework. Essentially, the orderby() function of CI’s Active Record says that you can sort by ASC (ascending), DESC (descending) or RAND(). Imagine this:
$query = "SELECT * FROM table ORDER BY RAND()";
Anyone familiar with PHP probably looks and that and thinks of the native PHP rand() function. This is pretty neat actually, and I’d never really thought about randomly ordering things. But on further investigation, it became clear that this code wasn’t as nice as it initially seemed. Firstly, its non-standard SQL, reducing its portability greatly. While MySQL uses RAND(), other databases have their own way of doing things:
So then, how would one randomly order their database results? Read on for my solution…
August 12th, 2007
CodeIgniter is one of the most flexible, powerful, unobtrusive frameworks you could choose to use. It’s so useful in many ways, including pre-existing convenience libraries (I can’t live without the email library anymore), plugins (for handy little tools such as javascript calendars and CAPTCHA creation) and the appropriately named “helpers”.
In the world of CodeIgniter libraries, plugins and helpers, the libraries are Rock Stars. They get most of your attention, they do most of the heavy lifting. They date super-models, party in Europe, and return on Monday to keep your application humming along (sometimes Tuesday… you know those darned unreliable rock stars). They can be extended, overwritten, and you can create your own, wholly new libraries just for your application. Wow. No wonder super-models want to date them.
One of the most handy aspects of libraries, is that you can create a libraries folder in system/application/libraries, and leave the existing CodeIgniter libraries untouched. This makes upgrading to newer versions of CI a breeze, as you never need to worry about going in and re-implementing all the changes you made to a library. From the userguide:
As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.
And while libraries are doing all this work, we’re left with the humble, but infinitely handy helper. Here’s what the userguide has to say about helpers.
Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.
Makes them sound like one-trick ponies; but they are not. Helpers, while not quite as flexible as libraries, are tremendously useful. They are written procedurally, instead of object oriented (as the rest of CodeIgniter is). And while they can’t be extended as libraries can, a little known fact is that they can be overwritten. Better yet, is the method in which you’d override them. Much like libraries, the trick lies in creating a folder underneath system/application for helpers. Then, when CodeIgniter loads helpers, it looks first in your application/helpers folder, and only if that folder can’t be found does it then fall back on the default system/helpers folder. So you can add your own helpers, or overwrite existing ones.
Here’s a case study. In BambooInvoice I use the date helper’s timespan() function to determine exactly how overdue an invoice might be. My problem is that the default timespan() function keeps tabs down to the second of the span of time. When tracking invoices, I don’t care how many hours, minutes and seconds have passed, only how many days and weeks (or months if you have a really bad-paying client ;)) So for use in Bamboo, I want that helpers default output changed. The solution, to remove or comment out those lines of the helper.
But the problem with modifying the system helper is that in the next upgrade, that helper gets overwritten again. Since I use BambooInvoice as part of my testing codebase for vetting new CodeIgniter code, I tend to overwrite these files a lot. This then required carefully keeping track of what helpers were modified, and introduced some other problems. A preferable approach would be to simply make a copy of system/helpers/date_helper.php in application/helpers. Now, the application helper will be loaded preferentially, and I can modify to my hearts content without ever fearing I’ll accidentally overwrite my changes.
Application based helpers are a handy feature indeed. There have been discussions about changing the procedural structure of helpers to allow for extending and over-riding, but for now, the simplicity of helper functions are preserved, and you can easily maintain your own modified versions.
How are you using CodeIgniter helpers? If you’ve modified any of the base helpers, or created your own, I’d love to hear about it here.